| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2021, Shreyas Srish (@shrsr) <[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_template_bd_dhcp_policy |
| 16 |
short_description: Manage BD DHCP Policy in schema templates |
| 17 |
description: |
| 18 |
- Manage BD DHCP policies in schema templates on Cisco ACI Multi-Site. |
| 19 |
author: |
| 20 |
- Shreyas Srish (@shrsr) |
| 21 |
options: |
| 22 |
schema: |
| 23 |
description: |
| 24 |
- The name of the schema. |
| 25 |
type: str |
| 26 |
required: true |
| 27 |
template: |
| 28 |
description: |
| 29 |
- The name of the template to change. |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
bd: |
| 33 |
description: |
| 34 |
- The name of the BD to manage. |
| 35 |
type: str |
| 36 |
required: true |
| 37 |
dhcp_policy: |
| 38 |
description: |
| 39 |
- The DHCP Policy |
| 40 |
type: str |
| 41 |
aliases: [ name ] |
| 42 |
version: |
| 43 |
description: |
| 44 |
- The version of DHCP Relay Policy. |
| 45 |
type: int |
| 46 |
dhcp_option_policy: |
| 47 |
description: |
| 48 |
- The DHCP Option Policy. |
| 49 |
type: dict |
| 50 |
suboptions: |
| 51 |
name: |
| 52 |
description: |
| 53 |
- The name of the DHCP Option Policy. |
| 54 |
type: str |
| 55 |
required: true |
| 56 |
version: |
| 57 |
description: |
| 58 |
- The version of the DHCP Option Policy. |
| 59 |
type: int |
| 60 |
required: true |
| 61 |
state: |
| 62 |
description: |
| 63 |
- Use C(present) or C(absent) for adding or removing. |
| 64 |
- Use C(query) for listing an object or multiple objects. |
| 65 |
type: str |
| 66 |
choices: [ absent, present, query ] |
| 67 |
default: present |
| 68 |
notes: |
| 69 |
- This module can only be used on versions of MSO that are 3.1.1h or greater. |
| 70 |
- This module can only be used on versions of NDO that are 3.7.2i or lower. |
| 71 |
extends_documentation_fragment: cisco.mso.modules |
| 72 |
""" |
| 73 |
|
| 74 |
EXAMPLES = r""" |
| 75 |
- name: Add a new DHCP policy to a BD |
| 76 |
cisco.mso.mso_schema_template_bd_dhcp_policy: |
| 77 |
host: mso_host |
| 78 |
username: admin |
| 79 |
password: SomeSecretPassword |
| 80 |
schema: Schema 1 |
| 81 |
template: Template 1 |
| 82 |
bd: BD 1 |
| 83 |
name: ansible_test |
| 84 |
version: 1 |
| 85 |
dhcp_option_policy: |
| 86 |
name: ansible_test_option |
| 87 |
version: 1 |
| 88 |
state: present |
| 89 |
|
| 90 |
- name: Remove a DHCP policy from a BD |
| 91 |
cisco.mso.mso_schema_template_bd_dhcp_policy: |
| 92 |
host: mso_host |
| 93 |
username: admin |
| 94 |
password: SomeSecretPassword |
| 95 |
schema: Schema 1 |
| 96 |
template: Template 1 |
| 97 |
bd: BD 1 |
| 98 |
name: ansible_test |
| 99 |
version: 1 |
| 100 |
state: absent |
| 101 |
|
| 102 |
- name: Query a specific BD DHCP Policy |
| 103 |
cisco.mso.mso_schema_template_bd_dhcp_policy: |
| 104 |
host: mso_host |
| 105 |
username: admin |
| 106 |
password: SomeSecretPassword |
| 107 |
schema: Schema 1 |
| 108 |
template: Template 1 |
| 109 |
bd: BD 1 |
| 110 |
name: ansible_test |
| 111 |
state: query |
| 112 |
register: query_result |
| 113 |
|
| 114 |
- name: Query all BD DHCP Policies |
| 115 |
cisco.mso.mso_schema_template_bd_dhcp_policy: |
| 116 |
host: mso_host |
| 117 |
username: admin |
| 118 |
password: SomeSecretPassword |
| 119 |
schema: Schema 1 |
| 120 |
template: Template 1 |
| 121 |
bd: BD 1 |
| 122 |
state: query |
| 123 |
register: query_result |
| 124 |
""" |
| 125 |
|
| 126 |
RETURN = r""" |
| 127 |
""" |
| 128 |
|
| 129 |
from ansible.module_utils.basic import AnsibleModule |
| 130 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, mso_dhcp_option_spec |
| 131 |
|
| 132 |
|
| 133 |
def main(): |
| 134 |
argument_spec = mso_argument_spec() |
| 135 |
argument_spec.update( |
| 136 |
schema=dict(type="str", required=True), |
| 137 |
template=dict(type="str", required=True), |
| 138 |
bd=dict(type="str", required=True), |
| 139 |
dhcp_policy=dict(type="str", aliases=["name"]), |
| 140 |
version=dict(type="int"), |
| 141 |
dhcp_option_policy=dict(type="dict", options=mso_dhcp_option_spec()), |
| 142 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 143 |
) |
| 144 |
|
| 145 |
module = AnsibleModule( |
| 146 |
argument_spec=argument_spec, |
| 147 |
supports_check_mode=True, |
| 148 |
required_if=[ |
| 149 |
["state", "absent", ["dhcp_policy"]], |
| 150 |
["state", "present", ["dhcp_policy", "version"]], |
| 151 |
], |
| 152 |
) |
| 153 |
|
| 154 |
schema = module.params.get("schema") |
| 155 |
template = module.params.get("template").replace(" ", "") |
| 156 |
bd = module.params.get("bd") |
| 157 |
dhcp_policy = module.params.get("dhcp_policy") |
| 158 |
dhcp_option_policy = module.params.get("dhcp_option_policy") |
| 159 |
version = module.params.get("version") |
| 160 |
state = module.params.get("state") |
| 161 |
|
| 162 |
mso = MSOModule(module) |
| 163 |
|
| 164 |
# Get schema |
| 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 |
template_idx = templates.index(template) |
| 172 |
|
| 173 |
# Get BD |
| 174 |
bds = [b.get("name") for b in schema_obj.get("templates")[template_idx]["bds"]] |
| 175 |
if bd not in bds: |
| 176 |
mso.fail_json(msg="Provided BD '{0}' does not exist. Existing BDs: {1}".format(bd, ", ".join(bds))) |
| 177 |
bd_idx = bds.index(bd) |
| 178 |
|
| 179 |
# Check if DHCP policy already exists |
| 180 |
if dhcp_policy: |
| 181 |
check_policy = mso.get_obj("policies/dhcp/relay", name=dhcp_policy, key="DhcpRelayPolicies") |
| 182 |
if check_policy: |
| 183 |
pass |
| 184 |
else: |
| 185 |
mso.fail_json(msg="DHCP policy '{dhcp_policy}' does not exist".format(dhcp_policy=dhcp_policy)) |
| 186 |
|
| 187 |
# Check if DHCP option policy already exists |
| 188 |
if dhcp_option_policy: |
| 189 |
check_option_policy = mso.get_obj("policies/dhcp/option", name=dhcp_option_policy.get("name"), key="DhcpRelayPolicies") |
| 190 |
if check_option_policy: |
| 191 |
pass |
| 192 |
else: |
| 193 |
mso.fail_json(msg="DHCP option policy '{dhcp_option_policy}' does not exist".format(dhcp_option_policy=dhcp_option_policy.get("name"))) |
| 194 |
|
| 195 |
# Get DHCP policies |
| 196 |
dhcp_policies = [s.get("name") for s in schema_obj.get("templates")[template_idx]["bds"][bd_idx]["dhcpLabels"]] |
| 197 |
if dhcp_policy in dhcp_policies: |
| 198 |
dhcp_idx = dhcp_policies.index(dhcp_policy) |
| 199 |
# FIXME: Changes based on index are DANGEROUS |
| 200 |
dhcp_policy_path = "/templates/{0}/bds/{1}/dhcpLabels/{2}".format(template, bd, dhcp_idx) |
| 201 |
mso.existing = schema_obj.get("templates")[template_idx]["bds"][bd_idx]["dhcpLabels"][dhcp_idx] |
| 202 |
|
| 203 |
if state == "query": |
| 204 |
if dhcp_policy is None: |
| 205 |
mso.existing = schema_obj.get("templates")[template_idx]["bds"][bd_idx]["dhcpLabels"] |
| 206 |
elif not mso.existing: |
| 207 |
mso.fail_json(msg="DHCP policy not associated with the bd") |
| 208 |
mso.exit_json() |
| 209 |
|
| 210 |
dhcp_policy_paths = "/templates/{0}/bds/{1}/dhcpLabels".format(template, bd) |
| 211 |
ops = [] |
| 212 |
|
| 213 |
mso.previous = mso.existing |
| 214 |
if state == "absent": |
| 215 |
if mso.existing: |
| 216 |
mso.sent = mso.existing = {} |
| 217 |
ops.append(dict(op="remove", path=dhcp_policy_path)) |
| 218 |
|
| 219 |
elif state == "present": |
| 220 |
payload = dict( |
| 221 |
name=dhcp_policy, |
| 222 |
version=version, |
| 223 |
dhcpOptionLabel=dhcp_option_policy, |
| 224 |
) |
| 225 |
|
| 226 |
mso.sanitize(payload, collate=True) |
| 227 |
|
| 228 |
if mso.existing: |
| 229 |
ops.append(dict(op="replace", path=dhcp_policy_path, value=mso.sent)) |
| 230 |
else: |
| 231 |
ops.append(dict(op="add", path=dhcp_policy_paths + "/-", 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() |