| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2022, Tim Cragg (@timcragg) |
| 5 |
# Copyright: (c) 2023, Akini Ross (@akinross) |
| 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": "community"} |
| 13 |
|
| 14 |
DOCUMENTATION = r""" |
| 15 |
--- |
| 16 |
module: aci_dhcp_relay_provider |
| 17 |
short_description: Manage DHCP relay policy providers (dhcp:RsProv) |
| 18 |
description: |
| 19 |
- Manage DHCP relay policy providers configuration on Cisco ACI fabrics. |
| 20 |
options: |
| 21 |
tenant: |
| 22 |
description: |
| 23 |
- Name of the tenant the relay_policy is in. |
| 24 |
- When tenant is not provided the module will be applied to the global (infra) policy. |
| 25 |
type: str |
| 26 |
relay_policy: |
| 27 |
description: |
| 28 |
- Name of an existing DHCP relay policy |
| 29 |
type: str |
| 30 |
aliases: [ relay_policy_name ] |
| 31 |
provider_tenant: |
| 32 |
description: |
| 33 |
- Name of the tenant the epg or external_epg is in |
| 34 |
- Required when epg or external_epg is in a different tenant than the relay_policy |
| 35 |
- Required when global (infra) relay_policy is configured with epg or external_epg types |
| 36 |
type: str |
| 37 |
epg_type: |
| 38 |
description: |
| 39 |
- Type of EPG the DHCP server is in |
| 40 |
type: str |
| 41 |
choices: [ epg, l2_external, l3_external, dn ] |
| 42 |
anp: |
| 43 |
description: |
| 44 |
- Application Profile the EPG is in. |
| 45 |
- Only used when epg_type is app_epg. |
| 46 |
type: str |
| 47 |
epg: |
| 48 |
description: |
| 49 |
- Name of the Application EPG the DHCP server is in. |
| 50 |
- Only used when epg_type is epg |
| 51 |
type: str |
| 52 |
aliases: [ app_epg ] |
| 53 |
l2out_name: |
| 54 |
description: |
| 55 |
- Name of the L2out the DHCP server is in. |
| 56 |
- Only used when epg_type is l2_external |
| 57 |
type: str |
| 58 |
l3out_name: |
| 59 |
description: |
| 60 |
- Name of the L3out the DHCP server is in. |
| 61 |
- Only used when epg_type is l3_external. |
| 62 |
type: str |
| 63 |
external_epg: |
| 64 |
description: |
| 65 |
- Name of the external network object the DHCP server is in. |
| 66 |
- Only used when epg_type is l2_external or l3_external. |
| 67 |
type: str |
| 68 |
aliases: [ external_net ] |
| 69 |
dn: |
| 70 |
description: |
| 71 |
- dn of the EPG the DHCP server is in |
| 72 |
- Only used when epg_type is dn |
| 73 |
type: str |
| 74 |
dhcp_server_addr: |
| 75 |
description: |
| 76 |
- DHCP server address |
| 77 |
type: str |
| 78 |
state: |
| 79 |
description: |
| 80 |
- Use C(present) or C(absent) for adding or removing. |
| 81 |
- Use C(query) for listing an object or multiple objects. |
| 82 |
type: str |
| 83 |
choices: [ absent, present, query ] |
| 84 |
default: present |
| 85 |
extends_documentation_fragment: |
| 86 |
- cisco.aci.aci |
| 87 |
- cisco.aci.annotation |
| 88 |
|
| 89 |
notes: |
| 90 |
- The C(tenant) and C(relay_policy) must exist before using this module in your playbook. |
| 91 |
The M(cisco.aci.aci_tenant) and C(cisco.aci.aci_dhcp_relay) modules can be used for this. |
| 92 |
seealso: |
| 93 |
- name: APIC Management Information Model reference |
| 94 |
description: More information about the internal APIC class B(dhcp:RsProv). |
| 95 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 96 |
author: |
| 97 |
- Tim Cragg (@timcragg) |
| 98 |
- Akini Ross (@akinross) |
| 99 |
""" |
| 100 |
|
| 101 |
EXAMPLES = r""" |
| 102 |
- name: Add a new DHCP relay App EPG provider |
| 103 |
cisco.aci.aci_dhcp_relay_provider: |
| 104 |
host: apic |
| 105 |
username: admin |
| 106 |
password: SomeSecretPassword |
| 107 |
tenant: Auto-Demo |
| 108 |
relay_policy: my_dhcp_relay |
| 109 |
epg_type: epg |
| 110 |
anp: my_anp |
| 111 |
epg: my_app_epg |
| 112 |
dhcp_server_addr: 10.20.30.40 |
| 113 |
state: present |
| 114 |
delegate_to: localhost |
| 115 |
|
| 116 |
- name: Add a new Global (infra) DHCP relay App EPG provider |
| 117 |
cisco.aci.aci_dhcp_relay_provider: |
| 118 |
host: apic |
| 119 |
username: admin |
| 120 |
password: SomeSecretPassword |
| 121 |
relay_policy: my_dhcp_relay |
| 122 |
provider_tenant: Auto-Demo |
| 123 |
epg_type: epg |
| 124 |
anp: my_anp |
| 125 |
epg: my_app_epg |
| 126 |
dhcp_server_addr: 10.20.30.40 |
| 127 |
state: present |
| 128 |
delegate_to: localhost |
| 129 |
|
| 130 |
- name: Add a new DHCP relay L3out provider |
| 131 |
cisco.aci.aci_dhcp_relay_provider: |
| 132 |
host: apic |
| 133 |
username: admin |
| 134 |
password: SomeSecretPassword |
| 135 |
tenant: Auto-Demo |
| 136 |
relay_policy: my_dhcp_relay |
| 137 |
epg_type: l3_external |
| 138 |
l3out_name: my_l3out |
| 139 |
external_net: my_l3out_ext_net |
| 140 |
dhcp_server_addr: 10.20.30.40 |
| 141 |
state: present |
| 142 |
delegate_to: localhost |
| 143 |
|
| 144 |
- name: Remove a DHCP relay provider |
| 145 |
cisco.aci.aci_dhcp_relay_provider: |
| 146 |
host: apic |
| 147 |
username: admin |
| 148 |
password: SomeSecretPassword |
| 149 |
tenant: Auto-Demo |
| 150 |
relay_policy: my_dhcp_relay |
| 151 |
epg_type: epg |
| 152 |
anp: my_anp |
| 153 |
epg: my_app_epg |
| 154 |
state: absent |
| 155 |
delegate_to: localhost |
| 156 |
|
| 157 |
- name: Remove a Global (infra) DHCP relay provider |
| 158 |
cisco.aci.aci_dhcp_relay_provider: |
| 159 |
host: apic |
| 160 |
username: admin |
| 161 |
password: SomeSecretPassword |
| 162 |
relay_policy: my_dhcp_relay |
| 163 |
provider_tenant: Auto-Demo |
| 164 |
epg_type: epg |
| 165 |
anp: my_anp |
| 166 |
epg: my_app_epg |
| 167 |
state: absent |
| 168 |
delegate_to: localhost |
| 169 |
|
| 170 |
- name: Query a DHCP relay provider |
| 171 |
cisco.aci.aci_dhcp_relay_provider: |
| 172 |
host: apic |
| 173 |
username: admin |
| 174 |
password: SomeSecretPassword |
| 175 |
tenant: Auto-Demo |
| 176 |
relay_policy: my_dhcp_relay |
| 177 |
epg_type: epg |
| 178 |
anp: my_anp |
| 179 |
epg: my_app_epg |
| 180 |
state: query |
| 181 |
delegate_to: localhost |
| 182 |
register: query_result |
| 183 |
|
| 184 |
- name: Query a Global (infra) DHCP relay provider |
| 185 |
cisco.aci.aci_dhcp_relay_provider: |
| 186 |
host: apic |
| 187 |
username: admin |
| 188 |
password: SomeSecretPassword |
| 189 |
relay_policy: my_dhcp_relay |
| 190 |
provider_tenant: Auto-Demo |
| 191 |
epg_type: epg |
| 192 |
anp: my_anp |
| 193 |
epg: my_app_epg |
| 194 |
state: query |
| 195 |
delegate_to: localhost |
| 196 |
register: query_result |
| 197 |
|
| 198 |
- name: Query all DHCP relay providers in a specific tenant |
| 199 |
cisco.aci.aci_dhcp_relay_provider: |
| 200 |
host: apic |
| 201 |
username: admin |
| 202 |
password: SomeSecretPassword |
| 203 |
tenant: Auto-Demo |
| 204 |
state: query |
| 205 |
delegate_to: localhost |
| 206 |
register: query_result |
| 207 |
|
| 208 |
- name: Query all DHCP relay providers |
| 209 |
cisco.aci.aci_dhcp_relay_provider: |
| 210 |
host: apic |
| 211 |
username: admin |
| 212 |
password: SomeSecretPassword |
| 213 |
state: query |
| 214 |
delegate_to: localhost |
| 215 |
register: query_result |
| 216 |
""" |
| 217 |
|
| 218 |
RETURN = r""" |
| 219 |
current: |
| 220 |
description: The existing configuration from the APIC after the module has finished |
| 221 |
returned: success |
| 222 |
type: list |
| 223 |
sample: |
| 224 |
[ |
| 225 |
{ |
| 226 |
"fvTenant": { |
| 227 |
"attributes": { |
| 228 |
"descr": "Production environment", |
| 229 |
"dn": "uni/tn-production", |
| 230 |
"name": "production", |
| 231 |
"nameAlias": "", |
| 232 |
"ownerKey": "", |
| 233 |
"ownerTag": "" |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
] |
| 238 |
error: |
| 239 |
description: The error information as returned from the APIC |
| 240 |
returned: failure |
| 241 |
type: dict |
| 242 |
sample: |
| 243 |
{ |
| 244 |
"code": "122", |
| 245 |
"text": "unknown managed object class foo" |
| 246 |
} |
| 247 |
raw: |
| 248 |
description: The raw output returned by the APIC REST API (xml or json) |
| 249 |
returned: parse error |
| 250 |
type: str |
| 251 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class "/></imdata>' |
| 252 |
sent: |
| 253 |
description: The actual/minimal configuration pushed to the APIC |
| 254 |
returned: info |
| 255 |
type: list |
| 256 |
sample: |
| 257 |
{ |
| 258 |
"fvTenant": { |
| 259 |
"attributes": { |
| 260 |
"descr": "Production environment" |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
previous: |
| 265 |
description: The original configuration from the APIC before the module has started |
| 266 |
returned: info |
| 267 |
type: list |
| 268 |
sample: |
| 269 |
[ |
| 270 |
{ |
| 271 |
"fvTenant": { |
| 272 |
"attributes": { |
| 273 |
"descr": "Production", |
| 274 |
"dn": "uni/tn-production", |
| 275 |
"name": "production", |
| 276 |
"nameAlias": "", |
| 277 |
"ownerKey": "", |
| 278 |
"ownerTag": "" |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
] |
| 283 |
proposed: |
| 284 |
description: The assembled configuration from the user-provided parameters |
| 285 |
returned: info |
| 286 |
type: dict |
| 287 |
sample: |
| 288 |
{ |
| 289 |
"fvTenant": { |
| 290 |
"attributes": { |
| 291 |
"descr": "Production environment", |
| 292 |
"name": "production" |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
filter_string: |
| 297 |
description: The filter string used for the request |
| 298 |
returned: failure or debug |
| 299 |
type: str |
| 300 |
sample: ?rsp-prop-include=config-only |
| 301 |
method: |
| 302 |
description: The HTTP method used for the request to the APIC |
| 303 |
returned: failure or debug |
| 304 |
type: str |
| 305 |
sample: POST |
| 306 |
response: |
| 307 |
description: The HTTP response from the APIC |
| 308 |
returned: failure or debug |
| 309 |
type: str |
| 310 |
sample: OK (30 bytes) |
| 311 |
status: |
| 312 |
description: The HTTP status from the APIC |
| 313 |
returned: failure or debug |
| 314 |
type: int |
| 315 |
sample: 200 |
| 316 |
url: |
| 317 |
description: The HTTP url used for the request to the APIC |
| 318 |
returned: failure or debug |
| 319 |
type: str |
| 320 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 321 |
""" |
| 322 |
|
| 323 |
from ansible.module_utils.basic import AnsibleModule |
| 324 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 325 |
|
| 326 |
|
| 327 |
def main(): |
| 328 |
argument_spec = aci_argument_spec() |
| 329 |
argument_spec.update(aci_annotation_spec()) |
| 330 |
argument_spec.update( |
| 331 |
relay_policy=dict(type="str", aliases=["relay_policy_name"]), |
| 332 |
epg_type=dict(type="str", choices=["epg", "l2_external", "l3_external", "dn"]), |
| 333 |
anp=dict(type="str"), |
| 334 |
epg=dict(type="str", aliases=["app_epg"]), |
| 335 |
l2out_name=dict(type="str"), |
| 336 |
l3out_name=dict(type="str"), |
| 337 |
external_epg=dict(type="str", aliases=["external_net"]), |
| 338 |
dhcp_server_addr=dict(type="str"), |
| 339 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 340 |
tenant=dict(type="str"), |
| 341 |
provider_tenant=dict(type="str"), |
| 342 |
dn=dict(type="str"), |
| 343 |
) |
| 344 |
|
| 345 |
module = AnsibleModule( |
| 346 |
argument_spec=argument_spec, |
| 347 |
supports_check_mode=True, |
| 348 |
required_if=[ |
| 349 |
["state", "absent", ["relay_policy", "epg_type"]], |
| 350 |
["state", "present", ["relay_policy", "epg_type"]], |
| 351 |
["epg_type", "epg", ["anp", "epg"]], |
| 352 |
["epg_type", "l2_external", ["l2out_name", "external_epg"]], |
| 353 |
["epg_type", "l3_external", ["l3out_name", "external_epg"]], |
| 354 |
["epg_type", "l3_external", ["provider_tenant", "tenant"], True], |
| 355 |
["epg_type", "l2_external", ["provider_tenant", "tenant"], True], |
| 356 |
["epg_type", "epg", ["provider_tenant", "tenant"], True], |
| 357 |
["epg_type", "dn", ["dn"]], |
| 358 |
], |
| 359 |
mutually_exclusive=[ |
| 360 |
["anp", "l2out_name"], |
| 361 |
["anp", "l3out_name"], |
| 362 |
["anp", "external_epg"], |
| 363 |
["anp", "dn"], |
| 364 |
["epg", "l2out_name"], |
| 365 |
["epg", "l3out_name"], |
| 366 |
["epg", "external_epg"], |
| 367 |
["epg", "dn"], |
| 368 |
["l2out_name", "l3out_name"], |
| 369 |
["l2out_name", "dn"], |
| 370 |
["l3out_name", "dn"], |
| 371 |
["external_epg", "dn"], |
| 372 |
], |
| 373 |
) |
| 374 |
|
| 375 |
relay_policy = module.params.get("relay_policy") |
| 376 |
state = module.params.get("state") |
| 377 |
tenant = module.params.get("tenant") |
| 378 |
epg_type = module.params.get("epg_type") |
| 379 |
anp = module.params.get("anp") |
| 380 |
epg = module.params.get("epg") |
| 381 |
l2out_name = module.params.get("l2out_name") |
| 382 |
l3out_name = module.params.get("l3out_name") |
| 383 |
external_epg = module.params.get("external_epg") |
| 384 |
dhcp_server_addr = module.params.get("dhcp_server_addr") |
| 385 |
provider_tenant = module.params.get("provider_tenant") |
| 386 |
dn = module.params.get("dn") |
| 387 |
|
| 388 |
if provider_tenant is None: |
| 389 |
provider_tenant = tenant |
| 390 |
|
| 391 |
if epg_type == "epg": |
| 392 |
tdn = "uni/tn-{0}/ap-{1}/epg-{2}".format(provider_tenant, anp, epg) |
| 393 |
elif epg_type == "l2_external": |
| 394 |
tdn = "uni/tn-{0}/l2out-{1}/instP-{2}".format(provider_tenant, l2out_name, external_epg) |
| 395 |
elif epg_type == "l3_external": |
| 396 |
tdn = "uni/tn-{0}/out-{1}/instP-{2}".format(provider_tenant, l3out_name, external_epg) |
| 397 |
elif epg_type == "dn": |
| 398 |
tdn = dn |
| 399 |
else: |
| 400 |
tdn = None |
| 401 |
|
| 402 |
if tenant is None: |
| 403 |
root_class = dict( |
| 404 |
aci_class="dhcpRelayP", |
| 405 |
aci_rn="infra/relayp-{0}".format(relay_policy), |
| 406 |
module_object=relay_policy, |
| 407 |
target_filter={"name": relay_policy}, |
| 408 |
) |
| 409 |
subclass_1 = dict( |
| 410 |
aci_class="dhcpRsProv", |
| 411 |
aci_rn="rsprov-[{0}]".format(tdn), |
| 412 |
module_object=tdn, |
| 413 |
target_filter={"tDn": tdn}, |
| 414 |
) |
| 415 |
subclass_2 = None |
| 416 |
else: |
| 417 |
root_class = dict( |
| 418 |
aci_class="fvTenant", |
| 419 |
aci_rn="tn-{0}".format(tenant), |
| 420 |
module_object=tenant, |
| 421 |
target_filter={"name": tenant}, |
| 422 |
) |
| 423 |
subclass_1 = dict( |
| 424 |
aci_class="dhcpRelayP", |
| 425 |
aci_rn="relayp-{0}".format(relay_policy), |
| 426 |
module_object=relay_policy, |
| 427 |
target_filter={"name": relay_policy}, |
| 428 |
) |
| 429 |
subclass_2 = dict( |
| 430 |
aci_class="dhcpRsProv", |
| 431 |
aci_rn="rsprov-[{0}]".format(tdn), |
| 432 |
module_object=tdn, |
| 433 |
target_filter={"tDn": tdn}, |
| 434 |
) |
| 435 |
|
| 436 |
aci = ACIModule(module) |
| 437 |
|
| 438 |
aci.construct_url(root_class=root_class, subclass_1=subclass_1, subclass_2=subclass_2) |
| 439 |
|
| 440 |
aci.get_existing() |
| 441 |
|
| 442 |
if state == "present": |
| 443 |
aci.payload( |
| 444 |
aci_class="dhcpRsProv", |
| 445 |
class_config=dict(addr=dhcp_server_addr, tDn=tdn), |
| 446 |
) |
| 447 |
|
| 448 |
aci.get_diff(aci_class="dhcpRsProv") |
| 449 |
|
| 450 |
aci.post_config() |
| 451 |
|
| 452 |
elif state == "absent": |
| 453 |
aci.delete_config() |
| 454 |
|
| 455 |
aci.exit_json() |
| 456 |
|
| 457 |
|
| 458 |
if __name__ == "__main__": |
| 459 |
main() |