| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2021, Anvitha Jain (@anvitha-jain) <[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": "community"} |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: mso_schema_site_l3out |
| 16 |
short_description: Manage site-local layer3 Out (L3Outs) in schema template |
| 17 |
description: |
| 18 |
- Manage site-local L3Outs in schema template on Cisco ACI Multi-Site. |
| 19 |
- This module can only be used on versions of MSO that are 3.0 or greater. |
| 20 |
- NOTE - Usage of this module for version lesser than 3.0 might break the MSO. |
| 21 |
author: |
| 22 |
- Anvitha Jain (@anvitha-jain) |
| 23 |
options: |
| 24 |
schema: |
| 25 |
description: |
| 26 |
- The name of the schema. |
| 27 |
type: str |
| 28 |
required: true |
| 29 |
site: |
| 30 |
description: |
| 31 |
- The name of the site. |
| 32 |
type: str |
| 33 |
required: true |
| 34 |
template: |
| 35 |
description: |
| 36 |
- The name of the template. |
| 37 |
type: str |
| 38 |
required: true |
| 39 |
vrf: |
| 40 |
description: |
| 41 |
- The VRF associated to this L3out. |
| 42 |
type: dict |
| 43 |
suboptions: |
| 44 |
name: |
| 45 |
description: |
| 46 |
- The name of the VRF to associate with. |
| 47 |
required: true |
| 48 |
type: str |
| 49 |
schema: |
| 50 |
description: |
| 51 |
- The schema that defines the referenced VRF. |
| 52 |
- If this parameter is unspecified, it defaults to the current schema. |
| 53 |
type: str |
| 54 |
template: |
| 55 |
description: |
| 56 |
- The template that defines the referenced VRF. |
| 57 |
- If this parameter is unspecified, it defaults to the current schema. |
| 58 |
type: str |
| 59 |
l3out: |
| 60 |
description: |
| 61 |
- The name of the l3out to manage. |
| 62 |
type: str |
| 63 |
aliases: [ name ] |
| 64 |
state: |
| 65 |
description: |
| 66 |
- Use C(present) or C(absent) for adding or removing. |
| 67 |
- Use C(query) for listing an object or multiple objects. |
| 68 |
type: str |
| 69 |
choices: [ absent, present, query ] |
| 70 |
default: present |
| 71 |
seealso: |
| 72 |
- module: cisco.mso.mso_schema_site |
| 73 |
- module: cisco.mso.mso_schema_template_l3out |
| 74 |
extends_documentation_fragment: cisco.mso.modules |
| 75 |
""" |
| 76 |
|
| 77 |
EXAMPLES = r""" |
| 78 |
- name: Add a new site L3Out |
| 79 |
cisco.mso.mso_schema_site_l3out: |
| 80 |
host: mso_host |
| 81 |
username: admin |
| 82 |
password: SomeSecretPassword |
| 83 |
schema: Schema1 |
| 84 |
site: Site1 |
| 85 |
template: Template1 |
| 86 |
l3out: L3out1 |
| 87 |
vrf: |
| 88 |
name: vrfName |
| 89 |
template: TemplateName |
| 90 |
schema: schemaName |
| 91 |
state: present |
| 92 |
|
| 93 |
- name: Remove a site L3Out |
| 94 |
cisco.mso.mso_schema_site_l3out: |
| 95 |
host: mso_host |
| 96 |
username: admin |
| 97 |
password: SomeSecretPassword |
| 98 |
schema: Schema1 |
| 99 |
site: Site1 |
| 100 |
template: Template1 |
| 101 |
l3out: L3out1 |
| 102 |
state: absent |
| 103 |
|
| 104 |
- name: Query a specific site L3Out |
| 105 |
cisco.mso.mso_schema_site_l3out: |
| 106 |
host: mso_host |
| 107 |
username: admin |
| 108 |
password: SomeSecretPassword |
| 109 |
schema: Schema1 |
| 110 |
site: Site1 |
| 111 |
template: Template1 |
| 112 |
l3out: L3out1 |
| 113 |
state: query |
| 114 |
register: query_result |
| 115 |
|
| 116 |
- name: Query all site l3outs |
| 117 |
cisco.mso.mso_schema_site_l3out: |
| 118 |
host: mso_host |
| 119 |
username: admin |
| 120 |
password: SomeSecretPassword |
| 121 |
schema: Schema1 |
| 122 |
site: Site1 |
| 123 |
template: Template1 |
| 124 |
state: query |
| 125 |
register: query_result |
| 126 |
""" |
| 127 |
|
| 128 |
RETURN = r""" |
| 129 |
""" |
| 130 |
|
| 131 |
from ansible.module_utils.basic import AnsibleModule |
| 132 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, mso_reference_spec |
| 133 |
|
| 134 |
|
| 135 |
def main(): |
| 136 |
argument_spec = mso_argument_spec() |
| 137 |
argument_spec.update( |
| 138 |
schema=dict(type="str", required=True), |
| 139 |
site=dict(type="str", required=True), |
| 140 |
template=dict(type="str", required=True), |
| 141 |
vrf=dict(type="dict", options=mso_reference_spec()), |
| 142 |
l3out=dict(type="str", aliases=["name"]), # This parameter is not required for querying all objects |
| 143 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 144 |
) |
| 145 |
|
| 146 |
module = AnsibleModule( |
| 147 |
argument_spec=argument_spec, |
| 148 |
supports_check_mode=True, |
| 149 |
required_if=[ |
| 150 |
["state", "absent", ["l3out"]], |
| 151 |
["state", "present", ["l3out", "vrf"]], |
| 152 |
], |
| 153 |
) |
| 154 |
|
| 155 |
schema = module.params.get("schema") |
| 156 |
site = module.params.get("site") |
| 157 |
template = module.params.get("template").replace(" ", "") |
| 158 |
l3out = module.params.get("l3out") |
| 159 |
vrf = module.params.get("vrf") |
| 160 |
state = module.params.get("state") |
| 161 |
|
| 162 |
mso = MSOModule(module) |
| 163 |
|
| 164 |
# Get schema objects |
| 165 |
schema_id, schema_path, schema_obj = mso.query_schema(schema) |
| 166 |
|
| 167 |
# Get template |
| 168 |
templates = [t.get("name") for t in schema_obj.get("templates")] |
| 169 |
if template not in templates: |
| 170 |
mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ", ".join(templates))) |
| 171 |
|
| 172 |
# Get site |
| 173 |
site_id = mso.lookup_site(site) |
| 174 |
|
| 175 |
# Get site_idx |
| 176 |
if not schema_obj.get("sites"): |
| 177 |
mso.fail_json(msg="No site associated with template '{0}'. Associate the site with the template using mso_schema_site.".format(template)) |
| 178 |
sites = [(s.get("siteId"), s.get("templateName")) for s in schema_obj.get("sites")] |
| 179 |
if (site_id, template) not in sites: |
| 180 |
mso.fail_json(msg="Provided template '{0}' is not associated to site".format(template)) |
| 181 |
|
| 182 |
# Schema-access uses indexes |
| 183 |
site_idx = sites.index((site_id, template)) |
| 184 |
# Path-based access uses site_id-template |
| 185 |
site_template = "{0}-{1}".format(site_id, template) |
| 186 |
|
| 187 |
# Get l3out |
| 188 |
l3out_ref = mso.l3out_ref(schema_id=schema_id, template=template, l3out=l3out) |
| 189 |
l3outs = [v.get("l3outRef") for v in schema_obj.get("sites")[site_idx]["intersiteL3outs"]] |
| 190 |
|
| 191 |
if l3out is not None and l3out_ref in l3outs: |
| 192 |
l3out_idx = l3outs.index(l3out_ref) |
| 193 |
l3out_path = "/sites/{0}/intersiteL3outs/{1}".format(site_template, l3out) |
| 194 |
mso.existing = schema_obj.get("sites")[site_idx]["intersiteL3outs"][l3out_idx] |
| 195 |
|
| 196 |
if state == "query": |
| 197 |
if l3out is None: |
| 198 |
mso.existing = schema_obj.get("sites")[site_idx]["intersiteL3outs"] |
| 199 |
for l3out in mso.existing: |
| 200 |
l3out["l3outRef"] = mso.dict_from_ref(l3out.get("l3outRef")) |
| 201 |
elif not mso.existing: |
| 202 |
mso.fail_json(msg="L3Out '{l3out}' not found".format(l3out=l3out)) |
| 203 |
mso.exit_json() |
| 204 |
|
| 205 |
l3outs_path = "/sites/{0}/intersiteL3outs".format(site_template) |
| 206 |
ops = [] |
| 207 |
|
| 208 |
mso.previous = mso.existing |
| 209 |
if state == "absent": |
| 210 |
if mso.existing: |
| 211 |
mso.sent = mso.existing = {} |
| 212 |
ops.append(dict(op="remove", path=l3out_path)) |
| 213 |
|
| 214 |
elif state == "present": |
| 215 |
vrf_ref = mso.make_reference(vrf, "vrf", schema_id, template) |
| 216 |
|
| 217 |
payload = dict( |
| 218 |
l3outRef=dict( |
| 219 |
schemaId=schema_id, |
| 220 |
templateName=template, |
| 221 |
l3outName=l3out, |
| 222 |
), |
| 223 |
vrfRef=vrf_ref, |
| 224 |
) |
| 225 |
|
| 226 |
mso.sanitize(payload, collate=True) |
| 227 |
|
| 228 |
if mso.existing: |
| 229 |
ops.append(dict(op="replace", path=l3out_path, value=mso.sent)) |
| 230 |
else: |
| 231 |
ops.append(dict(op="add", path=l3outs_path + "/-", value=mso.sent)) |
| 232 |
|
| 233 |
mso.existing = mso.proposed |
| 234 |
|
| 235 |
if not module.check_mode: |
| 236 |
mso.request(schema_path, method="PATCH", data=ops) |
| 237 |
|
| 238 |
mso.exit_json() |
| 239 |
|
| 240 |
|
| 241 |
if __name__ == "__main__": |
| 242 |
main() |