Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_aaa_user.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Dag Wieers (dagwieers) <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"}
12
13 DOCUMENTATION = r"""
14 ---
15 module: aci_aaa_user
16 short_description: Manage AAA users (aaa:User)
17 description:
18 - Manage AAA users on Cisco ACI fabrics.
19 requirements:
20 - python-dateutil
21 options:
22 aaa_password:
23 description:
24 - The password of the locally-authenticated user.
25 - Providing this option will always result in a change because it is a secure property that cannot be retrieved from APIC.
26 type: str
27 aaa_password_lifetime:
28 description:
29 - The lifetime of the locally-authenticated user password.
30 type: int
31 aaa_password_update_required:
32 description:
33 - Whether this account needs password update.
34 type: bool
35 aaa_user:
36 description:
37 - The name of the locally-authenticated user user to add.
38 type: str
39 aliases: [ name ]
40 clear_password_history:
41 description:
42 - Whether to clear the password history of a locally-authenticated user.
43 type: bool
44 description:
45 description:
46 - Description for the AAA user.
47 type: str
48 aliases: [ descr ]
49 email:
50 description:
51 - The email address of the locally-authenticated user.
52 type: str
53 enabled:
54 description:
55 - The status of the locally-authenticated user account.
56 type: bool
57 expiration:
58 description:
59 - The expiration date of the locally-authenticated user account.
60 type: str
61 expires:
62 description:
63 - Whether to enable an expiration date for the locally-authenticated user account.
64 type: bool
65 first_name:
66 description:
67 - The first name of the locally-authenticated user.
68 type: str
69 last_name:
70 description:
71 - The last name of the locally-authenticated user.
72 type: str
73 phone:
74 description:
75 - The phone number of the locally-authenticated user.
76 type: str
77 state:
78 description:
79 - Use C(present) or C(absent) for adding or removing.
80 - Use C(query) for listing an object or multiple objects.
81 type: str
82 choices: [ absent, present, query ]
83 default: present
84 name_alias:
85 description:
86 - The alias for the current object. This relates to the nameAlias field in ACI.
87 type: str
88 extends_documentation_fragment:
89 - cisco.aci.aci
90 - cisco.aci.annotation
91 - cisco.aci.owner
92
93 notes:
94 - This module is not idempotent when C(aaa_password) is being used
95 (even if that password was already set identically). This
96 appears to be an inconsistency wrt. the idempotent nature
97 of the APIC REST API. The vendor has been informed.
98 More information in :ref:`the ACI documentation <aci_guide_known_issues>`.
99 seealso:
100 - module: cisco.aci.aci_aaa_user_certificate
101 - name: APIC Management Information Model reference
102 description: More information about the internal APIC class B(aaa:User).
103 link: https://developer.cisco.com/docs/apic-mim-ref/
104 author:
105 - Dag Wieers (@dagwieers)
106 """
107
108 EXAMPLES = r"""
109 - name: Add a user
110 cisco.aci.aci_aaa_user:
111 host: apic
112 username: admin
113 password: SomeSecretPassword
114 aaa_user: dag
115 aaa_password: AnotherSecretPassword
116 expiration: never
117 expires: false
118 email: [email protected]
119 phone: 1-234-555-678
120 first_name: Dag
121 last_name: Wieers
122 state: present
123 delegate_to: localhost
124
125 - name: Remove a user
126 cisco.aci.aci_aaa_user:
127 host: apic
128 username: admin
129 password: SomeSecretPassword
130 aaa_user: dag
131 state: absent
132 delegate_to: localhost
133
134 - name: Query a user
135 cisco.aci.aci_aaa_user:
136 host: apic
137 username: admin
138 password: SomeSecretPassword
139 aaa_user: dag
140 state: query
141 delegate_to: localhost
142 register: query_result
143
144 - name: Query all users
145 cisco.aci.aci_aaa_user:
146 host: apic
147 username: admin
148 password: SomeSecretPassword
149 state: query
150 delegate_to: localhost
151 register: query_result
152 """
153
154 RETURN = r"""
155 current:
156 description: The existing configuration from the APIC after the module has finished
157 returned: success
158 type: list
159 sample:
160 [
161 {
162 "fvTenant": {
163 "attributes": {
164 "descr": "Production environment",
165 "dn": "uni/tn-production",
166 "name": "production",
167 "nameAlias": "",
168 "ownerKey": "",
169 "ownerTag": ""
170 }
171 }
172 }
173 ]
174 error:
175 description: The error information as returned from the APIC
176 returned: failure
177 type: dict
178 sample:
179 {
180 "code": "122",
181 "text": "unknown managed object class foo"
182 }
183 raw:
184 description: The raw output returned by the APIC REST API (xml or json)
185 returned: parse error
186 type: str
187 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
188 sent:
189 description: The actual/minimal configuration pushed to the APIC
190 returned: info
191 type: list
192 sample:
193 {
194 "fvTenant": {
195 "attributes": {
196 "descr": "Production environment"
197 }
198 }
199 }
200 previous:
201 description: The original configuration from the APIC before the module has started
202 returned: info
203 type: list
204 sample:
205 [
206 {
207 "fvTenant": {
208 "attributes": {
209 "descr": "Production",
210 "dn": "uni/tn-production",
211 "name": "production",
212 "nameAlias": "",
213 "ownerKey": "",
214 "ownerTag": ""
215 }
216 }
217 }
218 ]
219 proposed:
220 description: The assembled configuration from the user-provided parameters
221 returned: info
222 type: dict
223 sample:
224 {
225 "fvTenant": {
226 "attributes": {
227 "descr": "Production environment",
228 "name": "production"
229 }
230 }
231 }
232 filter_string:
233 description: The filter string used for the request
234 returned: failure or debug
235 type: str
236 sample: '?rsp-prop-include=config-only'
237 method:
238 description: The HTTP method used for the request to the APIC
239 returned: failure or debug
240 type: str
241 sample: POST
242 response:
243 description: The HTTP response from the APIC
244 returned: failure or debug
245 type: str
246 sample: OK (30 bytes)
247 status:
248 description: The HTTP status from the APIC
249 returned: failure or debug
250 type: int
251 sample: 200
252 url:
253 description: The HTTP url used for the request to the APIC
254 returned: failure or debug
255 type: str
256 sample: https://10.11.12.13/api/mo/uni/tn-production.json
257 """
258
259 try:
260 from dateutil.tz import tzutc
261 import dateutil.parser
262
263 HAS_DATEUTIL = True
264 except ImportError:
265 HAS_DATEUTIL = False
266
267 from ansible.module_utils.basic import AnsibleModule
268 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
269
270
271 def main():
272 argument_spec = aci_argument_spec()
273 argument_spec.update(aci_annotation_spec())
274 argument_spec.update(aci_owner_spec())
275 argument_spec.update(
276 aaa_password=dict(type="str", no_log=True),
277 aaa_password_lifetime=dict(type="int", no_log=False),
278 aaa_password_update_required=dict(type="bool", no_log=False),
279 aaa_user=dict(type="str", aliases=["name"]), # Not required for querying all objects
280 clear_password_history=dict(type="bool", no_log=False),
281 description=dict(type="str", aliases=["descr"]),
282 email=dict(type="str"),
283 enabled=dict(type="bool"),
284 expiration=dict(type="str"),
285 expires=dict(type="bool"),
286 first_name=dict(type="str"),
287 last_name=dict(type="str"),
288 phone=dict(type="str"),
289 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
290 name_alias=dict(type="str"),
291 )
292
293 module = AnsibleModule(
294 argument_spec=argument_spec,
295 supports_check_mode=True,
296 required_if=[
297 ["state", "absent", ["aaa_user"]],
298 ["state", "present", ["aaa_user"]],
299 ["expires", True, ["expiration"]],
300 ],
301 )
302
303 aci = ACIModule(module)
304
305 if not HAS_DATEUTIL:
306 module.fail_json(msg="dateutil required for this module")
307
308 aaa_password = module.params.get("aaa_password")
309 aaa_password_lifetime = module.params.get("aaa_password_lifetime")
310 aaa_password_update_required = aci.boolean(module.params.get("aaa_password_update_required"))
311 aaa_user = module.params.get("aaa_user")
312 clear_password_history = aci.boolean(module.params.get("clear_password_history"))
313 description = module.params.get("description")
314 email = module.params.get("email")
315 enabled = aci.boolean(module.params.get("enabled"), "active", "inactive")
316 expires = aci.boolean(module.params.get("expires"))
317 first_name = module.params.get("first_name")
318 last_name = module.params.get("last_name")
319 phone = module.params.get("phone")
320 state = module.params.get("state")
321 name_alias = module.params.get("name_alias")
322
323 expiration = module.params.get("expiration")
324 if expiration is not None and expiration != "never":
325 try:
326 expiration = aci.iso8601_format(dateutil.parser.parse(expiration).replace(tzinfo=tzutc()))
327 except Exception as e:
328 module.fail_json(msg="Failed to parse date format '%s', %s" % (module.params.get("expiration"), e))
329
330 aci.construct_url(
331 root_class=dict(
332 aci_class="aaaUser",
333 aci_rn="userext/user-{0}".format(aaa_user),
334 module_object=aaa_user,
335 target_filter={"name": aaa_user},
336 ),
337 )
338 aci.get_existing()
339
340 if state == "present":
341 aci.payload(
342 aci_class="aaaUser",
343 class_config=dict(
344 accountStatus=enabled,
345 clearPwdHistory=clear_password_history,
346 descr=description,
347 email=email,
348 expiration=expiration,
349 expires=expires,
350 firstName=first_name,
351 lastName=last_name,
352 name=aaa_user,
353 phone=phone,
354 pwd=aaa_password,
355 pwdLifeTime=aaa_password_lifetime,
356 pwdUpdateRequired=aaa_password_update_required,
357 nameAlias=name_alias,
358 ),
359 )
360
361 aci.get_diff(aci_class="aaaUser")
362
363 aci.post_config()
364
365 elif state == "absent":
366 aci.delete_config()
367
368 aci.exit_json()
369
370
371 if __name__ == "__main__":
372 main()