| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, Akini Ross (@akinross) <[email protected]> |
| 5 |
|
| 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: ndo_dhcp_relay_policy |
| 17 |
short_description: Manage DHCP Relay Policies on Cisco Nexus Dashboard Orchestrator (NDO). |
| 18 |
description: |
| 19 |
- Manage DHCP Relay Policies on Cisco Nexus Dashboard Orchestrator (NDO). |
| 20 |
- This module is only supported on ND v3.1 (NDO v4.3) and later. |
| 21 |
author: |
| 22 |
- Akini Ross (@akinross) |
| 23 |
options: |
| 24 |
template: |
| 25 |
description: |
| 26 |
- The name of the template. |
| 27 |
- The template must be a tenant template. |
| 28 |
type: str |
| 29 |
required: true |
| 30 |
relay_policy: |
| 31 |
description: |
| 32 |
- The name of the DHCP Relay Policy. |
| 33 |
type: str |
| 34 |
aliases: [ name ] |
| 35 |
relay_policy_uuid: |
| 36 |
description: |
| 37 |
- The uuid of the DHCP Relay Policy. |
| 38 |
- This parameter is required when the O(relay_policy) needs to be updated. |
| 39 |
type: str |
| 40 |
aliases: [ uuid ] |
| 41 |
description: |
| 42 |
description: |
| 43 |
- The description of the DHCP Relay Policy. |
| 44 |
type: str |
| 45 |
providers: |
| 46 |
description: |
| 47 |
- A list of providers attached to the DHCP Relay Policy. |
| 48 |
- The list of configured providers must contain at least one provider. |
| 49 |
- When the list of providers is null the update will not change existing providers configuration. |
| 50 |
type: list |
| 51 |
elements: dict |
| 52 |
suboptions: |
| 53 |
schema: |
| 54 |
description: |
| 55 |
- The name of the schema for the (External) EPG. |
| 56 |
type: str |
| 57 |
required: true |
| 58 |
template: |
| 59 |
description: |
| 60 |
- The name of the template for the (External) EPG. |
| 61 |
type: str |
| 62 |
required: true |
| 63 |
anp: |
| 64 |
description: |
| 65 |
- The name of the Application Profile (ANP). |
| 66 |
- This parameter is required when O(providers.epg) is provided. |
| 67 |
type: str |
| 68 |
epg: |
| 69 |
description: |
| 70 |
- The name of the Endpoint Group (EPG). |
| 71 |
- O(providers.epg) and O(providers.external_epg) are mutually exclusive. |
| 72 |
- O(providers.epg) is required when O(providers.external_epg) is not provided. |
| 73 |
type: str |
| 74 |
external_epg: |
| 75 |
description: |
| 76 |
- The name of the External Endpoint Group (EPG). |
| 77 |
- O(providers.external_epg) and O(providers.epg) are mutually exclusive. |
| 78 |
- O(providers.external_epg) is required when O(providers.epg) is not provided. |
| 79 |
type: str |
| 80 |
ip: |
| 81 |
description: |
| 82 |
- The IP address of the DHCP server. |
| 83 |
type: str |
| 84 |
required: true |
| 85 |
use_server_vrf: |
| 86 |
description: |
| 87 |
- Use the server VRF. |
| 88 |
type: bool |
| 89 |
default: false |
| 90 |
state: |
| 91 |
description: |
| 92 |
- Use C(absent) for removing. |
| 93 |
- Use C(query) for listing an object or multiple objects. |
| 94 |
- Use C(present) for creating or updating. |
| 95 |
type: str |
| 96 |
choices: [ absent, query, present ] |
| 97 |
default: query |
| 98 |
extends_documentation_fragment: cisco.mso.modules |
| 99 |
""" |
| 100 |
|
| 101 |
EXAMPLES = r""" |
| 102 |
- name: Create a new dhcp relay policy |
| 103 |
cisco.mso.ndo_dhcp_relay_policy: |
| 104 |
host: mso_host |
| 105 |
username: admin |
| 106 |
password: SomeSecretPassword |
| 107 |
template: ansible_tenant_template |
| 108 |
relay_policy: ansible_test_relay_policy |
| 109 |
providers: |
| 110 |
- schema: ansible_test_schema |
| 111 |
template: ansible_test_template |
| 112 |
anp: ansible_test_anp |
| 113 |
epg: ansible_test_epg_1 |
| 114 |
ip: 1.1.1.1 |
| 115 |
state: present |
| 116 |
register: create |
| 117 |
|
| 118 |
- name: Query a dhcp relay policy with name |
| 119 |
cisco.mso.ndo_dhcp_relay_policy: |
| 120 |
host: mso_host |
| 121 |
username: admin |
| 122 |
password: SomeSecretPassword |
| 123 |
template: ansible_tenant_template |
| 124 |
relay_policy: ansible_test_relay_policy |
| 125 |
state: query |
| 126 |
register: query_one |
| 127 |
|
| 128 |
- name: Query a dhcp relay policy with UUID |
| 129 |
cisco.mso.ndo_dhcp_relay_policy: |
| 130 |
host: mso_host |
| 131 |
username: admin |
| 132 |
password: SomeSecretPassword |
| 133 |
template: ansible_tenant_template |
| 134 |
relay_policy_uuid: '{{ create.current.uuid }}' |
| 135 |
state: query |
| 136 |
register: query_with_uuid |
| 137 |
|
| 138 |
- name: Query all dhcp relay policy in the template |
| 139 |
cisco.mso.ndo_dhcp_relay_policy: |
| 140 |
host: mso_host |
| 141 |
username: admin |
| 142 |
password: SomeSecretPassword |
| 143 |
template: ansible_tenant_template |
| 144 |
state: query |
| 145 |
register: query_all |
| 146 |
|
| 147 |
- name: Delete a dhcp relay policy |
| 148 |
cisco.mso.ndo_dhcp_relay_policy: |
| 149 |
host: mso_host |
| 150 |
username: admin |
| 151 |
password: SomeSecretPassword |
| 152 |
template: ansible_tenant_template |
| 153 |
relay_policy: ansible_test_relay_policy |
| 154 |
state: absent |
| 155 |
""" |
| 156 |
|
| 157 |
RETURN = r""" |
| 158 |
""" |
| 159 |
|
| 160 |
|
| 161 |
import copy |
| 162 |
from ansible.module_utils.basic import AnsibleModule |
| 163 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 164 |
from ansible_collections.cisco.mso.plugins.module_utils.schema import MSOSchema |
| 165 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair |
| 166 |
|
| 167 |
|
| 168 |
def main(): |
| 169 |
argument_spec = mso_argument_spec() |
| 170 |
argument_spec.update( |
| 171 |
template=dict(type="str", required=True), |
| 172 |
relay_policy=dict(type="str", aliases=["name"]), |
| 173 |
relay_policy_uuid=dict(type="str", aliases=["uuid"]), |
| 174 |
description=dict(type="str"), |
| 175 |
providers=dict( |
| 176 |
type="list", |
| 177 |
elements="dict", |
| 178 |
options=dict( |
| 179 |
schema=dict(type="str", required=True), |
| 180 |
template=dict(type="str", required=True), |
| 181 |
anp=dict(type="str"), |
| 182 |
epg=dict(type="str"), |
| 183 |
external_epg=dict(type="str"), |
| 184 |
ip=dict(type="str", required=True), |
| 185 |
use_server_vrf=dict(type="bool", default=False), |
| 186 |
), |
| 187 |
), |
| 188 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 189 |
) |
| 190 |
|
| 191 |
module = AnsibleModule( |
| 192 |
argument_spec=argument_spec, |
| 193 |
supports_check_mode=True, |
| 194 |
required_if=[ |
| 195 |
["state", "absent", ["relay_policy"]], |
| 196 |
["state", "present", ["relay_policy"]], |
| 197 |
], |
| 198 |
) |
| 199 |
|
| 200 |
mso = MSOModule(module) |
| 201 |
|
| 202 |
template = module.params.get("template") |
| 203 |
relay_policy = module.params.get("relay_policy") |
| 204 |
relay_policy_uuid = module.params.get("relay_policy_uuid") |
| 205 |
providers = get_providers_payload(mso, module.params.get("providers")) if module.params.get("providers") else [] |
| 206 |
description = module.params.get("description") |
| 207 |
state = module.params.get("state") |
| 208 |
|
| 209 |
ops = [] |
| 210 |
match = None |
| 211 |
err_message_min_providers = "At least one provider is required when state is present." |
| 212 |
|
| 213 |
mso_template = MSOTemplate(mso, "tenant", template) |
| 214 |
mso_template.validate_template("tenantPolicy") |
| 215 |
|
| 216 |
path = "/tenantPolicyTemplate/template/dhcpRelayPolicies" |
| 217 |
match = get_dhcp_relay_policy(mso_template, relay_policy_uuid, relay_policy) |
| 218 |
|
| 219 |
if relay_policy_uuid or relay_policy: |
| 220 |
if match: |
| 221 |
mso.existing = mso.previous = copy.deepcopy(insert_dhcp_relay_policy_relation_name(match.details, mso_template)) # Query a specific object |
| 222 |
elif match: |
| 223 |
mso.existing = [insert_dhcp_relay_policy_relation_name(dhcp_relay_policy, mso_template) for dhcp_relay_policy in match] # Query all objects |
| 224 |
|
| 225 |
if state == "present": |
| 226 |
if match: |
| 227 |
if module.params.get("providers") is not None and len(providers) == 0: |
| 228 |
mso.fail_json(msg=err_message_min_providers) |
| 229 |
|
| 230 |
if relay_policy and match.details.get("name") != relay_policy: |
| 231 |
ops.append(dict(op="replace", path="{0}/{1}/name".format(path, match.index), value=relay_policy)) |
| 232 |
match.details["name"] = relay_policy |
| 233 |
|
| 234 |
if description is not None and match.details.get("description") != description: |
| 235 |
ops.append(dict(op="replace", path="{0}/{1}/description".format(path, match.index), value=description)) |
| 236 |
match.details["description"] = description |
| 237 |
|
| 238 |
if module.params.get("providers") is not None and match.details.get("providers") != providers: |
| 239 |
ops.append(dict(op="replace", path="{0}/{1}/providers".format(path, match.index), value=providers)) |
| 240 |
match.details["providers"] = providers |
| 241 |
|
| 242 |
mso.sanitize(match.details) |
| 243 |
|
| 244 |
else: |
| 245 |
if not providers: |
| 246 |
mso.fail_json(msg=err_message_min_providers) |
| 247 |
|
| 248 |
payload = {"name": relay_policy, "providers": providers} |
| 249 |
if description: |
| 250 |
payload["description"] = description |
| 251 |
|
| 252 |
ops.append(dict(op="add", path="{0}/-".format(path), value=payload)) |
| 253 |
|
| 254 |
mso.sanitize(payload) |
| 255 |
|
| 256 |
mso.existing = mso.proposed |
| 257 |
|
| 258 |
elif state == "absent": |
| 259 |
if match: |
| 260 |
ops.append(dict(op="remove", path="{0}/{1}".format(path, match.index))) |
| 261 |
mso.existing = {} |
| 262 |
|
| 263 |
if not module.check_mode and ops: |
| 264 |
mso_template.template = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 265 |
match = get_dhcp_relay_policy(mso_template, relay_policy_uuid, relay_policy) |
| 266 |
if match: |
| 267 |
mso.existing = insert_dhcp_relay_policy_relation_name(match.details, mso_template) # When the state is present |
| 268 |
else: |
| 269 |
mso.existing = {} # When the state is absent |
| 270 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 271 |
mso.existing = mso.proposed if state == "present" else {} |
| 272 |
|
| 273 |
mso.exit_json() |
| 274 |
|
| 275 |
|
| 276 |
def insert_dhcp_relay_policy_relation_name(dhcp_relay_policy, mso_template): |
| 277 |
for provider in dhcp_relay_policy.get("providers"): |
| 278 |
if provider.get("epgRef"): |
| 279 |
provider["epgName"] = mso_template.get_template_object_name_by_uuid("epg", provider.get("epgRef")) |
| 280 |
if provider.get("externalEpgRef"): |
| 281 |
provider["externalEpgName"] = mso_template.get_template_object_name_by_uuid("externalEpg", provider.get("externalEpgRef")) |
| 282 |
return dhcp_relay_policy |
| 283 |
|
| 284 |
|
| 285 |
def get_dhcp_relay_policy(mso_template, uuid=None, name=None, fail_module=False): |
| 286 |
match = mso_template.template.get("tenantPolicyTemplate", {}).get("template", {}).get("dhcpRelayPolicies", []) |
| 287 |
if uuid or name: # Query a specific object |
| 288 |
return mso_template.get_object_by_key_value_pairs("DHCP Relay Policy", match, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module) |
| 289 |
return match # Query all objects |
| 290 |
|
| 291 |
|
| 292 |
def get_providers_payload(mso, providers): |
| 293 |
# Cache used to reduce the number of schema queries done by MSOSchema function. |
| 294 |
schema_cache = {} |
| 295 |
|
| 296 |
payload = [] |
| 297 |
for provider in providers: |
| 298 |
schema = provider.get("schema") |
| 299 |
template = provider.get("template") |
| 300 |
anp = provider.get("anp") |
| 301 |
epg = provider.get("epg") |
| 302 |
external_epg = provider.get("external_epg") |
| 303 |
ip = provider.get("ip") |
| 304 |
|
| 305 |
provider_payload = {"ip": ip, "useServerVrf": provider.get("use_server_vrf")} |
| 306 |
|
| 307 |
# Check if schema is already in cache, if not create a new MSOSchema object and add it to the cache. |
| 308 |
if not schema_cache.get(schema): |
| 309 |
schema_cache[schema] = MSOSchema(mso, schema) |
| 310 |
|
| 311 |
schema_cache[schema].set_template(template) |
| 312 |
|
| 313 |
if epg and not anp: |
| 314 |
mso.fail_json(msg="The anp argument is required for each provider when the epg argument is provided.") |
| 315 |
elif not epg and not external_epg: |
| 316 |
mso.fail_json(msg="The epg or external_epg argument is required for each provider.") |
| 317 |
elif epg and external_epg: |
| 318 |
mso.fail_json(msg="The epg and external_epg arguments are mutually exclusive for each provider.") |
| 319 |
elif external_epg and anp: |
| 320 |
mso.fail_json(msg="The anp and external_epg arguments are mutually exclusive for each provider.") |
| 321 |
elif epg: |
| 322 |
schema_cache[schema].set_template_anp(anp) |
| 323 |
schema_cache[schema].set_template_anp_epg(epg) |
| 324 |
provider_payload["epgRef"] = schema_cache[schema].schema_objects["template_anp_epg"].details.get("uuid") |
| 325 |
provider_payload["epgName"] = epg |
| 326 |
else: |
| 327 |
schema_cache[schema].set_template_external_epg(external_epg) |
| 328 |
provider_payload["externalEpgRef"] = schema_cache[schema].schema_objects.get("template_external_epg").details.get("uuid") |
| 329 |
provider_payload["externalEpgName"] = external_epg |
| 330 |
|
| 331 |
payload.append(provider_payload) |
| 332 |
return payload |
| 333 |
|
| 334 |
|
| 335 |
if __name__ == "__main__": |
| 336 |
main() |