| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, Akini Ross <[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_epg_useg_attribute_block_statement |
| 16 |
short_description: Manage EPG useg Attributes Block Statements (fv:SCrtrn) |
| 17 |
description: |
| 18 |
- Manage EPG useg Attributes Block Statements |
| 19 |
options: |
| 20 |
tenant: |
| 21 |
description: |
| 22 |
- The name of an existing tenant. |
| 23 |
type: str |
| 24 |
aliases: [ tenant_name ] |
| 25 |
ap: |
| 26 |
description: |
| 27 |
- The name of an existing application network profile. |
| 28 |
type: str |
| 29 |
aliases: [ app_profile, app_profile_name ] |
| 30 |
epg: |
| 31 |
description: |
| 32 |
- The name of an existing end point group. |
| 33 |
type: str |
| 34 |
aliases: [ epg_name ] |
| 35 |
parent_block_statements: |
| 36 |
description: |
| 37 |
- The list of parent block statements. |
| 38 |
- The order of the provided list matters, assuming the list ["A", "B"]. |
| 39 |
- The block statement "A" will be the parent of "B" |
| 40 |
- The block statement "A" will be a child of the default block statement. |
| 41 |
- The maximum amount of parent block statements is 2. |
| 42 |
type: list |
| 43 |
elements: str |
| 44 |
aliases: [ blocks, parent_blocks ] |
| 45 |
name: |
| 46 |
description: |
| 47 |
- The name of the block statement. |
| 48 |
type: str |
| 49 |
aliases: [ block_statement, block_statement_name ] |
| 50 |
match: |
| 51 |
description: |
| 52 |
- The match type of the Block Statement. |
| 53 |
- The APIC defaults to C(any) when unset during creation. |
| 54 |
type: str |
| 55 |
choices: [ any, all ] |
| 56 |
state: |
| 57 |
description: |
| 58 |
- Use C(present) or C(absent) for adding or removing. |
| 59 |
- Use C(query) for listing an object or multiple objects. |
| 60 |
type: str |
| 61 |
choices: [ absent, present, query ] |
| 62 |
default: present |
| 63 |
extends_documentation_fragment: |
| 64 |
- cisco.aci.aci |
| 65 |
- cisco.aci.annotation |
| 66 |
|
| 67 |
notes: |
| 68 |
- The I(tenant), I(ap) and I(epg) used must exist before using this module in your playbook. |
| 69 |
The M(cisco.aci.aci_tenant), M(cisco.aci.aci_ap) and M(cisco.aci.aci_epg) modules can be used for this. |
| 70 |
seealso: |
| 71 |
- module: cisco.aci.aci_tenant |
| 72 |
- module: cisco.aci.aci_ap |
| 73 |
- module: cisco.aci.aci_epg |
| 74 |
- name: APIC Management Information Model reference |
| 75 |
description: More information about the internal APIC class B(fv:SCrtrn). |
| 76 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 77 |
author: |
| 78 |
- Akini Ross (@akinross)) |
| 79 |
""" |
| 80 |
|
| 81 |
EXAMPLES = r""" |
| 82 |
- name: Add a new block statement |
| 83 |
cisco.aci.aci_epg_useg_attribute_block_statement: |
| 84 |
host: apic |
| 85 |
username: admin |
| 86 |
password: SomeSecretPassword |
| 87 |
tenant: anstest |
| 88 |
ap: anstest |
| 89 |
epg: anstest |
| 90 |
name: block_a |
| 91 |
state: present |
| 92 |
delegate_to: localhost |
| 93 |
|
| 94 |
- name: Add a new nested block statement |
| 95 |
cisco.aci.aci_epg_useg_attribute_block_statement: |
| 96 |
host: apic |
| 97 |
username: admin |
| 98 |
password: SomeSecretPassword |
| 99 |
tenant: anstest |
| 100 |
ap: anstest |
| 101 |
epg: anstest |
| 102 |
parent_block_statements: |
| 103 |
- block_a |
| 104 |
- block_b |
| 105 |
name: block_c |
| 106 |
match: any |
| 107 |
state: present |
| 108 |
delegate_to: localhost |
| 109 |
|
| 110 |
- name: Query a block statement |
| 111 |
cisco.aci.aci_epg_useg_attribute_block_statement: |
| 112 |
host: apic |
| 113 |
username: admin |
| 114 |
password: SomeSecretPassword |
| 115 |
tenant: anstest |
| 116 |
ap: anstest |
| 117 |
epg: anstest |
| 118 |
name: block_a |
| 119 |
state: query |
| 120 |
delegate_to: localhost |
| 121 |
register: query_result |
| 122 |
|
| 123 |
- name: Query all block statements |
| 124 |
cisco.aci.aci_epg_useg_attribute_block_statement: |
| 125 |
host: apic |
| 126 |
username: admin |
| 127 |
password: SomeSecretPassword |
| 128 |
state: query |
| 129 |
delegate_to: localhost |
| 130 |
register: query_result |
| 131 |
|
| 132 |
- name: Remove an existing block statement |
| 133 |
cisco.aci.aci_epg_useg_attribute_block_statement: |
| 134 |
host: apic |
| 135 |
username: admin |
| 136 |
password: SomeSecretPassword |
| 137 |
tenant: anstest |
| 138 |
ap: anstest |
| 139 |
epg: anstest |
| 140 |
name: block_a |
| 141 |
state: absent |
| 142 |
delegate_to: localhost |
| 143 |
""" |
| 144 |
|
| 145 |
RETURN = r""" |
| 146 |
current: |
| 147 |
description: The existing configuration from the APIC after the module has finished |
| 148 |
returned: success |
| 149 |
type: list |
| 150 |
sample: |
| 151 |
[ |
| 152 |
{ |
| 153 |
"fvTenant": { |
| 154 |
"attributes": { |
| 155 |
"descr": "Production environment", |
| 156 |
"dn": "uni/tn-production", |
| 157 |
"name": "production", |
| 158 |
"nameAlias": "", |
| 159 |
"ownerKey": "", |
| 160 |
"ownerTag": "" |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
] |
| 165 |
error: |
| 166 |
description: The error information as returned from the APIC |
| 167 |
returned: failure |
| 168 |
type: dict |
| 169 |
sample: |
| 170 |
{ |
| 171 |
"code": "122", |
| 172 |
"text": "unknown managed object class foo" |
| 173 |
} |
| 174 |
raw: |
| 175 |
description: The raw output returned by the APIC REST API (xml or json) |
| 176 |
returned: parse error |
| 177 |
type: str |
| 178 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 179 |
sent: |
| 180 |
description: The actual/minimal configuration pushed to the APIC |
| 181 |
returned: info |
| 182 |
type: list |
| 183 |
sample: |
| 184 |
{ |
| 185 |
"fvTenant": { |
| 186 |
"attributes": { |
| 187 |
"descr": "Production environment" |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
previous: |
| 192 |
description: The original configuration from the APIC before the module has started |
| 193 |
returned: info |
| 194 |
type: list |
| 195 |
sample: |
| 196 |
[ |
| 197 |
{ |
| 198 |
"fvTenant": { |
| 199 |
"attributes": { |
| 200 |
"descr": "Production", |
| 201 |
"dn": "uni/tn-production", |
| 202 |
"name": "production", |
| 203 |
"nameAlias": "", |
| 204 |
"ownerKey": "", |
| 205 |
"ownerTag": "" |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
] |
| 210 |
proposed: |
| 211 |
description: The assembled configuration from the user-provided parameters |
| 212 |
returned: info |
| 213 |
type: dict |
| 214 |
sample: |
| 215 |
{ |
| 216 |
"fvTenant": { |
| 217 |
"attributes": { |
| 218 |
"descr": "Production environment", |
| 219 |
"name": "production" |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
filter_string: |
| 224 |
description: The filter string used for the request |
| 225 |
returned: failure or debug |
| 226 |
type: str |
| 227 |
sample: ?rsp-prop-include=config-only |
| 228 |
method: |
| 229 |
description: The HTTP method used for the request to the APIC |
| 230 |
returned: failure or debug |
| 231 |
type: str |
| 232 |
sample: POST |
| 233 |
response: |
| 234 |
description: The HTTP response from the APIC |
| 235 |
returned: failure or debug |
| 236 |
type: str |
| 237 |
sample: OK (30 bytes) |
| 238 |
status: |
| 239 |
description: The HTTP status from the APIC |
| 240 |
returned: failure or debug |
| 241 |
type: int |
| 242 |
sample: 200 |
| 243 |
url: |
| 244 |
description: The HTTP url used for the request to the APIC |
| 245 |
returned: failure or debug |
| 246 |
type: str |
| 247 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 248 |
""" |
| 249 |
|
| 250 |
from ansible.module_utils.basic import AnsibleModule |
| 251 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 252 |
|
| 253 |
|
| 254 |
def main(): |
| 255 |
argument_spec = aci_argument_spec() |
| 256 |
argument_spec.update(aci_annotation_spec()) |
| 257 |
argument_spec.update( |
| 258 |
ap=dict(type="str", aliases=["app_profile", "app_profile_name"]), # Not required for querying all objects |
| 259 |
epg=dict(type="str", aliases=["epg_name"]), # Not required for querying all objects |
| 260 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 261 |
tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects |
| 262 |
parent_block_statements=dict(type="list", elements="str", aliases=["parent_blocks", "blocks"]), |
| 263 |
name=dict(type="str", aliases=["block_statement", "block_statement_name"]), |
| 264 |
match=dict(type="str", choices=["any", "all"]), |
| 265 |
) |
| 266 |
|
| 267 |
module = AnsibleModule( |
| 268 |
argument_spec=argument_spec, |
| 269 |
supports_check_mode=True, |
| 270 |
required_if=[ |
| 271 |
["state", "absent", ["ap", "epg", "tenant", "name"]], |
| 272 |
["state", "present", ["ap", "epg", "tenant", "name"]], |
| 273 |
], |
| 274 |
) |
| 275 |
|
| 276 |
aci = ACIModule(module) |
| 277 |
|
| 278 |
ap = module.params.get("ap") |
| 279 |
epg = module.params.get("epg") |
| 280 |
state = module.params.get("state") |
| 281 |
tenant = module.params.get("tenant") |
| 282 |
blocks = module.params.get("parent_block_statements") |
| 283 |
name = module.params.get("name") |
| 284 |
match = module.params.get("match") |
| 285 |
|
| 286 |
block_statement_class = "fvSCrtrn" |
| 287 |
|
| 288 |
if blocks: |
| 289 |
if len(blocks) > 2: |
| 290 |
module.fail_json(msg="{0} block statements are provided but the maximum amount of parent_block_statements is 2".format(len(blocks))) |
| 291 |
parent_blocks_class = block_statement_class |
| 292 |
parent_blocks_rn = "crtrn/crtrn-{0}".format("/crtrn-".join(blocks)) |
| 293 |
parent_blocks_name = blocks[-1] |
| 294 |
else: |
| 295 |
parent_blocks_class = "fvCrtrn" |
| 296 |
parent_blocks_rn = "crtrn" |
| 297 |
parent_blocks_name = "default" |
| 298 |
|
| 299 |
aci.construct_url( |
| 300 |
root_class=dict( |
| 301 |
aci_class="fvTenant", |
| 302 |
aci_rn="tn-{0}".format(tenant), |
| 303 |
module_object=tenant, |
| 304 |
target_filter={"name": tenant}, |
| 305 |
), |
| 306 |
subclass_1=dict( |
| 307 |
aci_class="fvAp", |
| 308 |
aci_rn="ap-{0}".format(ap), |
| 309 |
module_object=ap, |
| 310 |
target_filter={"name": ap}, |
| 311 |
), |
| 312 |
subclass_2=dict( |
| 313 |
aci_class="fvAEPg", |
| 314 |
aci_rn="epg-{0}".format(epg), |
| 315 |
module_object=epg, |
| 316 |
target_filter={"name": epg}, |
| 317 |
), |
| 318 |
subclass_3=dict( |
| 319 |
aci_class=parent_blocks_class, |
| 320 |
aci_rn=parent_blocks_rn, |
| 321 |
module_object=parent_blocks_name, |
| 322 |
target_filter={"name": parent_blocks_name}, |
| 323 |
), |
| 324 |
subclass_4=dict( |
| 325 |
aci_class=block_statement_class, |
| 326 |
aci_rn="crtrn-{0}".format(name), |
| 327 |
module_object=name, |
| 328 |
target_filter={"name": name}, |
| 329 |
), |
| 330 |
) |
| 331 |
|
| 332 |
aci.get_existing() |
| 333 |
|
| 334 |
if state == "present": |
| 335 |
aci.payload(aci_class=block_statement_class, class_config=dict(name=name, match=match)) |
| 336 |
|
| 337 |
aci.get_diff(aci_class=block_statement_class) |
| 338 |
|
| 339 |
aci.post_config() |
| 340 |
|
| 341 |
elif state == "absent": |
| 342 |
aci.delete_config() |
| 343 |
|
| 344 |
aci.exit_json() |
| 345 |
|
| 346 |
|
| 347 |
if __name__ == "__main__": |
| 348 |
main() |