| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, Lionel Hercot (@lhercot) <[email protected]> |
| 5 |
# Copyright: (c) 2020, Jorge Gomez (@jgomezve) <[email protected]> (based on mso_dhcp_relay_policy module) |
| 6 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = { |
| 12 |
"metadata_version": "1.1", |
| 13 |
"status": ["preview"], |
| 14 |
"supported_by": "community", |
| 15 |
} |
| 16 |
|
| 17 |
DOCUMENTATION = r""" |
| 18 |
--- |
| 19 |
module: mso_dhcp_option_policy_option |
| 20 |
short_description: Manage DHCP options in a DHCP Option policy. |
| 21 |
description: |
| 22 |
- Manage DHCP options in a DHCP Option policy on Cisco Multi-Site Orchestrator. |
| 23 |
- This module is only supported on NDO version prior to v4.0. |
| 24 |
author: |
| 25 |
- Lionel Hercot (@lhercot) |
| 26 |
options: |
| 27 |
dhcp_option_policy: |
| 28 |
description: |
| 29 |
- Name of the DHCP Option Policy |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
name: |
| 33 |
description: |
| 34 |
- Name of the option in the DHCP Option Policy |
| 35 |
type: str |
| 36 |
aliases: [ option ] |
| 37 |
id: |
| 38 |
description: |
| 39 |
- Id of the option in the DHCP Option Policy |
| 40 |
type: int |
| 41 |
data: |
| 42 |
description: |
| 43 |
- Data of the DHCP option in the DHCP Option Policy |
| 44 |
type: str |
| 45 |
state: |
| 46 |
description: |
| 47 |
- Use C(present) or C(absent) for adding or removing. |
| 48 |
- Use C(query) for listing an object or multiple objects. |
| 49 |
type: str |
| 50 |
choices: [ absent, present, query ] |
| 51 |
default: present |
| 52 |
extends_documentation_fragment: cisco.mso.modules |
| 53 |
""" |
| 54 |
|
| 55 |
EXAMPLES = r""" |
| 56 |
- name: Add a new option to a DHCP Option Policy |
| 57 |
cisco.mso.mso_dhcp_option_policy_option: |
| 58 |
host: mso_host |
| 59 |
username: admin |
| 60 |
password: SomeSecretPassword |
| 61 |
dhcp_option_policy: my_test_dhcp_policy |
| 62 |
name: ansible_test |
| 63 |
id: 1 |
| 64 |
data: Data stored in the option |
| 65 |
state: present |
| 66 |
|
| 67 |
- name: Remove a option to a DHCP Option Policy |
| 68 |
cisco.mso.mso_dhcp_option_policy_option: |
| 69 |
host: mso_host |
| 70 |
username: admin |
| 71 |
password: SomeSecretPassword |
| 72 |
dhcp_option_policy: my_test_dhcp_policy |
| 73 |
name: ansible_test |
| 74 |
state: absent |
| 75 |
|
| 76 |
- name: Query a option to a DHCP Option Policy |
| 77 |
cisco.mso.mso_dhcp_option_policy_option: |
| 78 |
host: mso_host |
| 79 |
username: admin |
| 80 |
password: SomeSecretPassword |
| 81 |
dhcp_option_policy: my_test_dhcp_policy |
| 82 |
name: ansible_test |
| 83 |
state: query |
| 84 |
register: query_result |
| 85 |
|
| 86 |
- name: Query all option of a DHCP Option Policy |
| 87 |
cisco.mso.mso_dhcp_option_policy_option: |
| 88 |
host: mso_host |
| 89 |
username: admin |
| 90 |
password: SomeSecretPassword |
| 91 |
dhcp_option_policy: my_test_dhcp_policy |
| 92 |
state: query |
| 93 |
register: query_result |
| 94 |
""" |
| 95 |
|
| 96 |
RETURN = r""" |
| 97 |
""" |
| 98 |
from ansible.module_utils.basic import AnsibleModule |
| 99 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import ( |
| 100 |
MSOModule, |
| 101 |
mso_argument_spec, |
| 102 |
) |
| 103 |
|
| 104 |
|
| 105 |
def main(): |
| 106 |
argument_spec = mso_argument_spec() |
| 107 |
argument_spec.update( |
| 108 |
dhcp_option_policy=dict(type="str", required=True), |
| 109 |
name=dict(type="str", aliases=["option"]), |
| 110 |
id=dict(type="int"), |
| 111 |
data=dict(type="str"), |
| 112 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 113 |
) |
| 114 |
module = AnsibleModule( |
| 115 |
argument_spec=argument_spec, |
| 116 |
supports_check_mode=True, |
| 117 |
required_if=[ |
| 118 |
["state", "present", ["name", "id", "data"]], |
| 119 |
["state", "absent", ["name"]], |
| 120 |
], |
| 121 |
) |
| 122 |
|
| 123 |
dhcp_option_policy = module.params.get("dhcp_option_policy") |
| 124 |
option_id = module.params.get("id") |
| 125 |
name = module.params.get("name") |
| 126 |
data = module.params.get("data") |
| 127 |
state = module.params.get("state") |
| 128 |
|
| 129 |
mso = MSOModule(module) |
| 130 |
|
| 131 |
path = "policies/dhcp/option" |
| 132 |
|
| 133 |
option_index = None |
| 134 |
previous_option = {} |
| 135 |
|
| 136 |
# Query for existing object(s) |
| 137 |
dhcp_option_obj = mso.get_obj(path, name=dhcp_option_policy, key="DhcpRelayPolicies") |
| 138 |
if "id" not in dhcp_option_obj: |
| 139 |
mso.fail_json(msg="DHCP Option Policy '{0}' is not a valid DHCP Option Policy name.".format(dhcp_option_policy)) |
| 140 |
policy_id = dhcp_option_obj.get("id") |
| 141 |
options = [] |
| 142 |
if "dhcpOption" in dhcp_option_obj: |
| 143 |
options = dhcp_option_obj.get("dhcpOption") |
| 144 |
for index, opt in enumerate(options): |
| 145 |
if opt.get("name") == name: |
| 146 |
previous_option = opt |
| 147 |
option_index = index |
| 148 |
|
| 149 |
# If we found an existing object, continue with it |
| 150 |
path = "{0}/{1}".format(path, policy_id) |
| 151 |
|
| 152 |
if state == "query": |
| 153 |
mso.existing = options |
| 154 |
if name is not None: |
| 155 |
mso.existing = previous_option |
| 156 |
mso.exit_json() |
| 157 |
|
| 158 |
mso.previous = previous_option |
| 159 |
if state == "absent": |
| 160 |
option = {} |
| 161 |
if previous_option and option_index is not None: |
| 162 |
options.pop(option_index) |
| 163 |
|
| 164 |
elif state == "present": |
| 165 |
option = dict( |
| 166 |
id=str(option_id), |
| 167 |
name=name, |
| 168 |
data=data, |
| 169 |
) |
| 170 |
if option_index is not None: |
| 171 |
options[option_index] = option |
| 172 |
else: |
| 173 |
options.append(option) |
| 174 |
|
| 175 |
if module.check_mode: |
| 176 |
mso.existing = option |
| 177 |
else: |
| 178 |
mso.existing = dhcp_option_obj |
| 179 |
dhcp_option_obj["dhcpOption"] = options |
| 180 |
mso.sanitize(dhcp_option_obj, collate=True) |
| 181 |
new_dhcp_option_obj = mso.request(path, method="PUT", data=mso.sent) |
| 182 |
mso.existing = {} |
| 183 |
for index, opt in enumerate(new_dhcp_option_obj.get("dhcpOption")): |
| 184 |
if opt.get("name") == name: |
| 185 |
mso.existing = opt |
| 186 |
|
| 187 |
mso.exit_json() |
| 188 |
|
| 189 |
|
| 190 |
if __name__ == "__main__": |
| 191 |
main() |