ansible  2.9.27
About: Ansible is an IT Configuration Management, Deployment \
About: Ansible (2.x) is an IT Configuration Management, Deployment & Orchestration tool.
ansible download page.
  Fossies Dox: ansible-2.9.27.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

mso_site.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4# Copyright: (c) 2018, Dag Wieers (@dagwieers) <dag@wieers.com>
5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7from __future__ import absolute_import, division, print_function
8__metaclass__ = type
9
10ANSIBLE_METADATA = {'metadata_version': '1.1',
11 'status': ['preview'],
12 'supported_by': 'community'}
13
14DOCUMENTATION = r'''
15---
16module: mso_site
17short_description: Manage sites
18description:
19- Manage sites on Cisco ACI Multi-Site.
20author:
21- Dag Wieers (@dagwieers)
22version_added: '2.8'
23options:
24 apic_password:
25 description:
26 - The password for the APICs.
27 type: str
28 required: yes
29 apic_site_id:
30 description:
31 - The site ID of the APICs.
32 type: str
33 required: yes
34 apic_username:
35 description:
36 - The username for the APICs.
37 type: str
38 required: yes
39 default: admin
40 site:
41 description:
42 - The name of the site.
43 type: str
44 required: yes
45 aliases: [ name ]
46 labels:
47 description:
48 - The labels for this site.
49 - Labels that do not already exist will be automatically created.
50 type: list
51 location:
52 description:
53 - Location of the site.
54 suboptions:
55 latitude:
56 description:
57 - The latitude of the location of the site.
58 type: float
59 longitude:
60 description:
61 - The longitude of the location of the site.
62 type: float
63 urls:
64 description:
65 - A list of URLs to reference the APICs.
66 type: list
67 state:
68 description:
69 - Use C(present) or C(absent) for adding or removing.
70 - Use C(query) for listing an object or multiple objects.
71 type: str
72 choices: [ absent, present, query ]
73 default: present
74extends_documentation_fragment: mso
75'''
76
77EXAMPLES = r'''
78- name: Add a new site
79 mso_site:
80 host: mso_host
81 username: admin
82 password: SomeSecretPassword
83 site: north_europe
84 description: North European Datacenter
85 apic_username: mso_admin
86 apic_password: AnotherSecretPassword
87 apic_site_id: 12
88 urls:
89 - 10.2.3.4
90 - 10.2.4.5
91 - 10.3.5.6
92 labels:
93 - NEDC
94 - Europe
95 - Diegem
96 location:
97 latitude: 50.887318
98 longitude: 4.447084
99 state: present
100 delegate_to: localhost
101
102- name: Remove a site
103 mso_site:
104 host: mso_host
105 username: admin
106 password: SomeSecretPassword
107 site: north_europe
108 state: absent
109 delegate_to: localhost
110
111- name: Query a site
112 mso_site:
113 host: mso_host
114 username: admin
115 password: SomeSecretPassword
116 site: north_europe
117 state: query
118 delegate_to: localhost
119 register: query_result
120
121- name: Query all sites
122 mso_site:
123 host: mso_host
124 username: admin
125 password: SomeSecretPassword
126 state: query
127 delegate_to: localhost
128 register: query_result
129'''
130
131RETURN = r'''
132'''
133
134from ansible.module_utils.basic import AnsibleModule
135from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec, issubset
136
137
138def main():
139 location_arg_spec = dict(
140 latitude=dict(type='float'),
141 longitude=dict(type='float'),
142 )
143
144 argument_spec = mso_argument_spec()
145 argument_spec.update(
146 apic_password=dict(type='str', no_log=True),
147 apic_site_id=dict(type='str'),
148 apic_username=dict(type='str', default='admin'),
149 labels=dict(type='list'),
150 location=dict(type='dict', options=location_arg_spec),
151 site=dict(type='str', aliases=['name']),
152 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
153 urls=dict(type='list'),
154 )
155
156 module = AnsibleModule(
157 argument_spec=argument_spec,
158 supports_check_mode=True,
159 required_if=[
160 ['state', 'absent', ['site']],
161 ['state', 'present', ['apic_site_id', 'site']],
162 ],
163 )
164
165 apic_username = module.params['apic_username']
166 apic_password = module.params['apic_password']
167 apic_site_id = module.params['apic_site_id']
168 site = module.params['site']
169 location = module.params['location']
170 if location is not None:
171 latitude = module.params['location']['latitude']
172 longitude = module.params['location']['longitude']
173 state = module.params['state']
174 urls = module.params['urls']
175
176 mso = MSOModule(module)
177
178 site_id = None
179 path = 'sites'
180
181 # Convert labels
182 labels = mso.lookup_labels(module.params['labels'], 'site')
183
184 # Query for mso.existing object(s)
185 if site:
186 mso.existing = mso.get_obj(path, name=site)
187 if mso.existing:
188 site_id = mso.existing['id']
189 # If we found an existing object, continue with it
190 path = 'sites/{id}'.format(id=site_id)
191 else:
192 mso.existing = mso.query_objs(path)
193
194 if state == 'query':
195 pass
196
197 elif state == 'absent':
198 mso.previous = mso.existing
199 if mso.existing:
200 if module.check_mode:
201 mso.existing = {}
202 else:
203 mso.existing = mso.request(path, method='DELETE', qs=dict(force='true'))
204
205 elif state == 'present':
206 mso.previous = mso.existing
207
208 payload = dict(
209 apicSiteId=apic_site_id,
210 id=site_id,
211 name=site,
212 urls=urls,
213 labels=labels,
214 username=apic_username,
215 password=apic_password,
216 )
217
218 if location is not None:
219 payload['location'] = dict(
220 lat=latitude,
221 long=longitude,
222 )
223
224 mso.sanitize(payload, collate=True)
225
226 if mso.existing:
227 if not issubset(mso.sent, mso.existing):
228 if module.check_mode:
229 mso.existing = mso.proposed
230 else:
231 mso.existing = mso.request(path, method='PUT', data=mso.sent)
232 else:
233 if module.check_mode:
234 mso.existing = mso.proposed
235 else:
236 mso.existing = mso.request(path, method='POST', data=mso.sent)
237
238 if 'password' in mso.existing:
239 mso.existing['password'] = '******'
240
241 mso.exit_json()
242
243
244if __name__ == "__main__":
245 main()
def issubset(subset, superset)
Definition: mso.py:19