| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, Faiz Mohammad (@Ziaf007) <[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_node_mgmt_epg_to_contract |
| 16 |
version_added: "2.12.0" |
| 17 |
short_description: Bind Node Management EPGs to Contracts (fv:RsCons, fv:RsProv, fv:RsProtBy, fv:RsConsIf and mgmt:RsOoBProv) |
| 18 |
description: |
| 19 |
- Bind Node Management EPGs to Contracts on Cisco ACI fabrics. |
| 20 |
notes: |
| 21 |
- The O(epg) and O(contract) used must exist before using this module in your playbook. |
| 22 |
The M(cisco.aci.aci_node_mgmt_epg), M(cisco.aci.aci_oob_contract) and M(cisco.aci.aci_contract) modules can be used for this. |
| 23 |
options: |
| 24 |
contract: |
| 25 |
description: |
| 26 |
- The name of the contract or contract interface. |
| 27 |
type: str |
| 28 |
aliases: [ contract_name, contract_interface ] |
| 29 |
contract_type: |
| 30 |
description: |
| 31 |
- Determines the type of the Contract. |
| 32 |
type: str |
| 33 |
required: true |
| 34 |
choices: [ consumer, provider, taboo, interface ] |
| 35 |
epg: |
| 36 |
description: |
| 37 |
- The name of the Node Management end point group. |
| 38 |
type: str |
| 39 |
aliases: [ epg_name ] |
| 40 |
epg_type: |
| 41 |
description: |
| 42 |
- The type of the Node Management end point group. |
| 43 |
type: str |
| 44 |
required: true |
| 45 |
aliases: [ type ] |
| 46 |
choices: [ in_band, out_of_band ] |
| 47 |
priority: |
| 48 |
description: |
| 49 |
- Quality of Service (QoS) class. |
| 50 |
- The APIC defaults to C(unspecified) when unset during creation. |
| 51 |
type: str |
| 52 |
choices: [ level1, level2, level3, level4, level5, level6, unspecified ] |
| 53 |
provider_match: |
| 54 |
description: |
| 55 |
- The matching algorithm for Provided Contracts. |
| 56 |
- The APIC defaults to C(at_least_one) when unset during creation. |
| 57 |
type: str |
| 58 |
choices: [ all, at_least_one, at_most_one, none ] |
| 59 |
state: |
| 60 |
description: |
| 61 |
- Use C(present) or C(absent) for adding or removing. |
| 62 |
- Use C(query) for listing an object or multiple objects. |
| 63 |
type: str |
| 64 |
choices: [ absent, present, query ] |
| 65 |
default: present |
| 66 |
extends_documentation_fragment: |
| 67 |
- cisco.aci.aci |
| 68 |
- cisco.aci.annotation |
| 69 |
|
| 70 |
seealso: |
| 71 |
- module: cisco.aci.aci_node_mgmt_epg |
| 72 |
- module: cisco.aci.aci_contract |
| 73 |
- module: cisco.aci.aci_oob_contract |
| 74 |
- name: APIC Management Information Model reference |
| 75 |
description: More information about the internal APIC classes B(fv:RsCons), B(fv:RsProv), B(fv:RsProtBy), B(fv:RsConsIf), and B(mgmt:RsOoBProv). |
| 76 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 77 |
author: |
| 78 |
- Faiz Mohammad (@Ziaf007) |
| 79 |
""" |
| 80 |
|
| 81 |
EXAMPLES = r""" |
| 82 |
- name: Add a new contract to Inband EPG binding |
| 83 |
cisco.aci.aci_node_mgmt_epg_to_contract: |
| 84 |
host: apic |
| 85 |
username: admin |
| 86 |
password: SomeSecretPassword |
| 87 |
epg: anstest |
| 88 |
epg_type: in_band |
| 89 |
contract: anstest_http |
| 90 |
contract_type: provider |
| 91 |
priority: level2 |
| 92 |
provider_match: at_least_one |
| 93 |
state: present |
| 94 |
delegate_to: localhost |
| 95 |
|
| 96 |
- name: Add a new contract to Out-of-Band EPG binding |
| 97 |
cisco.aci.aci_node_mgmt_epg_to_contract: |
| 98 |
host: apic |
| 99 |
username: admin |
| 100 |
password: SomeSecretPassword |
| 101 |
epg: anstest |
| 102 |
epg_type: out_of_band |
| 103 |
contract: anstest_http |
| 104 |
contract_type: provider |
| 105 |
priority: level3 |
| 106 |
state: present |
| 107 |
delegate_to: localhost |
| 108 |
|
| 109 |
- name: Update a contract of Inband EPG binding |
| 110 |
cisco.aci.aci_node_mgmt_epg_to_contract: |
| 111 |
host: apic |
| 112 |
username: admin |
| 113 |
password: SomeSecretPassword |
| 114 |
epg: anstest |
| 115 |
epg_type: in_band |
| 116 |
contract: anstest_http |
| 117 |
contract_type: provider |
| 118 |
priority: level5 |
| 119 |
provider_match: all |
| 120 |
state: present |
| 121 |
delegate_to: localhost |
| 122 |
|
| 123 |
- name: Query a specific contract to EPG binding |
| 124 |
cisco.aci.aci_node_mgmt_epg_to_contract: |
| 125 |
host: apic |
| 126 |
username: admin |
| 127 |
password: SomeSecretPassword |
| 128 |
epg: anstest |
| 129 |
epg_type: in_band |
| 130 |
contract: anstest_http |
| 131 |
contract_type: consumer |
| 132 |
state: query |
| 133 |
delegate_to: localhost |
| 134 |
register: query_result |
| 135 |
|
| 136 |
- name: Query all provider contract to EPG bindings |
| 137 |
cisco.aci.aci_node_mgmt_epg_to_contract: |
| 138 |
host: apic |
| 139 |
username: admin |
| 140 |
password: SomeSecretPassword |
| 141 |
contract_type: provider |
| 142 |
state: query |
| 143 |
delegate_to: localhost |
| 144 |
register: query_result |
| 145 |
|
| 146 |
- name: Remove an existing contract to Inband EPG binding |
| 147 |
cisco.aci.aci_node_mgmt_epg_to_contract: |
| 148 |
host: apic |
| 149 |
username: admin |
| 150 |
password: SomeSecretPassword |
| 151 |
epg: anstest |
| 152 |
epg_type: in_band |
| 153 |
contract: anstest_http |
| 154 |
contract_type: consumer |
| 155 |
state: absent |
| 156 |
delegate_to: localhost |
| 157 |
""" |
| 158 |
|
| 159 |
RETURN = r""" |
| 160 |
current: |
| 161 |
description: The existing configuration from the APIC after the module has finished |
| 162 |
returned: success |
| 163 |
type: list |
| 164 |
sample: |
| 165 |
[ |
| 166 |
{ |
| 167 |
"fvTenant": { |
| 168 |
"attributes": { |
| 169 |
"descr": "Production environment", |
| 170 |
"dn": "uni/tn-production", |
| 171 |
"name": "production", |
| 172 |
"nameAlias": "", |
| 173 |
"ownerKey": "", |
| 174 |
"ownerTag": "" |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
] |
| 179 |
error: |
| 180 |
description: The error information as returned from the APIC |
| 181 |
returned: failure |
| 182 |
type: dict |
| 183 |
sample: |
| 184 |
{ |
| 185 |
"code": "122", |
| 186 |
"text": "unknown managed object class foo" |
| 187 |
} |
| 188 |
raw: |
| 189 |
description: The raw output returned by the APIC REST API (xml or json) |
| 190 |
returned: parse error |
| 191 |
type: str |
| 192 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 193 |
sent: |
| 194 |
description: The actual/minimal configuration pushed to the APIC |
| 195 |
returned: info |
| 196 |
type: list |
| 197 |
sample: |
| 198 |
{ |
| 199 |
"fvTenant": { |
| 200 |
"attributes": { |
| 201 |
"descr": "Production environment" |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
previous: |
| 206 |
description: The original configuration from the APIC before the module has started |
| 207 |
returned: info |
| 208 |
type: list |
| 209 |
sample: |
| 210 |
[ |
| 211 |
{ |
| 212 |
"fvTenant": { |
| 213 |
"attributes": { |
| 214 |
"descr": "Production", |
| 215 |
"dn": "uni/tn-production", |
| 216 |
"name": "production", |
| 217 |
"nameAlias": "", |
| 218 |
"ownerKey": "", |
| 219 |
"ownerTag": "" |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
] |
| 224 |
proposed: |
| 225 |
description: The assembled configuration from the user-provided parameters |
| 226 |
returned: info |
| 227 |
type: dict |
| 228 |
sample: |
| 229 |
{ |
| 230 |
"fvTenant": { |
| 231 |
"attributes": { |
| 232 |
"descr": "Production environment", |
| 233 |
"name": "production" |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
filter_string: |
| 238 |
description: The filter string used for the request |
| 239 |
returned: failure or debug |
| 240 |
type: str |
| 241 |
sample: ?rsp-prop-include=config-only |
| 242 |
method: |
| 243 |
description: The HTTP method used for the request to the APIC |
| 244 |
returned: failure or debug |
| 245 |
type: str |
| 246 |
sample: POST |
| 247 |
response: |
| 248 |
description: The HTTP response from the APIC |
| 249 |
returned: failure or debug |
| 250 |
type: str |
| 251 |
sample: OK (30 bytes) |
| 252 |
status: |
| 253 |
description: The HTTP status from the APIC |
| 254 |
returned: failure or debug |
| 255 |
type: int |
| 256 |
sample: 200 |
| 257 |
url: |
| 258 |
description: The HTTP url used for the request to the APIC |
| 259 |
returned: failure or debug |
| 260 |
type: str |
| 261 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 262 |
""" |
| 263 |
|
| 264 |
from ansible.module_utils.basic import AnsibleModule |
| 265 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 266 |
from ansible_collections.cisco.aci.plugins.module_utils.constants import ACI_CLASS_MAPPING, MANAGEMENT_EPG_CLASS_MAPPING, PROVIDER_MATCH_MAPPING |
| 267 |
|
| 268 |
|
| 269 |
def main(): |
| 270 |
argument_spec = aci_argument_spec() |
| 271 |
argument_spec.update(aci_annotation_spec()) |
| 272 |
argument_spec.update( |
| 273 |
contract_type=dict(type="str", choices=["consumer", "provider", "taboo", "interface"], required=True), |
| 274 |
epg_type=dict( |
| 275 |
type="str", aliases=["type"], choices=["in_band", "out_of_band"], required=True |
| 276 |
), # required for querying as provider class for INB and OOB are different |
| 277 |
epg=dict(type="str", aliases=["epg_name"]), # Not required for querying all objects |
| 278 |
contract=dict(type="str", aliases=["contract_name", "contract_interface"]), # Not required for querying all objects |
| 279 |
priority=dict(type="str", choices=["level1", "level2", "level3", "level4", "level5", "level6", "unspecified"]), |
| 280 |
provider_match=dict(type="str", choices=["all", "at_least_one", "at_most_one", "none"]), |
| 281 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 282 |
) |
| 283 |
|
| 284 |
module = AnsibleModule( |
| 285 |
argument_spec=argument_spec, |
| 286 |
supports_check_mode=True, |
| 287 |
required_if=[ |
| 288 |
["state", "absent", ["epg", "contract"]], |
| 289 |
["state", "present", ["epg", "contract"]], |
| 290 |
], |
| 291 |
) |
| 292 |
|
| 293 |
epg_type = module.params.get("epg_type") |
| 294 |
contract = module.params.get("contract") |
| 295 |
contract_type = module.params.get("contract_type") |
| 296 |
epg = module.params.get("epg") |
| 297 |
priority = module.params.get("priority") |
| 298 |
provider_match = module.params.get("provider_match") |
| 299 |
if provider_match is not None: |
| 300 |
provider_match = PROVIDER_MATCH_MAPPING[provider_match] |
| 301 |
state = module.params.get("state") |
| 302 |
|
| 303 |
identifier = None |
| 304 |
if epg_type == "in_band": |
| 305 |
|
| 306 |
if contract_type != "provider" and provider_match is not None: |
| 307 |
module.fail_json(msg="the provider_match is only configurable for Provider Contracts") |
| 308 |
|
| 309 |
identifier = contract_type |
| 310 |
class_config = {"matchT": provider_match, "prio": priority, ACI_CLASS_MAPPING[contract_type]["name"]: contract} |
| 311 |
|
| 312 |
elif epg_type == "out_of_band": |
| 313 |
|
| 314 |
if contract_type != "provider": |
| 315 |
module.fail_json(msg="only provider contract_type is supported for out_of_band epg_type.") |
| 316 |
|
| 317 |
if provider_match is not None: |
| 318 |
module.fail_json(msg="The provider_match argument is not supported for out_of_band Provider contracts") |
| 319 |
|
| 320 |
identifier = "oob_provider" |
| 321 |
class_config = {"prio": priority, "tnVzOOBBrCPName": contract} |
| 322 |
|
| 323 |
aci_class = ACI_CLASS_MAPPING[identifier]["class"] |
| 324 |
aci_rn = ACI_CLASS_MAPPING[identifier]["rn"] |
| 325 |
aci_name = ACI_CLASS_MAPPING[identifier]["name"] |
| 326 |
|
| 327 |
aci = ACIModule(module) |
| 328 |
aci.construct_url( |
| 329 |
root_class=dict( |
| 330 |
aci_class="mgmtp", |
| 331 |
aci_rn="tn-mgmt/mgmtp-default", |
| 332 |
module_object=None, |
| 333 |
), |
| 334 |
subclass_1=dict( |
| 335 |
aci_class=MANAGEMENT_EPG_CLASS_MAPPING[epg_type]["epg_class"], |
| 336 |
aci_rn="{0}{1}".format(MANAGEMENT_EPG_CLASS_MAPPING[epg_type]["epg_rn"], epg), |
| 337 |
module_object=epg, |
| 338 |
target_filter={"name": epg}, |
| 339 |
), |
| 340 |
subclass_2=dict( |
| 341 |
aci_class=aci_class, |
| 342 |
aci_rn="{0}{1}".format(aci_rn, contract), |
| 343 |
module_object=contract, |
| 344 |
target_filter={aci_name: contract}, |
| 345 |
), |
| 346 |
) |
| 347 |
|
| 348 |
aci.get_existing() |
| 349 |
|
| 350 |
if state == "present": |
| 351 |
aci.payload( |
| 352 |
aci_class=aci_class, |
| 353 |
class_config=class_config, |
| 354 |
) |
| 355 |
|
| 356 |
aci.get_diff(aci_class=aci_class) |
| 357 |
|
| 358 |
aci.post_config() |
| 359 |
|
| 360 |
elif state == "absent": |
| 361 |
aci.delete_config() |
| 362 |
|
| 363 |
aci.exit_json() |
| 364 |
|
| 365 |
|
| 366 |
if __name__ == "__main__": |
| 367 |
main() |