| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2023, Christian Kolrep <[email protected]> |
| 5 |
# Copyright: (c) 2024, Akini Ross <[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_epg_useg_attribute_simple_statement |
| 17 |
short_description: Manage EPG useg Attributes Simple Statements (fv:DnsAttr, fv:IdGroupAttr, fv:IpAttr, fv:MacAttr, and fv:VmAttr) |
| 18 |
description: |
| 19 |
- Manage EPG useg Attributes Simple Statements |
| 20 |
options: |
| 21 |
tenant: |
| 22 |
description: |
| 23 |
- The name of an existing tenant. |
| 24 |
type: str |
| 25 |
aliases: [ tenant_name ] |
| 26 |
ap: |
| 27 |
description: |
| 28 |
- The name of an existing application network profile. |
| 29 |
type: str |
| 30 |
aliases: [ app_profile, app_profile_name ] |
| 31 |
epg: |
| 32 |
description: |
| 33 |
- The name of an existing end point group. |
| 34 |
type: str |
| 35 |
aliases: [ epg_name ] |
| 36 |
parent_block_statements: |
| 37 |
description: |
| 38 |
- The list of parent block statements. |
| 39 |
- The order of the provided list matters, assuming the list ["A", "B", "C"]. |
| 40 |
- The block statement "A" will be the parent of "B" |
| 41 |
- The block statement "A" will be a child of the default block statement. |
| 42 |
- The maximum amount of parent block statements is 3. |
| 43 |
type: list |
| 44 |
elements: str |
| 45 |
aliases: [ blocks, parent_blocks ] |
| 46 |
name: |
| 47 |
description: |
| 48 |
- The name of the EPG useg attribute. |
| 49 |
type: str |
| 50 |
aliases: [ useg_attribute_name ] |
| 51 |
type: |
| 52 |
description: |
| 53 |
- The type of the EPG useg attribute |
| 54 |
type: str |
| 55 |
required: true |
| 56 |
choices: |
| 57 |
- ip |
| 58 |
- mac |
| 59 |
- dns |
| 60 |
- ad_group |
| 61 |
- vm_custom_attr |
| 62 |
- vm_vmm_domain |
| 63 |
- vm_operating_system |
| 64 |
- vm_hypervisor_id |
| 65 |
- vm_datacenter |
| 66 |
- vm_id |
| 67 |
- vm_name |
| 68 |
- vm_folder |
| 69 |
- vm_folder_path |
| 70 |
- vm_vnic |
| 71 |
- vm_tag |
| 72 |
aliases: [ useg_attribute_type ] |
| 73 |
operator: |
| 74 |
description: |
| 75 |
- The operator of the EPG useg attribute. |
| 76 |
type: str |
| 77 |
choices: [ equals, contains, starts_with, ends_with ] |
| 78 |
category: |
| 79 |
description: |
| 80 |
- The name of the vmware tag category or vmware custom attribute. |
| 81 |
type: str |
| 82 |
aliases: [ custom_attribute ] |
| 83 |
use_subnet: |
| 84 |
description: |
| 85 |
- Whether to use the EPG subnet definition for ip. |
| 86 |
type: bool |
| 87 |
value: |
| 88 |
description: |
| 89 |
- The value of the EPG useg attribute. |
| 90 |
type: str |
| 91 |
state: |
| 92 |
description: |
| 93 |
- Use C(present) or C(absent) for adding or removing. |
| 94 |
- Use C(query) for listing an object or multiple objects. |
| 95 |
type: str |
| 96 |
choices: [ absent, present, query ] |
| 97 |
default: present |
| 98 |
extends_documentation_fragment: |
| 99 |
- cisco.aci.aci |
| 100 |
- cisco.aci.annotation |
| 101 |
|
| 102 |
notes: |
| 103 |
- The I(tenant), I(ap) and I(epg) used must exist before using this module in your playbook. |
| 104 |
The M(cisco.aci.aci_tenant), M(cisco.aci.aci_ap) and M(cisco.aci.aci_epg) modules can be used for this. |
| 105 |
seealso: |
| 106 |
- module: cisco.aci.aci_tenant |
| 107 |
- module: cisco.aci.aci_ap |
| 108 |
- module: cisco.aci.aci_epg |
| 109 |
- name: APIC Management Information Model reference |
| 110 |
description: More information about the internal APIC class B(fv:DnsAttr), B(fv:IdGroupAttr), B(fv:IpAttr), B(fv:MacAttr), and B(fv:VmAttr). |
| 111 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 112 |
author: |
| 113 |
- Christian Kolrep (@Christian-Kolrep) |
| 114 |
- Akini Ross (@akinross) |
| 115 |
""" |
| 116 |
|
| 117 |
EXAMPLES = r""" |
| 118 |
- name: Add a new vmtag useg attribute in default block statement |
| 119 |
cisco.aci.aci_epg_useg_attribute_simple_statement: |
| 120 |
host: apic |
| 121 |
username: admin |
| 122 |
password: SomeSecretPassword |
| 123 |
tenant: anstest |
| 124 |
ap: anstest |
| 125 |
epg: anstest |
| 126 |
name: vmtagprod |
| 127 |
type: vmtag |
| 128 |
category: Environment |
| 129 |
operator: equals |
| 130 |
value: Production |
| 131 |
state: present |
| 132 |
delegate_to: localhost |
| 133 |
|
| 134 |
- name: Add a new vmtag useg attribute in nested block statement |
| 135 |
cisco.aci.aci_epg_useg_attribute_simple_statement: |
| 136 |
host: apic |
| 137 |
username: admin |
| 138 |
password: SomeSecretPassword |
| 139 |
tenant: anstest |
| 140 |
ap: anstest |
| 141 |
epg: anstest |
| 142 |
name: vmtagprod |
| 143 |
parent_block_statements: |
| 144 |
- block_a |
| 145 |
- block_b |
| 146 |
type: vmtag |
| 147 |
category: Environment |
| 148 |
operator: equals |
| 149 |
value: Production |
| 150 |
state: present |
| 151 |
delegate_to: localhost |
| 152 |
|
| 153 |
- name: Query a specific vmtag useg attribute in default block statement |
| 154 |
cisco.aci.aci_epg_useg_attribute_simple_statement: |
| 155 |
host: apic |
| 156 |
username: admin |
| 157 |
password: SomeSecretPassword |
| 158 |
tenant: anstest |
| 159 |
ap: anstest |
| 160 |
epg: anstest |
| 161 |
name: vmtagprod |
| 162 |
type: vmtag |
| 163 |
state: query |
| 164 |
delegate_to: localhost |
| 165 |
register: query_result |
| 166 |
|
| 167 |
- name: Query all vmtag useg attributes |
| 168 |
cisco.aci.aci_epg_useg_attribute_simple_statement: |
| 169 |
host: apic |
| 170 |
username: admin |
| 171 |
password: SomeSecretPassword |
| 172 |
state: query |
| 173 |
type: vmtag |
| 174 |
delegate_to: localhost |
| 175 |
register: query_result |
| 176 |
|
| 177 |
- name: Remove an existing vmtag useg attribute from default block statement |
| 178 |
cisco.aci.aci_epg_useg_attribute_simple_statement: |
| 179 |
host: apic |
| 180 |
username: admin |
| 181 |
password: SomeSecretPassword |
| 182 |
tenant: anstest |
| 183 |
ap: anstest |
| 184 |
epg: anstest |
| 185 |
name: vmtagprod |
| 186 |
type: vmtag |
| 187 |
state: absent |
| 188 |
delegate_to: localhost |
| 189 |
""" |
| 190 |
|
| 191 |
RETURN = r""" |
| 192 |
current: |
| 193 |
description: The existing configuration from the APIC after the module has finished |
| 194 |
returned: success |
| 195 |
type: list |
| 196 |
sample: |
| 197 |
[ |
| 198 |
{ |
| 199 |
"fvTenant": { |
| 200 |
"attributes": { |
| 201 |
"descr": "Production environment", |
| 202 |
"dn": "uni/tn-production", |
| 203 |
"name": "production", |
| 204 |
"nameAlias": "", |
| 205 |
"ownerKey": "", |
| 206 |
"ownerTag": "" |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
] |
| 211 |
error: |
| 212 |
description: The error information as returned from the APIC |
| 213 |
returned: failure |
| 214 |
type: dict |
| 215 |
sample: |
| 216 |
{ |
| 217 |
"code": "122", |
| 218 |
"text": "unknown managed object class foo" |
| 219 |
} |
| 220 |
raw: |
| 221 |
description: The raw output returned by the APIC REST API (xml or json) |
| 222 |
returned: parse error |
| 223 |
type: str |
| 224 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 225 |
sent: |
| 226 |
description: The actual/minimal configuration pushed to the APIC |
| 227 |
returned: info |
| 228 |
type: list |
| 229 |
sample: |
| 230 |
{ |
| 231 |
"fvTenant": { |
| 232 |
"attributes": { |
| 233 |
"descr": "Production environment" |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
previous: |
| 238 |
description: The original configuration from the APIC before the module has started |
| 239 |
returned: info |
| 240 |
type: list |
| 241 |
sample: |
| 242 |
[ |
| 243 |
{ |
| 244 |
"fvTenant": { |
| 245 |
"attributes": { |
| 246 |
"descr": "Production", |
| 247 |
"dn": "uni/tn-production", |
| 248 |
"name": "production", |
| 249 |
"nameAlias": "", |
| 250 |
"ownerKey": "", |
| 251 |
"ownerTag": "" |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
] |
| 256 |
proposed: |
| 257 |
description: The assembled configuration from the user-provided parameters |
| 258 |
returned: info |
| 259 |
type: dict |
| 260 |
sample: |
| 261 |
{ |
| 262 |
"fvTenant": { |
| 263 |
"attributes": { |
| 264 |
"descr": "Production environment", |
| 265 |
"name": "production" |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
filter_string: |
| 270 |
description: The filter string used for the request |
| 271 |
returned: failure or debug |
| 272 |
type: str |
| 273 |
sample: ?rsp-prop-include=config-only |
| 274 |
method: |
| 275 |
description: The HTTP method used for the request to the APIC |
| 276 |
returned: failure or debug |
| 277 |
type: str |
| 278 |
sample: POST |
| 279 |
response: |
| 280 |
description: The HTTP response from the APIC |
| 281 |
returned: failure or debug |
| 282 |
type: str |
| 283 |
sample: OK (30 bytes) |
| 284 |
status: |
| 285 |
description: The HTTP status from the APIC |
| 286 |
returned: failure or debug |
| 287 |
type: int |
| 288 |
sample: 200 |
| 289 |
url: |
| 290 |
description: The HTTP url used for the request to the APIC |
| 291 |
returned: failure or debug |
| 292 |
type: str |
| 293 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 294 |
""" |
| 295 |
|
| 296 |
from ansible.module_utils.basic import AnsibleModule |
| 297 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 298 |
from ansible_collections.cisco.aci.plugins.module_utils.constants import USEG_ATTRIBUTE_MAPPING, OPERATOR_MAPPING |
| 299 |
|
| 300 |
|
| 301 |
def main(): |
| 302 |
argument_spec = aci_argument_spec() |
| 303 |
argument_spec.update(aci_annotation_spec()) |
| 304 |
argument_spec.update( |
| 305 |
ap=dict(type="str", aliases=["app_profile", "app_profile_name"]), # Not required for querying all objects |
| 306 |
epg=dict(type="str", aliases=["epg_name"]), # Not required for querying all objects |
| 307 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 308 |
tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects |
| 309 |
parent_block_statements=dict(type="list", elements="str", aliases=["parent_blocks", "blocks"]), |
| 310 |
name=dict(type="str", aliases=["useg_attribute_name"]), |
| 311 |
type=dict(type="str", required=True, choices=list(USEG_ATTRIBUTE_MAPPING.keys()), aliases=["useg_attribute_type"]), |
| 312 |
operator=dict(type="str", choices=list(OPERATOR_MAPPING.keys())), |
| 313 |
category=dict(type="str", aliases=["custom_attribute"]), |
| 314 |
value=dict(type="str"), |
| 315 |
use_subnet=dict(type="bool"), |
| 316 |
) |
| 317 |
|
| 318 |
module = AnsibleModule( |
| 319 |
argument_spec=argument_spec, |
| 320 |
supports_check_mode=True, |
| 321 |
required_if=[ |
| 322 |
["state", "absent", ["ap", "epg", "tenant", "name"]], |
| 323 |
["state", "present", ["ap", "epg", "tenant", "name"]], |
| 324 |
], |
| 325 |
) |
| 326 |
|
| 327 |
aci = ACIModule(module) |
| 328 |
|
| 329 |
ap = module.params.get("ap") |
| 330 |
epg = module.params.get("epg") |
| 331 |
state = module.params.get("state") |
| 332 |
tenant = module.params.get("tenant") |
| 333 |
blocks = module.params.get("parent_block_statements") |
| 334 |
name = module.params.get("name") |
| 335 |
attribute_type = module.params.get("type") |
| 336 |
value = module.params.get("value") |
| 337 |
operator = module.params.get("operator") |
| 338 |
category = module.params.get("category") |
| 339 |
use_subnet = aci.boolean(module.params.get("use_subnet")) |
| 340 |
|
| 341 |
# Excluding below classes from the module: |
| 342 |
# fvProtoAttr: |
| 343 |
# Was used in AVS, but it is not longer in use. |
| 344 |
# fvUsegBDCont: |
| 345 |
# Was part of a feature that allowed uSeg attributes to be applied at VRF (instead of BD) level. |
| 346 |
# It has been since deprecated and we no longer allow setting the scope at fvCtrn to scope-vrf. |
| 347 |
# This type of functionality has been replaced by the ESG feature. |
| 348 |
attribute_class = USEG_ATTRIBUTE_MAPPING[attribute_type]["attribute_class"] |
| 349 |
attribute_rn = USEG_ATTRIBUTE_MAPPING[attribute_type]["rn_format"].format(name) |
| 350 |
attribute_type = USEG_ATTRIBUTE_MAPPING[attribute_type]["attribute_type"] |
| 351 |
|
| 352 |
if blocks: |
| 353 |
if len(blocks) > 3: |
| 354 |
module.fail_json(msg="{0} block statements are provided but the maximum amount of parent_block_statements is 3".format(len(blocks))) |
| 355 |
parent_blocks_class = "fvSCrtrn" |
| 356 |
parent_blocks_rn = "crtrn/crtrn-{0}".format("/crtrn-".join(blocks)) |
| 357 |
parent_blocks_name = blocks[-1] |
| 358 |
else: |
| 359 |
parent_blocks_class = "fvCrtrn" |
| 360 |
parent_blocks_rn = "crtrn" |
| 361 |
parent_blocks_name = "default" |
| 362 |
|
| 363 |
aci.construct_url( |
| 364 |
root_class=dict( |
| 365 |
aci_class="fvTenant", |
| 366 |
aci_rn="tn-{0}".format(tenant), |
| 367 |
module_object=tenant, |
| 368 |
target_filter={"name": tenant}, |
| 369 |
), |
| 370 |
subclass_1=dict( |
| 371 |
aci_class="fvAp", |
| 372 |
aci_rn="ap-{0}".format(ap), |
| 373 |
module_object=ap, |
| 374 |
target_filter={"name": ap}, |
| 375 |
), |
| 376 |
subclass_2=dict( |
| 377 |
aci_class="fvAEPg", |
| 378 |
aci_rn="epg-{0}".format(epg), |
| 379 |
module_object=epg, |
| 380 |
target_filter={"name": epg}, |
| 381 |
), |
| 382 |
subclass_3=dict( |
| 383 |
aci_class=parent_blocks_class, |
| 384 |
aci_rn=parent_blocks_rn, |
| 385 |
module_object=parent_blocks_name, |
| 386 |
target_filter={"name": parent_blocks_name}, |
| 387 |
), |
| 388 |
subclass_4=dict( |
| 389 |
aci_class=attribute_class, |
| 390 |
aci_rn=attribute_rn, |
| 391 |
module_object=name, |
| 392 |
target_filter={"name": name}, |
| 393 |
), |
| 394 |
) |
| 395 |
|
| 396 |
aci.get_existing() |
| 397 |
|
| 398 |
if state == "present": |
| 399 |
class_config = dict(name=name) |
| 400 |
|
| 401 |
if attribute_class == "fvVmAttr": |
| 402 |
class_config.update(type=attribute_type) |
| 403 |
class_config.update(operator=OPERATOR_MAPPING.get(operator)) |
| 404 |
class_config.update(value=value) |
| 405 |
if attribute_type == "tag": |
| 406 |
class_config.update(category=category) |
| 407 |
elif attribute_type == "custom-label": |
| 408 |
class_config.update(labelName=category) |
| 409 |
|
| 410 |
elif attribute_class == "fvIpAttr": |
| 411 |
class_config.update(usefvSubnet=use_subnet) |
| 412 |
class_config.update(ip=value) |
| 413 |
|
| 414 |
elif attribute_class == "fvMacAttr": |
| 415 |
class_config.update(mac=value.upper()) |
| 416 |
|
| 417 |
elif attribute_class == "fvDnsAttr": |
| 418 |
class_config.update(filter=value) |
| 419 |
|
| 420 |
elif attribute_class == "fvIdGroupAttr": |
| 421 |
class_config.update(selector=value) |
| 422 |
|
| 423 |
aci.payload(aci_class=attribute_class, class_config=class_config) |
| 424 |
|
| 425 |
aci.get_diff(aci_class=attribute_class) |
| 426 |
|
| 427 |
aci.post_config() |
| 428 |
|
| 429 |
elif state == "absent": |
| 430 |
aci.delete_config() |
| 431 |
|
| 432 |
aci.exit_json() |
| 433 |
|
| 434 |
|
| 435 |
if __name__ == "__main__": |
| 436 |
main() |