| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2022, Sabari Jaganathan (@sajagana) <[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 |
|
| 14 |
DOCUMENTATION = r""" |
| 15 |
--- |
| 16 |
module: aci_esg_epg_selector |
| 17 |
short_description: Manage ESG - EPG Selectors (fv:EPgSelector) |
| 18 |
description: |
| 19 |
- Manage Endpoint Security Groups - EPG Selectors on Cisco ACI fabrics. |
| 20 |
|
| 21 |
options: |
| 22 |
tenant: |
| 23 |
description: |
| 24 |
- Name of the tenant. |
| 25 |
type: str |
| 26 |
aliases: [ tenant_name ] |
| 27 |
ap: |
| 28 |
description: |
| 29 |
- The name of the application profile. |
| 30 |
type: str |
| 31 |
aliases: [ app_profile, app_profile_name ] |
| 32 |
esg: |
| 33 |
description: |
| 34 |
- Name of the Endpoint Security Group. |
| 35 |
type: str |
| 36 |
aliases: [ esg_name ] |
| 37 |
epg_ap: |
| 38 |
description: |
| 39 |
- Name of the Application profile which contains the EPG. |
| 40 |
type: str |
| 41 |
epg: |
| 42 |
description: |
| 43 |
- Name of the EPG which is used to create EPG Selector object under the ESG. |
| 44 |
type: str |
| 45 |
aliases: [ epg_name ] |
| 46 |
description: |
| 47 |
description: |
| 48 |
- Description of the ESG Tag Selector. |
| 49 |
type: str |
| 50 |
aliases: [ epg_selector_description ] |
| 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 |
|
| 66 |
seealso: |
| 67 |
- module: cisco.aci.aci_esg |
| 68 |
- name: Manage Endpoint Security Groups (ESGs) objects (fv:ESg) |
| 69 |
description: Manage Endpoint Security Groups (ESGs) on Cisco ACI fabrics. |
| 70 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 71 |
author: |
| 72 |
- Sabari Jaganathan (@sajagana) |
| 73 |
""" |
| 74 |
|
| 75 |
|
| 76 |
EXAMPLES = r""" |
| 77 |
- name: Add an EPG selector |
| 78 |
cisco.aci.aci_esg_epg_selector: |
| 79 |
host: apic |
| 80 |
username: admin |
| 81 |
password: SomeSecretPassword |
| 82 |
tenant: production |
| 83 |
ap: production_ap |
| 84 |
esg: web_esg |
| 85 |
epg_ap: production_ap1 |
| 86 |
epg: production_ap1-epg |
| 87 |
description: epg-test-description |
| 88 |
state: present |
| 89 |
delegate_to: localhost |
| 90 |
|
| 91 |
- name: Add list of EPG selectors |
| 92 |
cisco.aci.aci_esg_epg_selector: |
| 93 |
host: apic |
| 94 |
username: admin |
| 95 |
password: SomeSecretPassword |
| 96 |
tenant: production |
| 97 |
ap: production_ap |
| 98 |
esg: "{{ item.esg }}" |
| 99 |
epg_ap: "{{ item.epg_ap }}" |
| 100 |
epg: "{{ item.epg }}" |
| 101 |
description: epg-test-description |
| 102 |
state: present |
| 103 |
delegate_to: localhost |
| 104 |
with_items: |
| 105 |
- {"epg_ap": "production_ap1", "epg": "epg-test1", "esg": "web_esg"} |
| 106 |
- {"epg_ap": "production_ap1", "epg": "epg-test2", "esg": "web_esg"} |
| 107 |
|
| 108 |
- name: Query an EPG selector with esg and epg name |
| 109 |
cisco.aci.aci_esg_epg_selector: |
| 110 |
host: apic |
| 111 |
username: admin |
| 112 |
password: SomeSecretPassword |
| 113 |
tenant: production |
| 114 |
ap: production_ap |
| 115 |
esg: web_esg |
| 116 |
epg_ap: production_ap1 |
| 117 |
epg: production_ap1-epg |
| 118 |
state: query |
| 119 |
delegate_to: localhost |
| 120 |
|
| 121 |
- name: Query all EPG selectors under a application profile |
| 122 |
cisco.aci.aci_esg_epg_selector: |
| 123 |
host: apic |
| 124 |
username: admin |
| 125 |
password: SomeSecretPassword |
| 126 |
tenant: production |
| 127 |
ap: production_ap |
| 128 |
state: query |
| 129 |
delegate_to: localhost |
| 130 |
|
| 131 |
- name: Query all EPG selectors |
| 132 |
cisco.aci.aci_esg_epg_selector: |
| 133 |
host: apic |
| 134 |
username: admin |
| 135 |
password: SomeSecretPassword |
| 136 |
state: query |
| 137 |
delegate_to: localhost |
| 138 |
|
| 139 |
- name: Remove an EPG selector |
| 140 |
cisco.aci.aci_esg_epg_selector: |
| 141 |
host: apic |
| 142 |
username: admin |
| 143 |
password: SomeSecretPassword |
| 144 |
tenant: production |
| 145 |
ap: production_ap |
| 146 |
esg: web_esg |
| 147 |
epg_ap: production_ap1 |
| 148 |
epg: production_ap1-epg |
| 149 |
state: absent |
| 150 |
delegate_to: localhost |
| 151 |
""" |
| 152 |
|
| 153 |
RETURN = r""" |
| 154 |
current: |
| 155 |
description: The existing configuration from the APIC after the module has finished |
| 156 |
returned: success |
| 157 |
type: list |
| 158 |
sample: |
| 159 |
[ |
| 160 |
{ |
| 161 |
"fvTenant": { |
| 162 |
"attributes": { |
| 163 |
"descr": "Production environment", |
| 164 |
"dn": "uni/tn-production", |
| 165 |
"name": "production", |
| 166 |
"nameAlias": "", |
| 167 |
"ownerKey": "", |
| 168 |
"ownerTag": "" |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
] |
| 173 |
error: |
| 174 |
description: The error information as returned from the APIC |
| 175 |
returned: failure |
| 176 |
type: dict |
| 177 |
sample: |
| 178 |
{ |
| 179 |
"code": "122", |
| 180 |
"text": "unknown managed object class foo" |
| 181 |
} |
| 182 |
raw: |
| 183 |
description: The raw output returned by the APIC REST API (xml or json) |
| 184 |
returned: parse error |
| 185 |
type: str |
| 186 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 187 |
sent: |
| 188 |
description: The actual/minimal configuration pushed to the APIC |
| 189 |
returned: info |
| 190 |
type: list |
| 191 |
sample: |
| 192 |
{ |
| 193 |
"fvTenant": { |
| 194 |
"attributes": { |
| 195 |
"descr": "Production environment" |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
previous: |
| 200 |
description: The original configuration from the APIC before the module has started |
| 201 |
returned: info |
| 202 |
type: list |
| 203 |
sample: |
| 204 |
[ |
| 205 |
{ |
| 206 |
"fvTenant": { |
| 207 |
"attributes": { |
| 208 |
"descr": "Production", |
| 209 |
"dn": "uni/tn-production", |
| 210 |
"name": "production", |
| 211 |
"nameAlias": "", |
| 212 |
"ownerKey": "", |
| 213 |
"ownerTag": "" |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
] |
| 218 |
proposed: |
| 219 |
description: The assembled configuration from the user-provided parameters |
| 220 |
returned: info |
| 221 |
type: dict |
| 222 |
sample: |
| 223 |
{ |
| 224 |
"fvTenant": { |
| 225 |
"attributes": { |
| 226 |
"descr": "Production environment", |
| 227 |
"name": "production" |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
filter_string: |
| 232 |
description: The filter string used for the request |
| 233 |
returned: failure or debug |
| 234 |
type: str |
| 235 |
sample: ?rsp-prop-include=config-only |
| 236 |
method: |
| 237 |
description: The HTTP method used for the request to the APIC |
| 238 |
returned: failure or debug |
| 239 |
type: str |
| 240 |
sample: POST |
| 241 |
response: |
| 242 |
description: The HTTP response from the APIC |
| 243 |
returned: failure or debug |
| 244 |
type: str |
| 245 |
sample: OK (30 bytes) |
| 246 |
status: |
| 247 |
description: The HTTP status from the APIC |
| 248 |
returned: failure or debug |
| 249 |
type: int |
| 250 |
sample: 200 |
| 251 |
url: |
| 252 |
description: The HTTP url used for the request to the APIC |
| 253 |
returned: failure or debug |
| 254 |
type: str |
| 255 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 256 |
""" |
| 257 |
|
| 258 |
from ansible.module_utils.basic import AnsibleModule |
| 259 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 260 |
|
| 261 |
|
| 262 |
def main(): |
| 263 |
argument_spec = aci_argument_spec() |
| 264 |
argument_spec.update(aci_annotation_spec()) |
| 265 |
argument_spec.update( |
| 266 |
tenant=dict(type="str", aliases=["tenant_name"]), |
| 267 |
ap=dict(type="str", aliases=["app_profile", "app_profile_name"]), |
| 268 |
esg=dict(type="str", aliases=["esg_name"]), |
| 269 |
epg_ap=dict(type="str"), |
| 270 |
epg=dict(type="str", aliases=["epg_name"]), |
| 271 |
description=dict(type="str", aliases=["epg_selector_description"]), |
| 272 |
state=dict( |
| 273 |
type="str", |
| 274 |
default="present", |
| 275 |
choices=["absent", "present", "query"], |
| 276 |
), |
| 277 |
name_alias=dict(type="str"), |
| 278 |
) |
| 279 |
|
| 280 |
module = AnsibleModule( |
| 281 |
argument_spec=argument_spec, |
| 282 |
supports_check_mode=True, |
| 283 |
required_if=[ |
| 284 |
["state", "absent", ["tenant", "ap", "esg", "epg", "epg_ap"]], |
| 285 |
["state", "present", ["tenant", "ap", "esg", "epg", "epg_ap"]], |
| 286 |
], |
| 287 |
) |
| 288 |
|
| 289 |
aci = ACIModule(module) |
| 290 |
tenant = module.params.get("tenant") |
| 291 |
ap = module.params.get("ap") |
| 292 |
esg = module.params.get("esg") |
| 293 |
epg_ap = module.params.get("epg_ap") |
| 294 |
epg = module.params.get("epg") |
| 295 |
description = module.params.get("description") |
| 296 |
state = module.params.get("state") |
| 297 |
|
| 298 |
matchEpgDn = "uni/tn-{0}/ap-{1}/epg-{2}".format(tenant, epg_ap, epg) |
| 299 |
epgselector = "epgselector-[{0}]".format(matchEpgDn) |
| 300 |
aci.construct_url( |
| 301 |
root_class=dict( |
| 302 |
aci_class="fvTenant", |
| 303 |
aci_rn="tn-{0}".format(tenant), |
| 304 |
module_object=tenant, |
| 305 |
target_filter={"name": tenant}, |
| 306 |
), |
| 307 |
subclass_1=dict( |
| 308 |
aci_class="fvAp", |
| 309 |
aci_rn="ap-{0}".format(ap), |
| 310 |
module_object=ap, |
| 311 |
target_filter={"name": ap}, |
| 312 |
), |
| 313 |
subclass_2=dict( |
| 314 |
aci_class="fvESg", |
| 315 |
aci_rn="esg-{0}".format(esg), |
| 316 |
module_object=esg, |
| 317 |
target_filter={"name": esg}, |
| 318 |
), |
| 319 |
subclass_3=dict( |
| 320 |
aci_class="fvEPgSelector", |
| 321 |
aci_rn=epgselector, |
| 322 |
module_object=epg, |
| 323 |
target_filter={"matchEpgDn": epg}, |
| 324 |
), |
| 325 |
) |
| 326 |
aci.get_existing() |
| 327 |
|
| 328 |
if state == "present": |
| 329 |
aci.payload( |
| 330 |
aci_class="fvEPgSelector", |
| 331 |
class_config=dict( |
| 332 |
matchEpgDn=matchEpgDn, |
| 333 |
descr=description, |
| 334 |
), |
| 335 |
) |
| 336 |
|
| 337 |
aci.get_diff(aci_class="fvEPgSelector") |
| 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() |