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_schema_site_anp.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4# Copyright: (c) 2019, 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_schema_site_anp
17short_description: Manage site-local Application Network Profiles (ANPs) in schema template
18description:
19- Manage site-local ANPs in schema template on Cisco ACI Multi-Site.
20author:
21- Dag Wieers (@dagwieers)
22version_added: '2.8'
23options:
24 schema:
25 description:
26 - The name of the schema.
27 type: str
28 required: yes
29 site:
30 description:
31 - The name of the site.
32 type: str
33 required: yes
34 template:
35 description:
36 - The name of the template.
37 type: str
38 required: yes
39 anp:
40 description:
41 - The name of the ANP to manage.
42 type: str
43 aliases: [ name ]
44 state:
45 description:
46 - Use C(present) or C(absent) for adding or removing.
47 - Use C(query) for listing an object or multiple objects.
48 type: str
49 choices: [ absent, present, query ]
50 default: present
51seealso:
52- module: mso_schema_site
53- module: mso_schema_site_anp_epg
54- module: mso_schema_template_anp
55extends_documentation_fragment: mso
56'''
57
58EXAMPLES = r'''
59- name: Add a new site ANP
60 mso_schema_site_anp:
61 host: mso_host
62 username: admin
63 password: SomeSecretPassword
64 schema: Schema1
65 site: Site1
66 template: Template1
67 anp: ANP1
68 state: present
69 delegate_to: localhost
70
71- name: Remove a site ANP
72 mso_schema_site_anp:
73 host: mso_host
74 username: admin
75 password: SomeSecretPassword
76 schema: Schema1
77 site: Site1
78 template: Template1
79 anp: ANP1
80 state: absent
81 delegate_to: localhost
82
83- name: Query a specific site ANPs
84 mso_schema_site_anp:
85 host: mso_host
86 username: admin
87 password: SomeSecretPassword
88 schema: Schema1
89 site: Site1
90 template: Template1
91 state: query
92 delegate_to: localhost
93 register: query_result
94
95- name: Query all site ANPs
96 mso_schema_site_anp:
97 host: mso_host
98 username: admin
99 password: SomeSecretPassword
100 schema: Schema1
101 site: Site1
102 template: Template1
103 state: query
104 delegate_to: localhost
105 register: query_result
106'''
107
108RETURN = r'''
109'''
110
111from ansible.module_utils.basic import AnsibleModule
112from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec, issubset
113
114
115def main():
116 argument_spec = mso_argument_spec()
117 argument_spec.update(
118 schema=dict(type='str', required=True),
119 site=dict(type='str', required=True),
120 template=dict(type='str', required=True),
121 anp=dict(type='str', aliases=['name']), # This parameter is not required for querying all objects
122 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
123 )
124
125 module = AnsibleModule(
126 argument_spec=argument_spec,
127 supports_check_mode=True,
128 required_if=[
129 ['state', 'absent', ['anp']],
130 ['state', 'present', ['anp']],
131 ],
132 )
133
134 schema = module.params['schema']
135 site = module.params['site']
136 template = module.params['template']
137 anp = module.params['anp']
138 state = module.params['state']
139
140 mso = MSOModule(module)
141
142 # Get schema_id
143 schema_obj = mso.get_obj('schemas', displayName=schema)
144 if not schema_obj:
145 mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema))
146
147 schema_path = 'schemas/{id}'.format(**schema_obj)
148 schema_id = schema_obj['id']
149
150 # Get site
151 site_id = mso.lookup_site(site)
152
153 # Get site_idx
154 sites = [(s['siteId'], s['templateName']) for s in schema_obj['sites']]
155 if (site_id, template) not in sites:
156 mso.fail_json(msg="Provided site/template '{0}-{1}' does not exist. Existing sites/templates: {2}".format(site, template, ', '.join(sites)))
157
158 # Schema-access uses indexes
159 site_idx = sites.index((site_id, template))
160 # Path-based access uses site_id-template
161 site_template = '{0}-{1}'.format(site_id, template)
162
163 # Get ANP
164 anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp)
165 anps = [a['anpRef'] for a in schema_obj['sites'][site_idx]['anps']]
166
167 if anp is not None and anp_ref in anps:
168 anp_idx = anps.index(anp_ref)
169 anp_path = '/sites/{0}/anps/{1}'.format(site_template, anp)
170 mso.existing = schema_obj['sites'][site_idx]['anps'][anp_idx]
171
172 if state == 'query':
173 if anp is None:
174 mso.existing = schema_obj['sites'][site_idx]['anps']
175 elif not mso.existing:
176 mso.fail_json(msg="ANP '{anp}' not found".format(anp=anp))
177 mso.exit_json()
178
179 anps_path = '/sites/{0}/anps'.format(site_template)
180 ops = []
181
182 mso.previous = mso.existing
183 if state == 'absent':
184 if mso.existing:
185 mso.sent = mso.existing = {}
186 ops.append(dict(op='remove', path=anp_path))
187
188 elif state == 'present':
189
190 payload = dict(
191 anpRef=dict(
192 schemaId=schema_id,
193 templateName=template,
194 anpName=anp,
195 ),
196 )
197
198 mso.sanitize(payload, collate=True)
199
200 if not mso.existing:
201 ops.append(dict(op='add', path=anps_path + '/-', value=mso.sent))
202
203 mso.existing = mso.proposed
204
205 if not module.check_mode:
206 mso.request(schema_path, method='PATCH', data=ops)
207
208 mso.exit_json()
209
210
211if __name__ == "__main__":
212 main()