Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2022, Tim Cragg (@timcragg)
5 # Copyright: (c) 2022, Sabari Jaganathan (@sajagana) <[email protected]>
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: aci_aaa_user_role
17 short_description: Manage AAA user roles (aaa:UserRole)
18 description:
19 - Manage AAA User Role configuration on Cisco ACI fabrics.
20 options:
21 aaa_user:
22 description:
23 - The name of the existing user to add roles and privileges
24 type: str
25 aliases: [ user_name ]
26 aaa_user_type:
27 description:
28 - Whether this is a normal user or an appuser.
29 type: str
30 choices: [ appuser, user ]
31 default: user
32 domain_name:
33 description:
34 - The name of the security domain
35 type: str
36 aliases: [ user_domain ]
37 name:
38 description:
39 - Name of the AAA role
40 type: str
41 aliases: [ role_name, user_role ]
42 privilege_type:
43 description:
44 - Privilege for the role
45 type: str
46 aliases: [ priv_type ]
47 choices: [ read, write ]
48 name_alias:
49 description:
50 - The alias for the current object. This relates to the nameAlias field in ACI.
51 type: str
52 state:
53 description:
54 - Use C(present) or C(absent) for adding or removing.
55 - Use C(query) for listing an object or multiple objects.
56 type: str
57 choices: [ absent, present, query ]
58 default: present
59 extends_documentation_fragment:
60 - cisco.aci.aci
61 - cisco.aci.annotation
62 - cisco.aci.owner
63
64 notes:
65 - The C(aaa_user) and C(domain_name) must exist before using this module in your playbook.
66 The M(cisco.aci.aci_aaa_user) and M(cisco.aci.aci_aaa_user_domain) modules can be used for this.
67 seealso:
68 - name: APIC Management Information Model reference
69 description: More information about the internal APIC class B(aaaUserRole).
70 link: https://developer.cisco.com/docs/apic-mim-ref/
71 author:
72 - Tim Cragg (@timcragg)
73 - Sabari Jaganathan (@sajagana)
74 """
75
76 EXAMPLES = r"""
77 - name: Add a user role to a user security domain
78 cisco.aci.aci_aaa_user_role:
79 host: apic
80 username: admin
81 password: SomeSecretPassword
82 aaa_user: my_user
83 domain_name: my_domain
84 name: my_role
85 privilege_type: read
86 state: present
87 delegate_to: localhost
88
89 - name: Add list of user roles to a user domain
90 cisco.aci.aci_aaa_user_role:
91 host: apic
92 username: admin
93 password: SomeSecretPassword
94 aaa_user: my_user
95 domain_name: my_domain
96 name: "{{ item.name }}"
97 privilege_type: "{{ item.privilege_type }}"
98 state: present
99 with_items:
100 - name: aaa
101 privilege_type: write
102 - name: access-admin
103 privilege_type: write
104 - name: ops
105 privilege_type: write
106 delegate_to: localhost
107
108 - name: Query a user role from a user security domain
109 cisco.aci.aci_aaa_user_role:
110 host: apic
111 username: admin
112 password: SomeSecretPassword
113 aaa_user: my_user
114 domain_name: my_domain
115 name: my_role
116 state: query
117 delegate_to: localhost
118 register: query_result
119
120 - name: Query all user roles from a user security domain
121 cisco.aci.aci_aaa_user_role:
122 host: apic
123 username: admin
124 password: SomeSecretPassword
125 aaa_user: my_user
126 domain_name: my_domain
127 state: query
128 delegate_to: localhost
129 register: query_results
130
131 - name: Query all user roles from a user
132 cisco.aci.aci_aaa_user_role:
133 host: apic
134 username: admin
135 password: SomeSecretPassword
136 aaa_user: my_user
137 state: query
138 delegate_to: localhost
139 register: query_all_roles_of_user
140
141 - name: Query all user roles
142 cisco.aci.aci_aaa_user_role:
143 host: apic
144 username: admin
145 password: SomeSecretPassword
146 state: query
147 delegate_to: localhost
148 register: query_all_user_roles
149
150 - name: Remove user role from a user domain
151 cisco.aci.aci_aaa_user_role:
152 host: apic
153 username: admin
154 password: SomeSecretPassword
155 aaa_user: my_user
156 domain_name: my_domain
157 name: my_role
158 state: absent
159 delegate_to: localhost
160 """
161
162 RETURN = r"""
163 current:
164 description: The existing configuration from the APIC after the module has finished
165 returned: success
166 type: list
167 sample:
168 [
169 {
170 "fvTenant": {
171 "attributes": {
172 "descr": "Production environment",
173 "dn": "uni/tn-production",
174 "name": "production",
175 "nameAlias": "",
176 "ownerKey": "",
177 "ownerTag": ""
178 }
179 }
180 }
181 ]
182 error:
183 description: The error information as returned from the APIC
184 returned: failure
185 type: dict
186 sample:
187 {
188 "code": "122",
189 "text": "unknown managed object class foo"
190 }
191 raw:
192 description: The raw output returned by the APIC REST API (xml or json)
193 returned: parse error
194 type: str
195 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
196 sent:
197 description: The actual/minimal configuration pushed to the APIC
198 returned: info
199 type: list
200 sample:
201 {
202 "fvTenant": {
203 "attributes": {
204 "descr": "Production environment"
205 }
206 }
207 }
208 previous:
209 description: The original configuration from the APIC before the module has started
210 returned: info
211 type: list
212 sample:
213 [
214 {
215 "fvTenant": {
216 "attributes": {
217 "descr": "Production",
218 "dn": "uni/tn-production",
219 "name": "production",
220 "nameAlias": "",
221 "ownerKey": "",
222 "ownerTag": ""
223 }
224 }
225 }
226 ]
227 proposed:
228 description: The assembled configuration from the user-provided parameters
229 returned: info
230 type: dict
231 sample:
232 {
233 "fvTenant": {
234 "attributes": {
235 "descr": "Production environment",
236 "name": "production"
237 }
238 }
239 }
240 filter_string:
241 description: The filter string used for the request
242 returned: failure or debug
243 type: str
244 sample: ?rsp-prop-include=config-only
245 method:
246 description: The HTTP method used for the request to the APIC
247 returned: failure or debug
248 type: str
249 sample: POST
250 response:
251 description: The HTTP response from the APIC
252 returned: failure or debug
253 type: str
254 sample: OK (30 bytes)
255 status:
256 description: The HTTP status from the APIC
257 returned: failure or debug
258 type: int
259 sample: 200
260 url:
261 description: The HTTP url used for the request to the APIC
262 returned: failure or debug
263 type: str
264 sample: https://10.11.12.13/api/mo/uni/tn-production.json
265 """
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 ACI_MAPPING = dict(
271 appuser=dict(
272 aci_class="aaaAppUser",
273 aci_mo="userext/appuser-",
274 ),
275 user=dict(
276 aci_class="aaaUser",
277 aci_mo="userext/user-",
278 ),
279 )
280
281 PRIV_TYPE_MAPPING = {
282 "read": "readPriv",
283 "write": "writePriv",
284 }
285
286
287 def main():
288 argument_spec = aci_argument_spec()
289 argument_spec.update(aci_annotation_spec())
290 argument_spec.update(aci_owner_spec())
291 argument_spec.update(
292 aaa_user=dict(type="str", aliases=["user_name"]),
293 aaa_user_type=dict(type="str", default="user", choices=["appuser", "user"]),
294 domain_name=dict(type="str", aliases=["user_domain"]),
295 name=dict(type="str", aliases=["role_name", "user_role"]),
296 privilege_type=dict(type="str", aliases=["priv_type"], choices=["read", "write"]),
297 name_alias=dict(type="str"),
298 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
299 )
300
301 module = AnsibleModule(
302 argument_spec=argument_spec,
303 supports_check_mode=True,
304 required_if=[
305 ["state", "absent", ["aaa_user", "domain_name", "name"]],
306 ["state", "present", ["aaa_user", "domain_name", "name"]],
307 ],
308 )
309
310 aaa_user = module.params.get("aaa_user")
311 aaa_user_type = module.params.get("aaa_user_type")
312 domain_name = module.params.get("domain_name")
313 name = module.params.get("name")
314 privilege_type = module.params.get("privilege_type")
315 name_alias = module.params.get("name_alias")
316 state = module.params.get("state")
317
318 if privilege_type is not None:
319 privilege_type = PRIV_TYPE_MAPPING[privilege_type]
320
321 aci = ACIModule(module)
322 aci.construct_url(
323 root_class=dict(
324 aci_class=ACI_MAPPING.get(aaa_user_type).get("aci_class"),
325 aci_rn="{0}{1}".format(ACI_MAPPING.get(aaa_user_type).get("aci_mo"), aaa_user),
326 module_object=aaa_user,
327 target_filter={"name": aaa_user},
328 ),
329 subclass_1=dict(
330 aci_class="aaaUserDomain",
331 aci_rn="userdomain-{0}".format(domain_name),
332 module_object=domain_name,
333 target_filter={"name": domain_name},
334 ),
335 subclass_2=dict(
336 aci_class="aaaUserRole",
337 aci_rn="role-{0}".format(name),
338 module_object=name,
339 target_filter={"name": name},
340 ),
341 )
342
343 aci.get_existing()
344
345 if state == "present":
346 aci.payload(
347 aci_class="aaaUserRole",
348 class_config=dict(
349 name=name,
350 privType=privilege_type,
351 nameAlias=name_alias,
352 ),
353 )
354
355 aci.get_diff(aci_class="aaaUserRole")
356
357 aci.post_config()
358
359 elif state == "absent":
360 aci.delete_config()
361
362 aci.exit_json()
363
364
365 if __name__ == "__main__":
366 main()