| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Dag Wieers (@dagwieers) <[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_site |
| 16 |
short_description: Manage sites |
| 17 |
description: |
| 18 |
- Manage sites on Cisco ACI Multi-Site. |
| 19 |
author: |
| 20 |
- Dag Wieers (@dagwieers) |
| 21 |
options: |
| 22 |
apic_password: |
| 23 |
description: |
| 24 |
- The password for the APICs. |
| 25 |
- The apic_password attribute is not supported when using with ND platform. |
| 26 |
- See the ND collection for complete site management. |
| 27 |
type: str |
| 28 |
apic_site_id: |
| 29 |
description: |
| 30 |
- The site ID of the APICs. |
| 31 |
type: str |
| 32 |
apic_username: |
| 33 |
description: |
| 34 |
- The username for the APICs. |
| 35 |
- The apic_username attribute is not supported when using with ND platform. |
| 36 |
- See the ND collection for complete site management. |
| 37 |
type: str |
| 38 |
default: admin |
| 39 |
apic_login_domain: |
| 40 |
description: |
| 41 |
- The AAA login domain for the username for the APICs. |
| 42 |
- The apic_login_domain attribute is not supported when using with ND platform. |
| 43 |
- See the ND collection for complete site management. |
| 44 |
type: str |
| 45 |
site: |
| 46 |
description: |
| 47 |
- The name of the site. |
| 48 |
type: str |
| 49 |
aliases: [ name ] |
| 50 |
labels: |
| 51 |
description: |
| 52 |
- The labels for this site. |
| 53 |
- Labels that do not already exist will be automatically created. |
| 54 |
- The labels attribute is not supported when using with ND platform. |
| 55 |
- See the ND collection for complete site management. |
| 56 |
type: list |
| 57 |
elements: str |
| 58 |
location: |
| 59 |
description: |
| 60 |
- Location of the site. |
| 61 |
- The location attribute is not supported when using with ND platform. |
| 62 |
- See the ND collection for complete site management. |
| 63 |
type: dict |
| 64 |
suboptions: |
| 65 |
latitude: |
| 66 |
description: |
| 67 |
- The latitude of the location of the site. |
| 68 |
type: float |
| 69 |
longitude: |
| 70 |
description: |
| 71 |
- The longitude of the location of the site. |
| 72 |
type: float |
| 73 |
urls: |
| 74 |
description: |
| 75 |
- A list of URLs to reference the APICs. |
| 76 |
- The urls attribute is not supported when using with ND platform. |
| 77 |
- See the ND collection for complete site management. |
| 78 |
type: list |
| 79 |
elements: str |
| 80 |
state: |
| 81 |
description: |
| 82 |
- Use C(present) or C(absent) for adding or removing. |
| 83 |
- Use C(query) for listing an object or multiple objects. |
| 84 |
type: str |
| 85 |
choices: [ absent, present, query ] |
| 86 |
default: present |
| 87 |
extends_documentation_fragment: cisco.mso.modules |
| 88 |
""" |
| 89 |
|
| 90 |
EXAMPLES = r""" |
| 91 |
- name: Add a new site |
| 92 |
cisco.mso.mso_site: |
| 93 |
host: mso_host |
| 94 |
username: admin |
| 95 |
password: SomeSecretPassword |
| 96 |
site: north_europe |
| 97 |
description: North European Datacenter |
| 98 |
apic_username: mso_admin |
| 99 |
apic_password: AnotherSecretPassword |
| 100 |
apic_site_id: 12 |
| 101 |
urls: |
| 102 |
- 10.2.3.4 |
| 103 |
- 10.2.4.5 |
| 104 |
- 10.3.5.6 |
| 105 |
labels: |
| 106 |
- NEDC |
| 107 |
- Europe |
| 108 |
- Diegem |
| 109 |
location: |
| 110 |
latitude: 50.887318 |
| 111 |
longitude: 4.447084 |
| 112 |
state: present |
| 113 |
|
| 114 |
- name: Remove a site |
| 115 |
cisco.mso.mso_site: |
| 116 |
host: mso_host |
| 117 |
username: admin |
| 118 |
password: SomeSecretPassword |
| 119 |
site: north_europe |
| 120 |
state: absent |
| 121 |
|
| 122 |
- name: Query a site |
| 123 |
cisco.mso.mso_site: |
| 124 |
host: mso_host |
| 125 |
username: admin |
| 126 |
password: SomeSecretPassword |
| 127 |
site: north_europe |
| 128 |
state: query |
| 129 |
register: query_result |
| 130 |
|
| 131 |
- name: Query all sites |
| 132 |
cisco.mso.mso_site: |
| 133 |
host: mso_host |
| 134 |
username: admin |
| 135 |
password: SomeSecretPassword |
| 136 |
state: query |
| 137 |
register: query_result |
| 138 |
""" |
| 139 |
|
| 140 |
RETURN = r""" |
| 141 |
""" |
| 142 |
|
| 143 |
from ansible.module_utils.basic import AnsibleModule |
| 144 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 145 |
|
| 146 |
|
| 147 |
def main(): |
| 148 |
location_arg_spec = dict( |
| 149 |
latitude=dict(type="float"), |
| 150 |
longitude=dict(type="float"), |
| 151 |
) |
| 152 |
|
| 153 |
argument_spec = mso_argument_spec() |
| 154 |
argument_spec.update( |
| 155 |
apic_password=dict(type="str", no_log=True), |
| 156 |
apic_site_id=dict(type="str"), |
| 157 |
apic_username=dict(type="str", default="admin"), |
| 158 |
apic_login_domain=dict(type="str"), |
| 159 |
labels=dict(type="list", elements="str"), |
| 160 |
location=dict(type="dict", options=location_arg_spec), |
| 161 |
site=dict(type="str", aliases=["name"]), |
| 162 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 163 |
urls=dict(type="list", elements="str"), |
| 164 |
) |
| 165 |
|
| 166 |
module = AnsibleModule( |
| 167 |
argument_spec=argument_spec, |
| 168 |
supports_check_mode=True, |
| 169 |
required_if=[ |
| 170 |
["state", "absent", ["site"]], |
| 171 |
["state", "present", ["apic_site_id", "site"]], |
| 172 |
], |
| 173 |
) |
| 174 |
|
| 175 |
apic_username = module.params.get("apic_username") |
| 176 |
apic_password = module.params.get("apic_password") |
| 177 |
apic_site_id = module.params.get("apic_site_id") |
| 178 |
site = module.params.get("site") |
| 179 |
location = module.params.get("location") |
| 180 |
if location is not None: |
| 181 |
latitude = module.params.get("location")["latitude"] |
| 182 |
longitude = module.params.get("location")["longitude"] |
| 183 |
state = module.params.get("state") |
| 184 |
urls = module.params.get("urls") |
| 185 |
apic_login_domain = module.params.get("apic_login_domain") |
| 186 |
|
| 187 |
mso = MSOModule(module) |
| 188 |
|
| 189 |
site_id = None |
| 190 |
path = "sites" |
| 191 |
api_version = "v1" |
| 192 |
if mso.platform == "nd": |
| 193 |
api_version = "v2" |
| 194 |
|
| 195 |
# Convert labels |
| 196 |
labels = mso.lookup_labels(module.params.get("labels"), "site") |
| 197 |
|
| 198 |
# Query for mso.existing object(s) |
| 199 |
if site: |
| 200 |
if mso.platform == "nd": |
| 201 |
site_info = mso.get_obj(path, api_version=api_version, common=dict(name=site)) |
| 202 |
path = "sites/manage" |
| 203 |
if site_info: |
| 204 |
# If we found an existing object, continue with it |
| 205 |
site_id = site_info.get("id") |
| 206 |
if site_id is not None and site_id != "": |
| 207 |
# Checking if site is managed by MSO |
| 208 |
mso.existing = site_info |
| 209 |
path = "sites/manage/{id}".format(id=site_id) |
| 210 |
else: |
| 211 |
mso.existing = mso.get_obj(path, name=site) |
| 212 |
if mso.existing: |
| 213 |
# If we found an existing object, continue with it |
| 214 |
site_id = mso.existing.get("id") |
| 215 |
path = "sites/{id}".format(id=site_id) |
| 216 |
|
| 217 |
else: |
| 218 |
mso.existing = mso.query_objs(path, api_version=api_version) |
| 219 |
|
| 220 |
if state == "query": |
| 221 |
pass |
| 222 |
|
| 223 |
elif state == "absent": |
| 224 |
mso.previous = mso.existing |
| 225 |
if mso.existing: |
| 226 |
if module.check_mode: |
| 227 |
mso.existing = {} |
| 228 |
else: |
| 229 |
mso.request(path, method="DELETE", qs=dict(force="true"), api_version=api_version) |
| 230 |
mso.existing = {} |
| 231 |
|
| 232 |
elif state == "present": |
| 233 |
mso.previous = mso.existing |
| 234 |
|
| 235 |
if mso.platform == "nd": |
| 236 |
if mso.existing: |
| 237 |
payload = mso.existing |
| 238 |
else: |
| 239 |
if site_info: |
| 240 |
payload = site_info |
| 241 |
payload["common"]["siteId"] = apic_site_id |
| 242 |
else: |
| 243 |
mso.fail_json(msg="Site '{0}' is not a valid Site configured at ND-level. Add Site to ND first.".format(site)) |
| 244 |
|
| 245 |
else: |
| 246 |
payload = dict( |
| 247 |
apicSiteId=apic_site_id, |
| 248 |
id=site_id, |
| 249 |
name=site, |
| 250 |
urls=urls, |
| 251 |
labels=labels, |
| 252 |
username=apic_username, |
| 253 |
password=apic_password, |
| 254 |
) |
| 255 |
|
| 256 |
if location is not None: |
| 257 |
payload["location"] = dict( |
| 258 |
lat=latitude, |
| 259 |
long=longitude, |
| 260 |
) |
| 261 |
|
| 262 |
if apic_login_domain is not None and apic_login_domain not in ["", "local", "Local"]: |
| 263 |
payload["username"] = "apic#{0}\\{1}".format(apic_login_domain, apic_username) |
| 264 |
|
| 265 |
mso.sanitize(payload, collate=True) |
| 266 |
|
| 267 |
if mso.existing: |
| 268 |
if mso.check_changed(): |
| 269 |
if module.check_mode: |
| 270 |
mso.existing = mso.proposed |
| 271 |
else: |
| 272 |
mso.existing = mso.request(path, method="PUT", data=mso.sent, api_version=api_version) |
| 273 |
else: |
| 274 |
if module.check_mode: |
| 275 |
mso.existing = mso.proposed |
| 276 |
else: |
| 277 |
mso.existing = mso.request(path, method="POST", data=mso.sent, api_version=api_version) |
| 278 |
|
| 279 |
if "password" in mso.existing: |
| 280 |
mso.existing["password"] = "******" |
| 281 |
|
| 282 |
mso.exit_json() |
| 283 |
|
| 284 |
|
| 285 |
if __name__ == "__main__": |
| 286 |
main() |