| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"} |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
--- |
| 14 |
module: aci_aep_to_epg |
| 15 |
short_description: Bind EPG to AEP (infra:RsFuncToEpg) |
| 16 |
description: |
| 17 |
- Bind EPG to AEP. |
| 18 |
options: |
| 19 |
aep: |
| 20 |
description: |
| 21 |
- The name of the Attachable Access Entity Profile. |
| 22 |
type: str |
| 23 |
aliases: [ aep_name ] |
| 24 |
tenant: |
| 25 |
description: |
| 26 |
- The name of an existing tenant. |
| 27 |
type: str |
| 28 |
aliases: [ tenant_name ] |
| 29 |
ap: |
| 30 |
description: |
| 31 |
- Name of an existing application network profile, that will contain the EPGs. |
| 32 |
type: str |
| 33 |
aliases: [ app_profile, app_profile_name ] |
| 34 |
epg: |
| 35 |
description: |
| 36 |
- The name of the end point group. |
| 37 |
type: str |
| 38 |
aliases: [ epg_name ] |
| 39 |
encap: |
| 40 |
description: |
| 41 |
- The VLAN associated with this application EPG. |
| 42 |
type: int |
| 43 |
aliases: [ vlan, vlan_id, encap_id ] |
| 44 |
primary_encap: |
| 45 |
description: |
| 46 |
- The primary VLAN associated with this EPG |
| 47 |
type: int |
| 48 |
aliases: [ primary_vlan, primary_vlan_id, primary_encap_id ] |
| 49 |
interface_mode: |
| 50 |
description: |
| 51 |
- Determines how layer 2 tags will be read from and added to frames. |
| 52 |
- Values C(802.1p) and C(native) are identical. |
| 53 |
- Values C(access) and C(untagged) are identical. |
| 54 |
- Values C(regular), C(tagged) and C(trunk) are identical. |
| 55 |
- The APIC defaults to C(trunk) when unset during creation. |
| 56 |
type: str |
| 57 |
choices: [ 802.1p, access, native, regular, tagged, trunk, untagged ] |
| 58 |
aliases: [ mode, mode_name, interface_mode_name ] |
| 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 |
author: |
| 71 |
- Marcel Zehnder (@maercu) |
| 72 |
""" |
| 73 |
|
| 74 |
EXAMPLES = r""" |
| 75 |
- name: Associate EPG with AEP |
| 76 |
cisco.aci.aci_aep_to_epg: |
| 77 |
host: apic |
| 78 |
username: admin |
| 79 |
password: SomeSecretPassword |
| 80 |
aep: aep1 |
| 81 |
tenant: tenant1 |
| 82 |
ap: ap1 |
| 83 |
epg: epg1 |
| 84 |
encap_id: 222 |
| 85 |
interface_mode: access |
| 86 |
state: present |
| 87 |
delegate_to: localhost |
| 88 |
|
| 89 |
- name: Associate EPG with AEP |
| 90 |
cisco.aci.aci_aep_to_epg: |
| 91 |
host: apic |
| 92 |
username: admin |
| 93 |
password: SomeSecretPassword |
| 94 |
aep: aep1 |
| 95 |
tenant: tenant1 |
| 96 |
ap: ap1 |
| 97 |
epg: epg1 |
| 98 |
encap_id: 222 |
| 99 |
interface_mode: access |
| 100 |
state: absent |
| 101 |
delegate_to: localhost |
| 102 |
|
| 103 |
- name: Get specific EPG with AEP association |
| 104 |
cisco.aci.aci_aep_to_epg: |
| 105 |
host: apic |
| 106 |
username: admin |
| 107 |
password: SomeSecretPassword |
| 108 |
aep: aep1 |
| 109 |
tenant: tenant1 |
| 110 |
ap: ap1 |
| 111 |
epg: epg1 |
| 112 |
encap_id: 222 |
| 113 |
interface_mode: access |
| 114 |
state: query |
| 115 |
delegate_to: localhost |
| 116 |
register: query_result |
| 117 |
|
| 118 |
- name: Get all EPG with AEP association |
| 119 |
cisco.aci.aci_aep_to_epg: |
| 120 |
host: apic |
| 121 |
username: admin |
| 122 |
password: SomeSecretPassword |
| 123 |
state: query |
| 124 |
delegate_to: localhost |
| 125 |
register: query_result |
| 126 |
""" |
| 127 |
|
| 128 |
RETURN = r""" |
| 129 |
current: |
| 130 |
description: The existing configuration from the APIC after the module has finished |
| 131 |
returned: success |
| 132 |
type: list |
| 133 |
sample: |
| 134 |
[ |
| 135 |
{ |
| 136 |
"fvTenant": { |
| 137 |
"attributes": { |
| 138 |
"descr": "Production environment", |
| 139 |
"dn": "uni/tn-production", |
| 140 |
"name": "production", |
| 141 |
"nameAlias": "", |
| 142 |
"ownerKey": "", |
| 143 |
"ownerTag": "" |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
] |
| 148 |
error: |
| 149 |
description: The error information as returned from the APIC |
| 150 |
returned: failure |
| 151 |
type: dict |
| 152 |
sample: |
| 153 |
{ |
| 154 |
"code": "122", |
| 155 |
"text": "unknown managed object class foo" |
| 156 |
} |
| 157 |
raw: |
| 158 |
description: The raw output returned by the APIC REST API (xml or json) |
| 159 |
returned: parse error |
| 160 |
type: str |
| 161 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 162 |
sent: |
| 163 |
description: The actual/minimal configuration pushed to the APIC |
| 164 |
returned: info |
| 165 |
type: list |
| 166 |
sample: |
| 167 |
{ |
| 168 |
"fvTenant": { |
| 169 |
"attributes": { |
| 170 |
"descr": "Production environment" |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
previous: |
| 175 |
description: The original configuration from the APIC before the module has started |
| 176 |
returned: info |
| 177 |
type: list |
| 178 |
sample: |
| 179 |
[ |
| 180 |
{ |
| 181 |
"fvTenant": { |
| 182 |
"attributes": { |
| 183 |
"descr": "Production", |
| 184 |
"dn": "uni/tn-production", |
| 185 |
"name": "production", |
| 186 |
"nameAlias": "", |
| 187 |
"ownerKey": "", |
| 188 |
"ownerTag": "" |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
] |
| 193 |
proposed: |
| 194 |
description: The assembled configuration from the user-provided parameters |
| 195 |
returned: info |
| 196 |
type: dict |
| 197 |
sample: |
| 198 |
{ |
| 199 |
"fvTenant": { |
| 200 |
"attributes": { |
| 201 |
"descr": "Production environment", |
| 202 |
"name": "production" |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
filter_string: |
| 207 |
description: The filter string used for the request |
| 208 |
returned: failure or debug |
| 209 |
type: str |
| 210 |
sample: ?rsp-prop-include=config-only |
| 211 |
method: |
| 212 |
description: The HTTP method used for the request to the APIC |
| 213 |
returned: failure or debug |
| 214 |
type: str |
| 215 |
sample: POST |
| 216 |
response: |
| 217 |
description: The HTTP response from the APIC |
| 218 |
returned: failure or debug |
| 219 |
type: str |
| 220 |
sample: OK (30 bytes) |
| 221 |
status: |
| 222 |
description: The HTTP status from the APIC |
| 223 |
returned: failure or debug |
| 224 |
type: int |
| 225 |
sample: 200 |
| 226 |
url: |
| 227 |
description: The HTTP url used for the request to the APIC |
| 228 |
returned: failure or debug |
| 229 |
type: str |
| 230 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 231 |
""" |
| 232 |
|
| 233 |
|
| 234 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 235 |
from ansible.module_utils.basic import AnsibleModule |
| 236 |
|
| 237 |
|
| 238 |
INTERFACE_MODE_MAPPING = { |
| 239 |
"802.1p": "native", |
| 240 |
"access": "untagged", |
| 241 |
"native": "native", |
| 242 |
"regular": "regular", |
| 243 |
"tagged": "regular", |
| 244 |
"trunk": "regular", |
| 245 |
"untagged": "untagged", |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
def main(): |
| 250 |
argument_spec = aci_argument_spec() |
| 251 |
argument_spec.update(aci_annotation_spec()) |
| 252 |
argument_spec.update( |
| 253 |
aep=dict(type="str", aliases=["aep_name"]), |
| 254 |
tenant=dict(type="str", aliases=["tenant_name"]), |
| 255 |
ap=dict(type="str", aliases=["app_profile", "app_profile_name"]), |
| 256 |
epg=dict(type="str", aliases=["epg_name"]), |
| 257 |
encap=dict(type="int", aliases=["vlan", "vlan_id", "encap_id"]), |
| 258 |
primary_encap=dict(type="int", aliases=["primary_vlan", "primary_vlan_id", "primary_encap_id"]), |
| 259 |
interface_mode=dict( |
| 260 |
type="str", choices=["802.1p", "access", "native", "regular", "tagged", "trunk", "untagged"], aliases=["mode_name", "mode", "interface_mode_name"] |
| 261 |
), |
| 262 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 263 |
) |
| 264 |
|
| 265 |
module = AnsibleModule( |
| 266 |
argument_spec=argument_spec, |
| 267 |
supports_check_mode=True, |
| 268 |
required_if=[ |
| 269 |
["state", "absent", ["aep", "epg", "ap", "tenant"]], |
| 270 |
["state", "present", ["interface_mode", "encap", "aep", "epg", "ap", "tenant"]], |
| 271 |
], |
| 272 |
) |
| 273 |
|
| 274 |
aep = module.params.get("aep") |
| 275 |
tenant = module.params.get("tenant") |
| 276 |
ap = module.params.get("ap") |
| 277 |
epg = module.params.get("epg") |
| 278 |
encap = module.params.get("encap") |
| 279 |
primary_encap = module.params.get("primary_encap") |
| 280 |
interface_mode = module.params.get("interface_mode") |
| 281 |
state = module.params.get("state") |
| 282 |
|
| 283 |
if interface_mode is not None: |
| 284 |
interface_mode = INTERFACE_MODE_MAPPING[interface_mode] |
| 285 |
|
| 286 |
if encap is not None: |
| 287 |
encap = "vlan-{0}".format(encap) |
| 288 |
|
| 289 |
if primary_encap is not None: |
| 290 |
primary_encap = "vlan-{0}".format(primary_encap) |
| 291 |
|
| 292 |
epg_mo = None |
| 293 |
if tenant is not None and ap is not None and epg is not None: |
| 294 |
epg_mo = "uni/tn-{0}/ap-{1}/epg-{2}".format(tenant, ap, epg) |
| 295 |
|
| 296 |
aci = ACIModule(module) |
| 297 |
aci.construct_url( |
| 298 |
root_class=dict(aci_class="infraAttEntityP", aci_rn="infra/attentp-{0}".format(aep), module_object=aep, target_filter={"name": aep}), |
| 299 |
subclass_1=dict(aci_class="infraGeneric", aci_rn="gen-default", module_object="default", target_filter={"name": "default"}), |
| 300 |
subclass_2=dict(aci_class="infraRsFuncToEpg", aci_rn="rsfuncToEpg-[{0}]".format(epg_mo), module_object=epg_mo, target_filter={"tDn": epg_mo}), |
| 301 |
) |
| 302 |
|
| 303 |
aci.get_existing() |
| 304 |
|
| 305 |
if state == "present": |
| 306 |
# Post configuration on infraGeneric (subclass_1) level instead of on |
| 307 |
# infraRsFuncToEpg (subclass_2) level. |
| 308 |
# The reason being that the MO "gen-default" (of class infraGeneric) does not |
| 309 |
# exist until the first EPG to AEP association is created. |
| 310 |
aci.construct_url( |
| 311 |
root_class=dict(aci_class="infraAttEntityP", aci_rn="infra/attentp-{0}".format(aep), module_object=aep, target_filter={"name": aep}), |
| 312 |
subclass_1=dict(aci_class="infraGeneric", aci_rn="gen-default", module_object="default", target_filter={"name": "default"}), |
| 313 |
child_classes=["infraRsFuncToEpg"], |
| 314 |
) |
| 315 |
|
| 316 |
aci.get_existing() |
| 317 |
|
| 318 |
child_configs = [dict(infraRsFuncToEpg=dict(attributes=dict(encap=encap, primaryEncap=primary_encap, mode=interface_mode, tDn=epg_mo)))] |
| 319 |
|
| 320 |
aci.payload(aci_class="infraGeneric", class_config=dict(name="default"), child_configs=child_configs) |
| 321 |
|
| 322 |
aci.get_diff(aci_class="infraGeneric") |
| 323 |
|
| 324 |
aci.post_config() |
| 325 |
|
| 326 |
elif state == "absent": |
| 327 |
aci.delete_config() |
| 328 |
|
| 329 |
aci.exit_json() |
| 330 |
|
| 331 |
|
| 332 |
if __name__ == "__main__": |
| 333 |
main() |