| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2022, Akini Ross (@akinross) <[email protected]> |
| 5 |
# Copyright: (c) 2019, Dag Wieers (@dagwieers) <[email protected]> |
| 6 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"} |
| 13 |
|
| 14 |
DOCUMENTATION = r""" |
| 15 |
--- |
| 16 |
module: mso_schema_site_bd_subnet |
| 17 |
short_description: Manage site-local BD subnets in schema template |
| 18 |
description: |
| 19 |
- Manage site-local BD subnets in schema template on Cisco ACI Multi-Site. |
| 20 |
author: |
| 21 |
- Dag Wieers (@dagwieers) |
| 22 |
options: |
| 23 |
schema: |
| 24 |
description: |
| 25 |
- The name of the schema. |
| 26 |
type: str |
| 27 |
required: true |
| 28 |
site: |
| 29 |
description: |
| 30 |
- The name of the site. |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
template: |
| 34 |
description: |
| 35 |
- The name of the template. |
| 36 |
type: str |
| 37 |
required: true |
| 38 |
bd: |
| 39 |
description: |
| 40 |
- The name of the BD. |
| 41 |
type: str |
| 42 |
required: true |
| 43 |
aliases: [ name ] |
| 44 |
subnet: |
| 45 |
description: |
| 46 |
- The IP range in CIDR notation. |
| 47 |
type: str |
| 48 |
aliases: [ ip ] |
| 49 |
description: |
| 50 |
description: |
| 51 |
- The description of this subnet. |
| 52 |
type: str |
| 53 |
scope: |
| 54 |
description: |
| 55 |
- The scope of the subnet. |
| 56 |
type: str |
| 57 |
choices: [ private, public ] |
| 58 |
shared: |
| 59 |
description: |
| 60 |
- Whether this subnet is shared between VRFs. |
| 61 |
type: bool |
| 62 |
default: false |
| 63 |
no_default_gateway: |
| 64 |
description: |
| 65 |
- Whether this subnet has a default gateway. |
| 66 |
type: bool |
| 67 |
default: false |
| 68 |
querier: |
| 69 |
description: |
| 70 |
- Whether this subnet is an IGMP querier. |
| 71 |
type: bool |
| 72 |
default: false |
| 73 |
primary: |
| 74 |
description: |
| 75 |
- Treat as Primary Subnet. |
| 76 |
- There can be only one primary subnet per address family under a BD. |
| 77 |
- This option can only be used on versions of MSO that are 3.1.1h or greater. |
| 78 |
type: bool |
| 79 |
default: false |
| 80 |
is_virtual_ip: |
| 81 |
description: |
| 82 |
- Treat as Virtual IP Address |
| 83 |
type: bool |
| 84 |
default: false |
| 85 |
state: |
| 86 |
description: |
| 87 |
- Use C(present) or C(absent) for adding or removing. |
| 88 |
- Use C(query) for listing an object or multiple objects. |
| 89 |
type: str |
| 90 |
choices: [ absent, present, query ] |
| 91 |
default: present |
| 92 |
notes: |
| 93 |
- The ACI MultiSite PATCH API has a deficiency requiring some objects to be referenced by index. |
| 94 |
This can cause silent corruption on concurrent access when changing/removing on object as |
| 95 |
the wrong object may be referenced. This module is affected by this deficiency. |
| 96 |
seealso: |
| 97 |
- module: cisco.mso.mso_schema_site_bd |
| 98 |
- module: cisco.mso.mso_schema_template_bd |
| 99 |
extends_documentation_fragment: cisco.mso.modules |
| 100 |
""" |
| 101 |
|
| 102 |
EXAMPLES = r""" |
| 103 |
- name: Add a new site BD subnet |
| 104 |
cisco.mso.mso_schema_site_bd_subnet: |
| 105 |
host: mso_host |
| 106 |
username: admin |
| 107 |
password: SomeSecretPassword |
| 108 |
schema: Schema1 |
| 109 |
site: Site1 |
| 110 |
template: Template1 |
| 111 |
bd: BD1 |
| 112 |
subnet: 11.11.11.0/24 |
| 113 |
state: present |
| 114 |
|
| 115 |
- name: Remove a site BD subnet |
| 116 |
cisco.mso.mso_schema_site_bd_subnet: |
| 117 |
host: mso_host |
| 118 |
username: admin |
| 119 |
password: SomeSecretPassword |
| 120 |
schema: Schema1 |
| 121 |
site: Site1 |
| 122 |
template: Template1 |
| 123 |
bd: BD1 |
| 124 |
subnet: 11.11.11.0/24 |
| 125 |
state: absent |
| 126 |
|
| 127 |
- name: Query a specific site BD subnet |
| 128 |
cisco.mso.mso_schema_site_bd_subnet: |
| 129 |
host: mso_host |
| 130 |
username: admin |
| 131 |
password: SomeSecretPassword |
| 132 |
schema: Schema1 |
| 133 |
site: Site1 |
| 134 |
template: Template1 |
| 135 |
bd: BD1 |
| 136 |
subnet: 11.11.11.0/24 |
| 137 |
state: query |
| 138 |
register: query_result |
| 139 |
|
| 140 |
- name: Query all site BD subnets |
| 141 |
cisco.mso.mso_schema_site_bd_subnet: |
| 142 |
host: mso_host |
| 143 |
username: admin |
| 144 |
password: SomeSecretPassword |
| 145 |
schema: Schema1 |
| 146 |
site: Site1 |
| 147 |
template: Template1 |
| 148 |
bd: BD1 |
| 149 |
state: query |
| 150 |
register: query_result |
| 151 |
""" |
| 152 |
|
| 153 |
RETURN = r""" |
| 154 |
""" |
| 155 |
|
| 156 |
from ansible.module_utils.basic import AnsibleModule |
| 157 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, mso_subnet_spec |
| 158 |
from ansible_collections.cisco.mso.plugins.module_utils.schema import MSOSchema |
| 159 |
|
| 160 |
|
| 161 |
def main(): |
| 162 |
argument_spec = mso_argument_spec() |
| 163 |
argument_spec.update(mso_subnet_spec()) |
| 164 |
argument_spec.update( |
| 165 |
schema=dict(type="str", required=True), |
| 166 |
site=dict(type="str", required=True), |
| 167 |
template=dict(type="str", required=True), |
| 168 |
bd=dict(type="str", aliases=["name"], required=True), |
| 169 |
subnet=dict(type="str", aliases=["ip"]), |
| 170 |
description=dict(type="str"), |
| 171 |
scope=dict(type="str", choices=["private", "public"]), |
| 172 |
shared=dict(type="bool", default=False), |
| 173 |
no_default_gateway=dict(type="bool", default=False), |
| 174 |
querier=dict(type="bool", default=False), |
| 175 |
primary=dict(type="bool", default=False), |
| 176 |
is_virtual_ip=dict(type="bool", default=False), |
| 177 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 178 |
) |
| 179 |
|
| 180 |
module = AnsibleModule( |
| 181 |
argument_spec=argument_spec, |
| 182 |
supports_check_mode=True, |
| 183 |
required_if=[ |
| 184 |
["state", "absent", ["subnet"]], |
| 185 |
["state", "present", ["subnet"]], |
| 186 |
], |
| 187 |
) |
| 188 |
|
| 189 |
schema = module.params.get("schema") |
| 190 |
site = module.params.get("site") |
| 191 |
template = module.params.get("template").replace(" ", "") |
| 192 |
bd = module.params.get("bd") |
| 193 |
ip = module.params.get("subnet") |
| 194 |
description = module.params.get("description") |
| 195 |
scope = module.params.get("scope") |
| 196 |
shared = module.params.get("shared") |
| 197 |
no_default_gateway = module.params.get("no_default_gateway") |
| 198 |
querier = module.params.get("querier") |
| 199 |
primary = module.params.get("primary") |
| 200 |
is_virtual_ip = module.params.get("is_virtual_ip") |
| 201 |
state = module.params.get("state") |
| 202 |
|
| 203 |
mso = MSOModule(module) |
| 204 |
|
| 205 |
mso_schema = MSOSchema(mso, schema, template, site) |
| 206 |
mso_objects = mso_schema.schema_objects |
| 207 |
|
| 208 |
mso_schema.set_template_bd(bd) |
| 209 |
if mso_objects.get("template_bd") and mso_objects.get("template_bd").details.get("l2Stretch") is True and state == "present": |
| 210 |
mso.fail_json( |
| 211 |
msg="The l2Stretch of template bd should be false in order to create a site bd subnet. " "Set l2Stretch as false using mso_schema_template_bd" |
| 212 |
) |
| 213 |
|
| 214 |
if state == "query": |
| 215 |
mso_schema.set_site_bd(bd) |
| 216 |
if not ip: |
| 217 |
mso.existing = mso_objects.get("site_bd").details.get("subnets") |
| 218 |
else: |
| 219 |
mso_schema.set_site_bd_subnet(ip) |
| 220 |
mso.existing = mso_objects.get("site_bd_subnet").details |
| 221 |
mso.exit_json() |
| 222 |
|
| 223 |
mso_schema.set_site_bd(bd, fail_module=False) |
| 224 |
|
| 225 |
subnet = None |
| 226 |
if mso_objects.get("site_bd"): |
| 227 |
mso_schema.set_site_bd_subnet(ip, fail_module=False) |
| 228 |
subnet = mso_objects.get("site_bd_subnet") |
| 229 |
|
| 230 |
mso.previous = mso.existing = subnet.details if subnet else mso.existing |
| 231 |
|
| 232 |
bd_path = "/sites/{0}-{1}/bds".format(mso_objects.get("site").details.get("siteId"), template) |
| 233 |
subnet_path = "{0}/{1}/subnets".format(bd_path, bd) |
| 234 |
ops = [] |
| 235 |
|
| 236 |
if state == "absent": |
| 237 |
if subnet: |
| 238 |
mso.sent = mso.existing = {} |
| 239 |
ops.append(dict(op="remove", path=subnet_path)) |
| 240 |
|
| 241 |
elif state == "present": |
| 242 |
if not mso_objects.get("site_bd"): |
| 243 |
bd_payload = dict( |
| 244 |
bdRef=dict( |
| 245 |
schemaId=mso_schema.id, |
| 246 |
templateName=template, |
| 247 |
bdName=bd, |
| 248 |
), |
| 249 |
hostBasedRouting=False, |
| 250 |
) |
| 251 |
ops.append(dict(op="add", path=bd_path + "/-", value=bd_payload)) |
| 252 |
|
| 253 |
if not subnet: |
| 254 |
if description is None: |
| 255 |
description = ip |
| 256 |
if scope is None: |
| 257 |
scope = "private" |
| 258 |
|
| 259 |
subnet_payload = dict( |
| 260 |
ip=ip, |
| 261 |
description=description, |
| 262 |
scope=scope, |
| 263 |
shared=shared, |
| 264 |
noDefaultGateway=no_default_gateway, |
| 265 |
virtual=is_virtual_ip, |
| 266 |
querier=querier, |
| 267 |
primary=primary, |
| 268 |
) |
| 269 |
|
| 270 |
mso.sanitize(subnet_payload, collate=True) |
| 271 |
|
| 272 |
if subnet: |
| 273 |
ops.append(dict(op="replace", path="{0}/{1}".format(subnet_path, subnet.index), value=mso.sent)) |
| 274 |
else: |
| 275 |
ops.append(dict(op="add", path="{0}/-".format(subnet_path), value=mso.sent)) |
| 276 |
|
| 277 |
mso.existing = mso.proposed |
| 278 |
|
| 279 |
if not module.check_mode: |
| 280 |
mso.request(mso_schema.path, method="PATCH", data=ops) |
| 281 |
|
| 282 |
mso.exit_json() |
| 283 |
|
| 284 |
|
| 285 |
if __name__ == "__main__": |
| 286 |
main() |