| 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_vrf |
| 15 |
short_description: Manage contexts or VRFs (fv:Ctx) |
| 16 |
description: |
| 17 |
- Manage contexts or VRFs on Cisco ACI fabrics. |
| 18 |
- Each context is a private network associated to a tenant, i.e. VRF. |
| 19 |
- Enable Preferred Groups for VRF |
| 20 |
options: |
| 21 |
tenant: |
| 22 |
description: |
| 23 |
- The name of the Tenant the VRF should belong to. |
| 24 |
type: str |
| 25 |
aliases: [ tenant_name ] |
| 26 |
vrf: |
| 27 |
description: |
| 28 |
- The name of the VRF. |
| 29 |
type: str |
| 30 |
aliases: [ context, name, vrf_name ] |
| 31 |
policy_control_direction: |
| 32 |
description: |
| 33 |
- Determines if the policy should be enforced by the fabric on ingress or egress. |
| 34 |
type: str |
| 35 |
choices: [ egress, ingress ] |
| 36 |
policy_control_preference: |
| 37 |
description: |
| 38 |
- Determines if the fabric should enforce contract policies to allow routing and packet forwarding. |
| 39 |
type: str |
| 40 |
choices: [ enforced, unenforced ] |
| 41 |
description: |
| 42 |
description: |
| 43 |
- The description for the VRF. |
| 44 |
type: str |
| 45 |
aliases: [ descr ] |
| 46 |
ip_data_plane_learning: |
| 47 |
description: |
| 48 |
- Whether IP data plane learning is enabled or disabled. |
| 49 |
- The APIC defaults to C(enabled) when unset during creation. |
| 50 |
type: str |
| 51 |
choices: [ enabled, disabled ] |
| 52 |
aliases: [ ip_dataplane_learning ] |
| 53 |
state: |
| 54 |
description: |
| 55 |
- Use C(present) or C(absent) for adding or removing. |
| 56 |
- Use C(query) for listing an object or multiple objects. |
| 57 |
type: str |
| 58 |
choices: [ absent, present, query ] |
| 59 |
default: present |
| 60 |
name_alias: |
| 61 |
description: |
| 62 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 63 |
type: str |
| 64 |
preferred_group: |
| 65 |
description: |
| 66 |
- Enables preferred groups for the VRF under vzAny |
| 67 |
type: str |
| 68 |
choices: [enabled, disabled] |
| 69 |
match_type: |
| 70 |
description: |
| 71 |
- Configures match type for contracts under vzAny |
| 72 |
type: str |
| 73 |
choices: [ all, at_least_one, at_most_one, none] |
| 74 |
extends_documentation_fragment: |
| 75 |
- cisco.aci.aci |
| 76 |
- cisco.aci.annotation |
| 77 |
- cisco.aci.owner |
| 78 |
|
| 79 |
notes: |
| 80 |
- The C(tenant) used must exist before using this module in your playbook. |
| 81 |
The M(cisco.aci.aci_tenant) module can be used for this. |
| 82 |
seealso: |
| 83 |
- module: cisco.aci.aci_tenant |
| 84 |
- name: APIC Management Information Model reference |
| 85 |
description: More information about the internal APIC class B(fv:Ctx). |
| 86 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 87 |
author: |
| 88 |
- Jacob McGill (@jmcgill298) |
| 89 |
""" |
| 90 |
|
| 91 |
EXAMPLES = r""" |
| 92 |
- name: Add a new VRF to a tenant |
| 93 |
cisco.aci.aci_vrf: |
| 94 |
host: apic |
| 95 |
username: admin |
| 96 |
password: SomeSecretPassword |
| 97 |
vrf: vrf_lab |
| 98 |
tenant: lab_tenant |
| 99 |
descr: Lab VRF |
| 100 |
policy_control_preference: enforced |
| 101 |
policy_control_direction: ingress |
| 102 |
state: present |
| 103 |
delegate_to: localhost |
| 104 |
|
| 105 |
- name: Remove a VRF for a tenant |
| 106 |
cisco.aci.aci_vrf: |
| 107 |
host: apic |
| 108 |
username: admin |
| 109 |
password: SomeSecretPassword |
| 110 |
vrf: vrf_lab |
| 111 |
tenant: lab_tenant |
| 112 |
state: absent |
| 113 |
delegate_to: localhost |
| 114 |
|
| 115 |
- name: Query a VRF of a tenant |
| 116 |
cisco.aci.aci_vrf: |
| 117 |
host: apic |
| 118 |
username: admin |
| 119 |
password: SomeSecretPassword |
| 120 |
vrf: vrf_lab |
| 121 |
tenant: lab_tenant |
| 122 |
state: query |
| 123 |
delegate_to: localhost |
| 124 |
register: query_result |
| 125 |
|
| 126 |
- name: Query all VRFs |
| 127 |
cisco.aci.aci_vrf: |
| 128 |
host: apic |
| 129 |
username: admin |
| 130 |
password: SomeSecretPassword |
| 131 |
state: query |
| 132 |
delegate_to: localhost |
| 133 |
register: query_result |
| 134 |
""" |
| 135 |
|
| 136 |
RETURN = r""" |
| 137 |
current: |
| 138 |
description: The existing configuration from the APIC after the module has finished |
| 139 |
returned: success |
| 140 |
type: list |
| 141 |
sample: |
| 142 |
[ |
| 143 |
{ |
| 144 |
"fvTenant": { |
| 145 |
"attributes": { |
| 146 |
"descr": "Production environment", |
| 147 |
"dn": "uni/tn-production", |
| 148 |
"name": "production", |
| 149 |
"nameAlias": "", |
| 150 |
"ownerKey": "", |
| 151 |
"ownerTag": "" |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
] |
| 156 |
error: |
| 157 |
description: The error information as returned from the APIC |
| 158 |
returned: failure |
| 159 |
type: dict |
| 160 |
sample: |
| 161 |
{ |
| 162 |
"code": "122", |
| 163 |
"text": "unknown managed object class foo" |
| 164 |
} |
| 165 |
raw: |
| 166 |
description: The raw output returned by the APIC REST API (xml or json) |
| 167 |
returned: parse error |
| 168 |
type: str |
| 169 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 170 |
sent: |
| 171 |
description: The actual/minimal configuration pushed to the APIC |
| 172 |
returned: info |
| 173 |
type: list |
| 174 |
sample: |
| 175 |
{ |
| 176 |
"fvTenant": { |
| 177 |
"attributes": { |
| 178 |
"descr": "Production environment" |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
previous: |
| 183 |
description: The original configuration from the APIC before the module has started |
| 184 |
returned: info |
| 185 |
type: list |
| 186 |
sample: |
| 187 |
[ |
| 188 |
{ |
| 189 |
"fvTenant": { |
| 190 |
"attributes": { |
| 191 |
"descr": "Production", |
| 192 |
"dn": "uni/tn-production", |
| 193 |
"name": "production", |
| 194 |
"nameAlias": "", |
| 195 |
"ownerKey": "", |
| 196 |
"ownerTag": "" |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
] |
| 201 |
proposed: |
| 202 |
description: The assembled configuration from the user-provided parameters |
| 203 |
returned: info |
| 204 |
type: dict |
| 205 |
sample: |
| 206 |
{ |
| 207 |
"fvTenant": { |
| 208 |
"attributes": { |
| 209 |
"descr": "Production environment", |
| 210 |
"name": "production" |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
filter_string: |
| 215 |
description: The filter string used for the request |
| 216 |
returned: failure or debug |
| 217 |
type: str |
| 218 |
sample: ?rsp-prop-include=config-only |
| 219 |
method: |
| 220 |
description: The HTTP method used for the request to the APIC |
| 221 |
returned: failure or debug |
| 222 |
type: str |
| 223 |
sample: POST |
| 224 |
response: |
| 225 |
description: The HTTP response from the APIC |
| 226 |
returned: failure or debug |
| 227 |
type: str |
| 228 |
sample: OK (30 bytes) |
| 229 |
status: |
| 230 |
description: The HTTP status from the APIC |
| 231 |
returned: failure or debug |
| 232 |
type: int |
| 233 |
sample: 200 |
| 234 |
url: |
| 235 |
description: The HTTP url used for the request to the APIC |
| 236 |
returned: failure or debug |
| 237 |
type: str |
| 238 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 239 |
""" |
| 240 |
|
| 241 |
from ansible.module_utils.basic import AnsibleModule |
| 242 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 243 |
|
| 244 |
MATCH_TYPE_MAPPING = dict( |
| 245 |
all="All", |
| 246 |
at_least_one="AtleastOne", |
| 247 |
at_most_one="AtmostOne", |
| 248 |
none="None", |
| 249 |
) |
| 250 |
|
| 251 |
|
| 252 |
def main(): |
| 253 |
argument_spec = aci_argument_spec() |
| 254 |
argument_spec.update(aci_annotation_spec()) |
| 255 |
argument_spec.update(aci_owner_spec()) |
| 256 |
argument_spec.update( |
| 257 |
tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects |
| 258 |
vrf=dict(type="str", aliases=["context", "name", "vrf_name"]), # Not required for querying all objects |
| 259 |
description=dict(type="str", aliases=["descr"]), |
| 260 |
policy_control_direction=dict(type="str", choices=["egress", "ingress"]), |
| 261 |
policy_control_preference=dict(type="str", choices=["enforced", "unenforced"]), |
| 262 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 263 |
preferred_group=dict(type="str", choices=["enabled", "disabled"]), |
| 264 |
match_type=dict(type="str", choices=["all", "at_least_one", "at_most_one", "none"]), |
| 265 |
name_alias=dict(type="str"), |
| 266 |
ip_data_plane_learning=dict(type="str", choices=["enabled", "disabled"], aliases=["ip_dataplane_learning"]), |
| 267 |
) |
| 268 |
|
| 269 |
module = AnsibleModule( |
| 270 |
argument_spec=argument_spec, |
| 271 |
supports_check_mode=True, |
| 272 |
required_if=[ |
| 273 |
["state", "absent", ["tenant", "vrf"]], |
| 274 |
["state", "present", ["tenant", "vrf"]], |
| 275 |
], |
| 276 |
) |
| 277 |
|
| 278 |
description = module.params.get("description") |
| 279 |
policy_control_direction = module.params.get("policy_control_direction") |
| 280 |
policy_control_preference = module.params.get("policy_control_preference") |
| 281 |
state = module.params.get("state") |
| 282 |
tenant = module.params.get("tenant") |
| 283 |
vrf = module.params.get("vrf") |
| 284 |
name_alias = module.params.get("name_alias") |
| 285 |
preferred_group = module.params.get("preferred_group") |
| 286 |
match_type = module.params.get("match_type") |
| 287 |
ip_data_plane_learning = module.params.get("ip_data_plane_learning") |
| 288 |
|
| 289 |
if match_type is not None: |
| 290 |
match_type = MATCH_TYPE_MAPPING[match_type] |
| 291 |
|
| 292 |
aci = ACIModule(module) |
| 293 |
aci.construct_url( |
| 294 |
root_class=dict( |
| 295 |
aci_class="fvTenant", |
| 296 |
aci_rn="tn-{0}".format(tenant), |
| 297 |
module_object=tenant, |
| 298 |
target_filter={"name": tenant}, |
| 299 |
), |
| 300 |
subclass_1=dict( |
| 301 |
aci_class="fvCtx", |
| 302 |
aci_rn="ctx-{0}".format(vrf), |
| 303 |
module_object=vrf, |
| 304 |
target_filter={"name": vrf}, |
| 305 |
), |
| 306 |
child_classes=["vzAny"], |
| 307 |
) |
| 308 |
|
| 309 |
aci.get_existing() |
| 310 |
|
| 311 |
if state == "present": |
| 312 |
aci.payload( |
| 313 |
aci_class="fvCtx", |
| 314 |
class_config=dict( |
| 315 |
descr=description, |
| 316 |
pcEnfDir=policy_control_direction, |
| 317 |
pcEnfPref=policy_control_preference, |
| 318 |
name=vrf, |
| 319 |
nameAlias=name_alias, |
| 320 |
ipDataPlaneLearning=ip_data_plane_learning, |
| 321 |
), |
| 322 |
child_configs=[ |
| 323 |
dict(vzAny=dict(attributes=dict(prefGrMemb=preferred_group, matchT=match_type))), |
| 324 |
], |
| 325 |
) |
| 326 |
|
| 327 |
aci.get_diff(aci_class="fvCtx") |
| 328 |
|
| 329 |
aci.post_config() |
| 330 |
|
| 331 |
elif state == "absent": |
| 332 |
aci.delete_config() |
| 333 |
|
| 334 |
aci.exit_json() |
| 335 |
|
| 336 |
|
| 337 |
if __name__ == "__main__": |
| 338 |
main() |