Debian Codegraph @

ansible_collections/cisco/aci/docs/sample_module/aci_module_template.md

raw
1 ```python
2
3 #!/usr/bin/python
4 # -*- coding: utf-8 -*-
5
6 # Copyright: (c) <year>, <author_name> (@author_github_handle)
7 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
8
9 from __future__ import absolute_import, division, print_function
10
11 __metaclass__ = type
12
13 ANSIBLE_METADATA = {
14 "metadata_version": "1.1",
15 "status": ["preview"],
16 "supported_by": "community",
17 }
18
19 DOCUMENTATION = r"""
20 ---
21 module: aci_<name_of_module> # Replace <module_name> with aci_l3out_interface
22 short_description: Short description of the module being created (config:<name_of_class>). # Replace (config:<name_of_class>) with config value and name of the class based on the module requirements Ex: (l3ext:RsPathL3OutAtt)
23 description:
24 - Manages <functionality> on Cisco ACI fabrics.
25 - This module is only available for APIC version x.y and above.
26 options:
27 object_id:
28 description:
29 - Description of the object.
30 type: Data type of object eg. 'str'
31 aliases: [ Alternate name of the object ]
32 object_prop1:
33 description:
34 - Description of property one.
35 type: Property's data type eg. 'int'
36 choices: [ choice one, choice two ]
37 object_prop2:
38 description:
39 - Description of property two.
40 - This attribute is only configurable in ACI versions 6.0(2h) and above.
41 type: Property's data type eg. 'bool'
42 object_prop3:
43 description:
44 - Description of property three.
45 - The APIC defaults to C(default_value) when unset during creation.
46 - The object_prop3 is only applicable when using 'object_prop2' is set to <specific_value>.
47 - The object_prop3 must be in the range 1 to 100. The default value is 50.
48 type: Property's data type eg. 'str'
49 required: true
50 child_object_prop:
51 description:
52 - Description of the child class object property
53 - This is required for child class object B(config:<name_of_child_class>)
54 state:
55 description:
56 - Use C(present) or C(absent) for adding or removing.
57 - Use C(query) for listing an object or multiple objects.
58 type: str
59 choices: [ absent, present, query ]
60 default: present
61 extends_documentation_fragment:
62 - cisco.aci.aci
63 - cisco.aci.annotation
64 - cisco.aci.owner
65
66 notes:
67 - The C(root_object), C(parent_object), C(object_prop), used must exist before using this module in your playbook.
68 The M(cisco.aci.aci_root_object_module) and M(cisco.aci.parent_object_module) modules can be used for this.
69 # Change the above lines with respect to the object in the module.
70 # For example for (l3ext:RsPathL3OutAtt) object
71 # - The C(tenant), C(l3out), C(logical_node), C(prefix), C(node_id) and C(pod_id) used must exist before using this module in your playbook.
72 # The M(cisco.aci.aci_tenant) and M(cisco.aci.aci_l3out) modules can be used for this.
73
74 seealso:
75 - module: cisco.aci.aci_root_object_module
76 - module: cisco.aci.aci_parent_object_module
77 # change the module values with required module names for the object
78 # For example for (l3ext:RsPathL3OutAtt) object
79 # - module: cisco.aci.aci_tenant
80 # - module: cisco.aci.aci_l3out
81 - name: APIC Management Information Model reference
82 description: More information about the internal APIC class B(config:<name_of_class>). # for example change B(config:<name_of_class>) to B(l3ext:RsPathL3OutAtt)
83 link: https://developer.cisco.com/docs/apic-mim-ref/
84 author:
85 - <author's name> (<author's github id>) # replace the author's name and github id
86
87 """
88
89 EXAMPLES = r"""
90 - name: Add a new object # Always follow the same order -> Add, Query, Query all, Remove
91 cisco.aci.aci_<name_of_module>:
92 host: apic
93 username: admin
94 password: SomeSecretePassword
95 object_id: id
96 object_prop1: prop1
97 object_prop2: prop2
98 state: present
99 delegate_to: localhost
100
101 - name: Query an object
102 cisco.aci.aci_<name_of_module>:
103 host: apic
104 username: admin
105 password: SomeSecretePassword
106 object_id: id
107 state: query
108 delegate_to: localhost
109
110 - name: Query all objects
111 cisco.aci.aci_<name_of_module>:
112 host: apic
113 username: admin
114 password: SomeSecretePassword
115 state: query
116 delegate_to: localhost
117
118 - name: Remove an object
119 cisco.aci.aci_<name_of_module>:
120 host: apic
121 username: admin
122 password: SomeSecretePassword
123 object_id: id
124 state: absent
125 delegate_to: localhost
126 """
127
128 RETURN = r"""
129 current:
130 description: The existing configuration from the APIC after the module has finished
131 returned: success
132 type: list
133 sample:
134 [
135 {
136 "fvTenant": {
137 "attributes": {
138 "descr": "Production environment",
139 "dn": "uni/tn-production",
140 "name": "production",
141 "nameAlias": "",
142 "ownerKey": "",
143 "ownerTag": ""
144 }
145 }
146 }
147 ]
148 error:
149 description: The error information as returned from the APIC
150 returned: failure
151 type: dict
152 sample:
153 {
154 "code": "122",
155 "text": "unknown managed object class foo"
156 }
157 raw:
158 description: The raw output returned by the APIC REST API (xml or json)
159 returned: parse error
160 type: str
161 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class "/></imdata>'
162 sent:
163 description: The actual/minimal configuration pushed to the APIC
164 returned: info
165 type: list
166 sample:
167 {
168 "fvTenant": {
169 "attributes": {
170 "descr": "Production environment"
171 }
172 }
173 }
174 previous:
175 description: The original configuration from the APIC before the module has started
176 returned: info
177 type: list
178 sample:
179 [
180 {
181 "fvTenant": {
182 "attributes": {
183 "descr": "Production",
184 "dn": "uni/tn-production",
185 "name": "production",
186 "nameAlias": "",
187 "ownerKey": "",
188 "ownerTag": ""
189 }
190 }
191 }
192 ]
193 proposed:
194 description: The assembled configuration from the user-provided parameters
195 returned: info
196 type: dict
197 sample:
198 {
199 "fvTenant": {
200 "attributes": {
201 "descr": "Production environment",
202 "name": "production"
203 }
204 }
205 }
206 filter_string:
207 description: The filter string used for the request
208 returned: failure or debug
209 type: str
210 sample: ?rsp-prop-include=config-only
211 method:
212 description: The HTTP method used for the request to the APIC
213 returned: failure or debug
214 type: str
215 sample: POST
216 response:
217 description: The HTTP response from the APIC
218 returned: failure or debug
219 type: str
220 sample: OK (30 bytes)
221 status:
222 description: The HTTP status from the APIC
223 returned: failure or debug
224 type: int
225 sample: 200
226 url:
227 description: The HTTP url used for the request to the APIC
228 returned: failure or debug
229 type: str
230 sample: https://10.11.12.13/api/mo/uni/tn-production.json
231 """
232
233 from ansible.module_utils.basic import AnsibleModule
234 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec # use aci_annotation_spec, aci_owner_spec only if supported by the module
235
236
237 def main():
238 argument_spec = aci_argument_spec()
239 argument_spec.update(aci_annotation_spec()) # use only if the module supports annotations
240 argument_spec.update(aci_owner_spec()) # use only if the module supports owner
241 argument_spec.update( # This section should contain all the parameters defined in the module's documentation
242 object_id=dict(type='str', aliases=['name']),
243 object_prop1=dict(type='str'),
244 object_prop2=dict(type='str', choices=['choice1', 'choice2', 'choice3']),
245 object_prop3=dict(type='int'),
246 parent_id=dict(type='str'),
247 child_object_id=dict(type='str'),
248 child_object_prop=dict(type='str'),
249 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
250 )
251
252 module = AnsibleModule(
253 argument_spec=argument_spec,
254 supports_check_mode=True,
255 required_if=[
256 ['state', 'absent', ['object_id', 'parent_id']],
257 ['state', 'present', ['object_id', 'parent_id']],
258 ],
259 )
260
261 object_id = module.params.get('object_id')
262 object_prop1 = module.params.get('object_prop1')
263 object_prop2 = module.params.get('object_prop2')
264 object_prop3 = module.params.get('object_prop3')
265 if object_prop3 is not None and object_prop3 not in range(x, y):
266 module.fail_json(msg='Valid object_prop3 values are between x and (y-1)')
267 child_object_id = module.params.get('child_object_id')
268 child_object_prop = module.params.get('child_object_prop')
269 state = module.params.get("state")
270
271 aci = ACIModule(module)
272
273 aci.construct_url(
274 root_class=dict(
275 aci_class='fvTenant',
276 aci_rn='tn-{0}'.format(tenant),
277 module_object=tenant,
278 target_filter={'name': tenant}
279 ),
280 subclass_1=dict(
281 aci_class='<object_class>',
282 aci_rn='<object_key>-{0}'.format(object_id),
283 module_object=object_id,
284 target_filter={'name': object_id}
285 )
286 )
287 # If "dn" = "uni/tn-ansible_tenant/out-ansible_l3out/lnodep-ansible_node_profile/", the subclass_1 will look like below:
288 # subclass_1=dict(
289 # aci_class='l3extOut',
290 # aci_rn='out-{0}'.format(l3out),
291 # module_object=l3out,
292 # target_filter={'name': l3out}
293 # )
294 #
295 # followed by subclass_2 for lnodep- and child_classes if required.
296 aci.get_existing()
297
298 if state == "present":
299 aci.payload(
300 aci_class='<object APIC class>', # Replace <object APIC class> with the actual APIC class name, e.g., 'l3extOut'
301 class_config=dict(
302 name=object_id,
303 prop1=object_prop1,
304 prop2=object_prop2,
305 prop3=object_prop3,
306 ),
307 child_configs=[
308 dict(
309 '<child APIC class>'=dict( # Replace <child APIC class> with the actual child APIC class name, e.g., 'hsrpRsIfPol'
310 attributes=dict(
311 child_key=child_object_id,
312 child_prop=child_object_prop
313 ),
314 ),
315 ),
316 ],
317 )
318
319 aci.get_diff(aci_class='<object APIC class>') # Replace <object APIC class> with the actual APIC class name, e.g., 'l3extOut'
320
321 aci.post_config()
322
323 elif state == "absent":
324 aci.delete_config()
325
326 aci.exit_json()
327
328
329 if __name__ == "__main__":
330 main()
331
332 ```