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_template_anp.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_schema_template_anp
17short_description: Manage Application Network Profiles (ANPs) in schema templates
18description:
19- Manage ANPs in schema templates 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 template:
30 description:
31 - The name of the template.
32 type: str
33 required: yes
34 anp:
35 description:
36 - The name of the ANP to manage.
37 type: str
38 aliases: [ name ]
39 display_name:
40 description:
41 - The name as displayed on the MSO web interface.
42 type: str
43 state:
44 description:
45 - Use C(present) or C(absent) for adding or removing.
46 - Use C(query) for listing an object or multiple objects.
47 type: str
48 choices: [ absent, present, query ]
49 default: present
50seealso:
51- module: mso_schema_template
52- module: mso_schema_template_anp_epg
53extends_documentation_fragment: mso
54'''
55
56EXAMPLES = r'''
57- name: Add a new ANP
58 mso_schema_template_anp:
59 host: mso_host
60 username: admin
61 password: SomeSecretPassword
62 schema: Schema 1
63 template: Template 1
64 anp: ANP 1
65 state: present
66 delegate_to: localhost
67
68- name: Remove an ANP
69 mso_schema_template_anp:
70 host: mso_host
71 username: admin
72 password: SomeSecretPassword
73 schema: Schema 1
74 template: Template 1
75 anp: ANP 1
76 state: absent
77 delegate_to: localhost
78
79- name: Query a specific ANPs
80 mso_schema_template_anp:
81 host: mso_host
82 username: admin
83 password: SomeSecretPassword
84 schema: Schema 1
85 template: Template 1
86 state: query
87 delegate_to: localhost
88 register: query_result
89
90- name: Query all ANPs
91 mso_schema_template_anp:
92 host: mso_host
93 username: admin
94 password: SomeSecretPassword
95 schema: Schema 1
96 template: Template 1
97 state: query
98 delegate_to: localhost
99 register: query_result
100'''
101
102RETURN = r'''
103'''
104
105from ansible.module_utils.basic import AnsibleModule
106from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec, issubset
107
108
109def main():
110 argument_spec = mso_argument_spec()
111 argument_spec.update(
112 schema=dict(type='str', required=True),
113 template=dict(type='str', required=True),
114 anp=dict(type='str', aliases=['name']), # This parameter is not required for querying all objects
115 display_name=dict(type='str'),
116 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
117 )
118
119 module = AnsibleModule(
120 argument_spec=argument_spec,
121 supports_check_mode=True,
122 required_if=[
123 ['state', 'absent', ['anp']],
124 ['state', 'present', ['anp']],
125 ],
126 )
127
128 schema = module.params['schema']
129 template = module.params['template']
130 anp = module.params['anp']
131 display_name = module.params['display_name']
132 state = module.params['state']
133
134 mso = MSOModule(module)
135
136 # Get schema_id
137 schema_obj = mso.get_obj('schemas', displayName=schema)
138 if not schema_obj:
139 mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema))
140
141 schema_path = 'schemas/{id}'.format(**schema_obj)
142
143 # Get template
144 templates = [t['name'] for t in schema_obj['templates']]
145 if template not in templates:
146 mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ', '.join(templates)))
147 template_idx = templates.index(template)
148
149 # Get ANP
150 anps = [a['name'] for a in schema_obj['templates'][template_idx]['anps']]
151
152 if anp is not None and anp in anps:
153 anp_idx = anps.index(anp)
154 mso.existing = schema_obj['templates'][template_idx]['anps'][anp_idx]
155
156 if state == 'query':
157 if anp is None:
158 mso.existing = schema_obj['templates'][template_idx]['anps']
159 elif not mso.existing:
160 mso.fail_json(msg="ANP '{anp}' not found".format(anp=anp))
161 mso.exit_json()
162
163 anps_path = '/templates/{0}/anps'.format(template)
164 anp_path = '/templates/{0}/anps/{1}'.format(template, anp)
165 ops = []
166
167 mso.previous = mso.existing
168 if state == 'absent':
169 if mso.existing:
170 mso.sent = mso.existing = {}
171 ops.append(dict(op='remove', path=anp_path))
172
173 elif state == 'present':
174
175 if display_name is None and not mso.existing:
176 display_name = anp
177
178 epgs = []
179 if mso.existing:
180 epgs = None
181
182 payload = dict(
183 name=anp,
184 displayName=display_name,
185 epgs=epgs,
186 )
187
188 mso.sanitize(payload, collate=True)
189
190 if mso.existing:
191 if display_name is not None:
192 ops.append(dict(op='replace', path=anp_path + '/displayName', value=display_name))
193 else:
194 ops.append(dict(op='add', path=anps_path + '/-', value=mso.sent))
195
196 mso.existing = mso.proposed
197
198 if not module.check_mode:
199 mso.request(schema_path, method='PATCH', data=ops)
200
201 mso.exit_json()
202
203
204if __name__ == "__main__":
205 main()