| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2023, Gaspard Micol (@gmicol) <[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_match_community_factor |
| 16 |
short_description: Manage Match Community Factor (rtctrl:MatchCommFactor) |
| 17 |
description: |
| 18 |
- Manage Match Community Factors for Match Terms Based on Community on Cisco ACI fabrics. |
| 19 |
options: |
| 20 |
tenant: |
| 21 |
description: |
| 22 |
- The name of an existing tenant. |
| 23 |
type: str |
| 24 |
aliases: [ tenant_name ] |
| 25 |
match_rule: |
| 26 |
description: |
| 27 |
- The name of an exising match rule profile. |
| 28 |
type: str |
| 29 |
aliases: [ match_rule_name ] |
| 30 |
match_community_term: |
| 31 |
description: |
| 32 |
- The name of an existing match community term. |
| 33 |
type: str |
| 34 |
aliases: [ match_community_term_name ] |
| 35 |
community: |
| 36 |
description: |
| 37 |
- The match community value. |
| 38 |
type: str |
| 39 |
scope: |
| 40 |
description: |
| 41 |
- The item scope. |
| 42 |
- If the scope is transitive, this community may be passed between ASs. |
| 43 |
- If the scope is non-transitive, this community should be carried only within the local AS. |
| 44 |
type: str |
| 45 |
choices: [ transitive, non-transitive ] |
| 46 |
description: |
| 47 |
description: |
| 48 |
- The description for the match community factor. |
| 49 |
type: str |
| 50 |
aliases: [ descr ] |
| 51 |
state: |
| 52 |
description: |
| 53 |
- Use C(present) or C(absent) for adding or removing. |
| 54 |
- Use C(query) for listing an object or multiple objects. |
| 55 |
type: str |
| 56 |
choices: [ absent, present, query ] |
| 57 |
default: present |
| 58 |
name_alias: |
| 59 |
description: |
| 60 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 61 |
type: str |
| 62 |
extends_documentation_fragment: |
| 63 |
- cisco.aci.aci |
| 64 |
- cisco.aci.annotation |
| 65 |
- cisco.aci.owner |
| 66 |
|
| 67 |
notes: |
| 68 |
- The C(tenant), the C(match_rule) and the C(match_community_term) used must exist before using this module in your playbook. |
| 69 |
The M(cisco.aci.aci_tenant), the M(cisco.aci.aci_match_rule) and M(cisco.aci.aci_match_community_term) modules can be used for this. |
| 70 |
seealso: |
| 71 |
- module: cisco.aci.aci_tenant |
| 72 |
- module: cisco.aci.aci_match_rule |
| 73 |
- module: cisco.aci.aci_match_community_term |
| 74 |
- name: APIC Management Information Model reference |
| 75 |
description: More information about the internal APIC class B(rtctrl:MatchCommFactor). |
| 76 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 77 |
author: |
| 78 |
- Gaspard Micol (@gmicol) |
| 79 |
""" |
| 80 |
|
| 81 |
EXAMPLES = r""" |
| 82 |
- name: Create a match match AS-path regex term |
| 83 |
cisco.aci.aci_match_community_factor: |
| 84 |
host: apic |
| 85 |
username: admin |
| 86 |
password: SomeSecretPassword |
| 87 |
match_rule: prod_match_rule |
| 88 |
match_community_term: prod_match_community_term |
| 89 |
community: regular:as2-nn2:4:15 |
| 90 |
scope: transitive |
| 91 |
tenant: production |
| 92 |
state: present |
| 93 |
delegate_to: localhost |
| 94 |
|
| 95 |
- name: Delete a match match AS-path regex term |
| 96 |
cisco.aci.aci_match_community_factor: |
| 97 |
host: apic |
| 98 |
username: admin |
| 99 |
password: SomeSecretPassword |
| 100 |
match_rule: prod_match_rule |
| 101 |
tenant: production |
| 102 |
match_community_term: prod_match_community_term |
| 103 |
community: regular:as2-nn2:4:15 |
| 104 |
state: absent |
| 105 |
delegate_to: localhost |
| 106 |
|
| 107 |
- name: Query all match AS-path regex terms |
| 108 |
cisco.aci.aci_match_community_factor: |
| 109 |
host: apic |
| 110 |
username: admin |
| 111 |
password: SomeSecretPassword |
| 112 |
state: query |
| 113 |
delegate_to: localhost |
| 114 |
register: query_result |
| 115 |
|
| 116 |
- name: Query a specific match match AS-path regex term |
| 117 |
cisco.aci.aci_match_community_factor: |
| 118 |
host: apic |
| 119 |
username: admin |
| 120 |
password: SomeSecretPassword |
| 121 |
match_rule: prod_match_rule |
| 122 |
tenant: production |
| 123 |
match_community_term: prod_match_community_term |
| 124 |
community: regular:as2-nn2:4:15 |
| 125 |
state: query |
| 126 |
delegate_to: localhost |
| 127 |
register: query_result |
| 128 |
""" |
| 129 |
|
| 130 |
RETURN = r""" |
| 131 |
current: |
| 132 |
description: The existing configuration from the APIC after the module has finished |
| 133 |
returned: success |
| 134 |
type: list |
| 135 |
sample: |
| 136 |
[ |
| 137 |
{ |
| 138 |
"fvTenant": { |
| 139 |
"attributes": { |
| 140 |
"descr": "Production environment", |
| 141 |
"dn": "uni/tn-production", |
| 142 |
"name": "production", |
| 143 |
"nameAlias": "", |
| 144 |
"ownerKey": "", |
| 145 |
"ownerauto_continue": "" |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
] |
| 150 |
error: |
| 151 |
description: The error information as returned from the APIC |
| 152 |
returned: failure |
| 153 |
type: dict |
| 154 |
sample: |
| 155 |
{ |
| 156 |
"code": "122", |
| 157 |
"text": "unknown managed object class foo" |
| 158 |
} |
| 159 |
raw: |
| 160 |
description: The raw output returned by the APIC REST API (xml or json) |
| 161 |
returned: parse error |
| 162 |
type: str |
| 163 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 164 |
sent: |
| 165 |
description: The actual/minimal configuration pushed to the APIC |
| 166 |
returned: info |
| 167 |
type: list |
| 168 |
sample: |
| 169 |
{ |
| 170 |
"fvTenant": { |
| 171 |
"attributes": { |
| 172 |
"descr": "Production environment" |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
previous: |
| 177 |
description: The original configuration from the APIC before the module has started |
| 178 |
returned: info |
| 179 |
type: list |
| 180 |
sample: |
| 181 |
[ |
| 182 |
{ |
| 183 |
"fvTenant": { |
| 184 |
"attributes": { |
| 185 |
"descr": "Production", |
| 186 |
"dn": "uni/tn-production", |
| 187 |
"name": "production", |
| 188 |
"nameAlias": "", |
| 189 |
"ownerKey": "", |
| 190 |
"ownerauto_continue": "" |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
] |
| 195 |
proposed: |
| 196 |
description: The assembled configuration from the user-provided parameters |
| 197 |
returned: info |
| 198 |
type: dict |
| 199 |
sample: |
| 200 |
{ |
| 201 |
"fvTenant": { |
| 202 |
"attributes": { |
| 203 |
"descr": "Production environment", |
| 204 |
"name": "production" |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
filter_string: |
| 209 |
description: The filter string used for the request |
| 210 |
returned: failure or debug |
| 211 |
type: str |
| 212 |
sample: ?rsp-prop-include=config-only |
| 213 |
method: |
| 214 |
description: The HTTP method used for the request to the APIC |
| 215 |
returned: failure or debug |
| 216 |
type: str |
| 217 |
sample: POST |
| 218 |
response: |
| 219 |
description: The HTTP response from the APIC |
| 220 |
returned: failure or debug |
| 221 |
type: str |
| 222 |
sample: OK (30 bytes) |
| 223 |
status: |
| 224 |
description: The HTTP status from the APIC |
| 225 |
returned: failure or debug |
| 226 |
type: int |
| 227 |
sample: 200 |
| 228 |
url: |
| 229 |
description: The HTTP url used for the request to the APIC |
| 230 |
returned: failure or debug |
| 231 |
type: str |
| 232 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 233 |
""" |
| 234 |
|
| 235 |
from ansible.module_utils.basic import AnsibleModule |
| 236 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 237 |
|
| 238 |
|
| 239 |
def main(): |
| 240 |
argument_spec = aci_argument_spec() |
| 241 |
argument_spec.update(aci_annotation_spec()) |
| 242 |
argument_spec.update(aci_owner_spec()) |
| 243 |
argument_spec.update( |
| 244 |
tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects |
| 245 |
match_rule=dict(type="str", aliases=["match_rule_name"]), # Not required for querying all objects |
| 246 |
match_community_term=dict(type="str", aliases=["match_community_term_name"]), # Not required for querying all objects |
| 247 |
community=dict(type="str"), |
| 248 |
scope=dict(type="str", choices=["transitive", "non-transitive"]), |
| 249 |
description=dict(type="str", aliases=["descr"]), |
| 250 |
name_alias=dict(type="str"), |
| 251 |
state=dict(type="str", default="present", choices=["present", "absent", "query"]), |
| 252 |
) |
| 253 |
|
| 254 |
module = AnsibleModule( |
| 255 |
argument_spec=argument_spec, |
| 256 |
supports_check_mode=True, |
| 257 |
required_if=[ |
| 258 |
["state", "absent", ["community", "tenant", "match_rule", "match_community_term"]], |
| 259 |
["state", "present", ["community", "tenant", "match_rule", "match_community_term"]], |
| 260 |
], |
| 261 |
) |
| 262 |
|
| 263 |
community = module.params.get("community") |
| 264 |
scope = module.params.get("scope") |
| 265 |
description = module.params.get("description") |
| 266 |
state = module.params.get("state") |
| 267 |
tenant = module.params.get("tenant") |
| 268 |
match_rule = module.params.get("match_rule") |
| 269 |
match_community_term = module.params.get("match_community_term") |
| 270 |
name_alias = module.params.get("name_alias") |
| 271 |
|
| 272 |
aci = ACIModule(module) |
| 273 |
|
| 274 |
aci.construct_url( |
| 275 |
root_class=dict( |
| 276 |
aci_class="fvTenant", |
| 277 |
aci_rn="tn-{0}".format(tenant), |
| 278 |
module_object=tenant, |
| 279 |
target_filter={"name": tenant}, |
| 280 |
), |
| 281 |
subclass_1=dict( |
| 282 |
aci_class="rtctrlSubjP", |
| 283 |
aci_rn="subj-{0}".format(match_rule), |
| 284 |
module_object=match_rule, |
| 285 |
target_filter={"name": match_rule}, |
| 286 |
), |
| 287 |
subclass_2=dict( |
| 288 |
aci_class="rtctrlMatchCommTerm", |
| 289 |
aci_rn="commtrm-{0}".format(match_community_term), |
| 290 |
module_object=match_community_term, |
| 291 |
target_filter={"name": match_community_term}, |
| 292 |
), |
| 293 |
subclass_3=dict( |
| 294 |
aci_class="rtctrlMatchCommFactor", |
| 295 |
aci_rn="commfct-{0}".format(community), |
| 296 |
module_object=community, |
| 297 |
target_filter={"community": community}, |
| 298 |
), |
| 299 |
) |
| 300 |
|
| 301 |
aci.get_existing() |
| 302 |
|
| 303 |
if state == "present": |
| 304 |
aci.payload( |
| 305 |
aci_class="rtctrlMatchCommFactor", |
| 306 |
class_config=dict( |
| 307 |
community=community, |
| 308 |
scope=scope, |
| 309 |
descr=description, |
| 310 |
nameAlias=name_alias, |
| 311 |
), |
| 312 |
) |
| 313 |
|
| 314 |
aci.get_diff(aci_class="rtctrlMatchCommFactor") |
| 315 |
|
| 316 |
aci.post_config() |
| 317 |
|
| 318 |
elif state == "absent": |
| 319 |
aci.delete_config() |
| 320 |
|
| 321 |
aci.exit_json() |
| 322 |
|
| 323 |
|
| 324 |
if __name__ == "__main__": |
| 325 |
main() |