| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2022, Sabari Jaganathan (@sajagana) <[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_role |
| 16 |
short_description: Manage AAA roles (aaa:Role) |
| 17 |
description: |
| 18 |
- Manage AAA roles on Cisco ACI fabrics. |
| 19 |
options: |
| 20 |
name: |
| 21 |
description: |
| 22 |
- The name of the aaa role. |
| 23 |
type: str |
| 24 |
aliases: [ aaa_role ] |
| 25 |
privileges: |
| 26 |
description: |
| 27 |
- The privilege(s) assigned to a role. |
| 28 |
type: list |
| 29 |
aliases: [ priv ] |
| 30 |
elements: str |
| 31 |
choices: [ |
| 32 |
admin, |
| 33 |
aaa, |
| 34 |
tenant-connectivity, |
| 35 |
tenant-protocol, |
| 36 |
vmm-policy, |
| 37 |
tenant-ext-connectivity, |
| 38 |
tenant-ext-protocol, |
| 39 |
tenant-qos, |
| 40 |
tenant-security, |
| 41 |
tenant-network-profile, |
| 42 |
tenant-epg, |
| 43 |
fabric-connectivity, |
| 44 |
fabric-protocol, |
| 45 |
fabric-equipment, |
| 46 |
access-connectivity, |
| 47 |
access-protocol, |
| 48 |
access-equipment, |
| 49 |
access-qos, |
| 50 |
nw-svc-params, |
| 51 |
ops, |
| 52 |
nw-svc-policy, |
| 53 |
site-admin, |
| 54 |
site-policy, |
| 55 |
config-manager, |
| 56 |
custom-privilege-1, |
| 57 |
custom-privilege-2, |
| 58 |
custom-privilege-3, |
| 59 |
custom-privilege-4, |
| 60 |
custom-privilege-5, |
| 61 |
custom-privilege-6, |
| 62 |
custom-privilege-7, |
| 63 |
custom-privilege-8, |
| 64 |
custom-privilege-9, |
| 65 |
custom-privilege-10, |
| 66 |
custom-privilege-11, |
| 67 |
custom-privilege-12, |
| 68 |
custom-privilege-13, |
| 69 |
custom-privilege-14, |
| 70 |
custom-privilege-15, |
| 71 |
custom-privilege-16, |
| 72 |
custom-privilege-17, |
| 73 |
custom-privilege-18, |
| 74 |
custom-privilege-19, |
| 75 |
custom-privilege-20, |
| 76 |
custom-privilege-21, |
| 77 |
custom-privilege-22, |
| 78 |
custom-port-privilege |
| 79 |
] |
| 80 |
description: |
| 81 |
description: |
| 82 |
- Description of the aaa role. |
| 83 |
type: str |
| 84 |
aliases: [ descr ] |
| 85 |
state: |
| 86 |
description: |
| 87 |
- Use C(present) or C(absent) for adding or removing. |
| 88 |
- Use C(query) for listing an object or multiple objects. |
| 89 |
type: str |
| 90 |
choices: [ absent, present, query ] |
| 91 |
default: present |
| 92 |
name_alias: |
| 93 |
description: |
| 94 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 95 |
type: str |
| 96 |
extends_documentation_fragment: |
| 97 |
- cisco.aci.aci |
| 98 |
|
| 99 |
seealso: |
| 100 |
- module: cisco.aci.aci_aaa_domain |
| 101 |
- name: Manage AAA domains (aaa:Domain) |
| 102 |
description: More information about the AAA domains class B(aaa:Domain). |
| 103 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 104 |
author: |
| 105 |
- Sabari Jaganathan (@sajagana) |
| 106 |
""" |
| 107 |
|
| 108 |
|
| 109 |
EXAMPLES = r""" |
| 110 |
- name: Add a aaa role |
| 111 |
cisco.aci.aci_aaa_role: |
| 112 |
host: apic |
| 113 |
username: admin |
| 114 |
password: SomeSecretPassword |
| 115 |
name: anstest |
| 116 |
privileges: aaa |
| 117 |
state: present |
| 118 |
delegate_to: localhost |
| 119 |
|
| 120 |
- name: Add list of aaa roles |
| 121 |
cisco.aci.aci_aaa_role: |
| 122 |
host: apic |
| 123 |
username: admin |
| 124 |
password: SomeSecretPassword |
| 125 |
name: "{{ item.name }}" |
| 126 |
privileges: "{{ item.privilege }}" |
| 127 |
state: present |
| 128 |
delegate_to: localhost |
| 129 |
with_items: |
| 130 |
- name: anstest1 |
| 131 |
privilege: site-admin |
| 132 |
- name: anstest2 |
| 133 |
privilege: site-policy |
| 134 |
|
| 135 |
- name: Query a aaa role with name |
| 136 |
cisco.aci.aci_aaa_role: |
| 137 |
host: apic |
| 138 |
username: admin |
| 139 |
password: SomeSecretPassword |
| 140 |
name: anstest |
| 141 |
state: query |
| 142 |
delegate_to: localhost |
| 143 |
|
| 144 |
- name: Query all aaa roles |
| 145 |
cisco.aci.aci_aaa_role: |
| 146 |
host: apic |
| 147 |
username: admin |
| 148 |
password: SomeSecretPassword |
| 149 |
state: query |
| 150 |
delegate_to: localhost |
| 151 |
|
| 152 |
- name: Remove a aaa role with name |
| 153 |
cisco.aci.aci_aaa_role: |
| 154 |
host: apic |
| 155 |
username: admin |
| 156 |
password: SomeSecretPassword |
| 157 |
name: anstest |
| 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 |
| 269 |
|
| 270 |
PRIVILEGES = [ |
| 271 |
"admin", |
| 272 |
"aaa", |
| 273 |
"tenant-connectivity", |
| 274 |
"tenant-protocol", |
| 275 |
"vmm-policy", |
| 276 |
"tenant-ext-connectivity", |
| 277 |
"tenant-ext-protocol", |
| 278 |
"tenant-qos", |
| 279 |
"tenant-security", |
| 280 |
"tenant-network-profile", |
| 281 |
"tenant-epg", |
| 282 |
"fabric-connectivity", |
| 283 |
"fabric-protocol", |
| 284 |
"fabric-equipment", |
| 285 |
"access-connectivity", |
| 286 |
"access-protocol", |
| 287 |
"access-equipment", |
| 288 |
"access-qos", |
| 289 |
"nw-svc-params", |
| 290 |
"ops", |
| 291 |
"nw-svc-policy", |
| 292 |
"site-admin", |
| 293 |
"site-policy", |
| 294 |
"config-manager", |
| 295 |
"custom-privilege-1", |
| 296 |
"custom-privilege-2", |
| 297 |
"custom-privilege-3", |
| 298 |
"custom-privilege-4", |
| 299 |
"custom-privilege-5", |
| 300 |
"custom-privilege-6", |
| 301 |
"custom-privilege-7", |
| 302 |
"custom-privilege-8", |
| 303 |
"custom-privilege-9", |
| 304 |
"custom-privilege-10", |
| 305 |
"custom-privilege-11", |
| 306 |
"custom-privilege-12", |
| 307 |
"custom-privilege-13", |
| 308 |
"custom-privilege-14", |
| 309 |
"custom-privilege-15", |
| 310 |
"custom-privilege-16", |
| 311 |
"custom-privilege-17", |
| 312 |
"custom-privilege-18", |
| 313 |
"custom-privilege-19", |
| 314 |
"custom-privilege-20", |
| 315 |
"custom-privilege-21", |
| 316 |
"custom-privilege-22", |
| 317 |
"custom-port-privilege", |
| 318 |
] |
| 319 |
|
| 320 |
|
| 321 |
def main(): |
| 322 |
argument_spec = aci_argument_spec() |
| 323 |
argument_spec.update( |
| 324 |
name=dict(type="str", aliases=["aaa_role"]), |
| 325 |
privileges=dict(type="list", aliases=["priv"], elements="str", choices=PRIVILEGES), |
| 326 |
description=dict(type="str", aliases=["descr"]), |
| 327 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 328 |
name_alias=dict(type="str"), |
| 329 |
) |
| 330 |
|
| 331 |
module = AnsibleModule( |
| 332 |
argument_spec=argument_spec, |
| 333 |
supports_check_mode=True, |
| 334 |
required_if=[ |
| 335 |
["state", "absent", ["name"]], |
| 336 |
["state", "present", ["name", "privileges"]], |
| 337 |
], |
| 338 |
) |
| 339 |
|
| 340 |
name = module.params.get("name") |
| 341 |
description = module.params.get("description") |
| 342 |
privileges = module.params.get("privileges") |
| 343 |
state = module.params.get("state") |
| 344 |
name_alias = module.params.get("name_alias") |
| 345 |
|
| 346 |
aci = ACIModule(module) |
| 347 |
aci.construct_url( |
| 348 |
root_class=dict( |
| 349 |
aci_class="aaaRole", |
| 350 |
aci_rn="userext/role-{0}".format(name), |
| 351 |
module_object=name, |
| 352 |
target_filter=dict(name=name), |
| 353 |
), |
| 354 |
) |
| 355 |
aci.get_existing() |
| 356 |
|
| 357 |
if state == "present": |
| 358 |
formatted_privileges = ",".join(privileges) |
| 359 |
aci.payload( |
| 360 |
aci_class="aaaRole", |
| 361 |
class_config=dict( |
| 362 |
name=name, |
| 363 |
priv=formatted_privileges, |
| 364 |
descr=description, |
| 365 |
nameAlias=name_alias, |
| 366 |
), |
| 367 |
) |
| 368 |
|
| 369 |
aci.get_diff(aci_class="aaaRole") |
| 370 |
|
| 371 |
aci.post_config() |
| 372 |
|
| 373 |
elif state == "absent": |
| 374 |
aci.delete_config() |
| 375 |
|
| 376 |
aci.exit_json() |
| 377 |
|
| 378 |
|
| 379 |
if __name__ == "__main__": |
| 380 |
main() |