Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_l3out_hsrp_group.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Shreyas Srish (@shrsr) <[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: aci_l3out_hsrp_group
16 short_description: Manage HSRP group (hsrp:GroupP) of the HSRP interface profile (hsrp:IfP)
17 description:
18 - Manage HSRP group of the HSRP interface profile on Cisco ACI fabrics.
19 options:
20 tenant:
21 description:
22 - Name of an existing tenant.
23 type: str
24 aliases: [ tenant_name ]
25 l3out:
26 description:
27 - Name of an existing L3Out.
28 type: str
29 aliases: [ l3out_name ]
30 node_profile:
31 description:
32 - Name of the node profile.
33 type: str
34 aliases: [ node_profile_name, logical_node ]
35 interface_profile:
36 description:
37 - Name of an existing interface profile.
38 type: str
39 aliases: [ interface_profile_name, logical_interface ]
40 hsrp_interface_group:
41 description:
42 - Name of the HSRP interface group.
43 type: str
44 aliases: [ name, hsrp_group ]
45 group_id:
46 description:
47 - The group id of the HSRP interface group.
48 type: int
49 ip:
50 description:
51 - The virtual IP address of the HSRP interface group.
52 type: str
53 mac:
54 description:
55 - The MAC address of the HSRP interface group.
56 type: str
57 group_name:
58 description:
59 - The group name is used to define and manage the specific HSRP interface group, facilitating high availability in the network.
60 type: str
61 description:
62 description:
63 - The description of the HSRP interface group.
64 type: str
65 aliases: [ descr ]
66 group_type:
67 description:
68 - The type of the HSRP interface group.
69 type: str
70 choices: [ ipv4, ipv6 ]
71 ip_obtain_mode:
72 description:
73 - The mode of method used to obtain the IP address.
74 type: str
75 choices: [ admin, auto, learn ]
76 group_policy:
77 description:
78 - The group policy of the HSRP interface group.
79 type: 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:
88 - cisco.aci.aci
89 - cisco.aci.annotation
90 - cisco.aci.owner
91
92 notes:
93 - The C(tenant), C(l3out), C(logical_node_profile), C(logical_interface_profile) and C(hsrp_interface_profile) must exist before using this module in
94 your playbook. The M(cisco.aci.aci_tenant), M(cisco.aci.aci_l3out), M(cisco.aci.aci_l3out_logical_node_profile),
95 M(cisco.aci.aci_l3out_logical_interface_profile) and M(cisco.aci.aci_l3out_hsrp_interface_profile) can be used for this.
96 seealso:
97 - module: cisco.aci.aci_tenant
98 - module: cisco.aci.aci_l3out
99 - module: cisco.aci.aci_l3out_logical_node_profile
100 - module: cisco.aci.aci_l3out_logical_interface_profile
101 - module: cisco.aci.aci_l3out_hsrp_interface_profile
102 - name: APIC Management Information Model reference
103 description: More information about the internal APIC class B(hsrp:IfP).
104 link: https://developer.cisco.com/docs/apic-mim-ref/
105 author:
106 - Shreyas Srish (@shrsr)
107 """
108
109 EXAMPLES = r"""
110 - name: Add a new HSRP group
111 cisco.aci.aci_l3out_hsrp_group:
112 host: apic
113 username: admin
114 password: SomeSecretPassword
115 tenant: my_tenant
116 l3out: my_l3out
117 node_profile: my_node_profile
118 interface_profile: my_interface_profile
119 hsrp_interface_group: group1
120 ip: 12.34.56.32
121 group_type: ipv4
122 ip_obtain_mode: admin
123 group_policy: default
124 state: present
125 delegate_to: localhost
126
127 - name: Delete a HSRP group
128 cisco.aci.aci_l3out_hsrp_group:
129 host: apic
130 username: admin
131 password: SomeSecretPassword
132 tenant: my_tenant
133 l3out: my_l3out
134 node_profile: my_node_profile
135 interface_profile: my_interface_profile
136 hsrp_interface_group: group1
137 ip: 12.34.56.32
138 group_type: ipv4
139 ip_obtain_mode: admin
140 group_policy: default
141 state: absent
142 delegate_to: localhost
143
144 - name: Query a HSRP group
145 cisco.aci.aci_l3out_hsrp_group:
146 host: apic
147 username: admin
148 password: SomeSecretPassword
149 tenant: my_tenant
150 l3out: my_l3out
151 node_profile: my_node_profile
152 interface_profile: my_interface_profile
153 hsrp_interface_group: group1
154 ip: 12.34.56.32
155 group_type: ipv4
156 ip_obtain_mode: admin
157 group_policy: default
158 state: query
159 delegate_to: localhost
160 register: query_result
161
162 - name: Query all HSRP groups
163 cisco.aci.aci_l3out_hsrp_group:
164 host: apic
165 username: admin
166 password: SomeSecretPassword
167 tenant: my_tenant
168 l3out: my_l3out
169 node_profile: my_node_profile
170 interface_profile: my_interface_profile
171 state: query
172 delegate_to: localhost
173 register: query_result
174 """
175
176 RETURN = r"""
177 current:
178 description: The existing configuration from the APIC after the module has finished
179 returned: success
180 type: list
181 sample:
182 [
183 {
184 "fvTenant": {
185 "attributes": {
186 "descr": "Production environment",
187 "dn": "uni/tn-production",
188 "name": "production",
189 "nameAlias": "",
190 "ownerKey": "",
191 "ownerTag": ""
192 }
193 }
194 }
195 ]
196 error:
197 description: The error information as returned from the APIC
198 returned: failure
199 type: dict
200 sample:
201 {
202 "code": "122",
203 "text": "unknown managed object class foo"
204 }
205 raw:
206 description: The raw output returned by the APIC REST API (xml or json)
207 returned: parse error
208 type: str
209 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
210 sent:
211 description: The actual/minimal configuration pushed to the APIC
212 returned: info
213 type: list
214 sample:
215 {
216 "fvTenant": {
217 "attributes": {
218 "descr": "Production environment"
219 }
220 }
221 }
222 previous:
223 description: The original configuration from the APIC before the module has started
224 returned: info
225 type: list
226 sample:
227 [
228 {
229 "fvTenant": {
230 "attributes": {
231 "descr": "Production",
232 "dn": "uni/tn-production",
233 "name": "production",
234 "nameAlias": "",
235 "ownerKey": "",
236 "ownerTag": ""
237 }
238 }
239 }
240 ]
241 proposed:
242 description: The assembled configuration from the user-provided parameters
243 returned: info
244 type: dict
245 sample:
246 {
247 "fvTenant": {
248 "attributes": {
249 "descr": "Production environment",
250 "name": "production"
251 }
252 }
253 }
254 filter_string:
255 description: The filter string used for the request
256 returned: failure or debug
257 type: str
258 sample: ?rsp-prop-include=config-only
259 method:
260 description: The HTTP method used for the request to the APIC
261 returned: failure or debug
262 type: str
263 sample: POST
264 response:
265 description: The HTTP response from the APIC
266 returned: failure or debug
267 type: str
268 sample: OK (30 bytes)
269 status:
270 description: The HTTP status from the APIC
271 returned: failure or debug
272 type: int
273 sample: 200
274 url:
275 description: The HTTP url used for the request to the APIC
276 returned: failure or debug
277 type: str
278 sample: https://10.11.12.13/api/mo/uni/tn-production.json
279 """
280
281
282 from ansible.module_utils.basic import AnsibleModule
283 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
284
285
286 def main():
287 argument_spec = aci_argument_spec()
288 argument_spec.update(aci_annotation_spec())
289 argument_spec.update(aci_owner_spec())
290 argument_spec.update(
291 tenant=dict(type="str", aliases=["tenant_name"]),
292 l3out=dict(type="str", aliases=["l3out_name"]),
293 node_profile=dict(type="str", aliases=["node_profile_name", "logical_node"]),
294 interface_profile=dict(type="str", aliases=["interface_profile_name", "logical_interface"]),
295 hsrp_interface_group=dict(type="str", aliases=["name", "hsrp_group"]),
296 group_id=dict(type="int"),
297 ip=dict(type="str"),
298 mac=dict(type="str"),
299 group_name=dict(type="str"),
300 description=dict(type="str", aliases=["descr"]),
301 group_type=dict(type="str", choices=["ipv4", "ipv6"]),
302 ip_obtain_mode=dict(type="str", choices=["admin", "auto", "learn"]),
303 group_policy=dict(type="str"),
304 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
305 )
306
307 module = AnsibleModule(
308 argument_spec=argument_spec,
309 supports_check_mode=True,
310 required_if=[
311 ["state", "absent", ["tenant", "l3out", "node_profile", "interface_profile", "hsrp_interface_group"]],
312 ["state", "present", ["tenant", "l3out", "node_profile", "interface_profile", "hsrp_interface_group"]],
313 ],
314 )
315
316 tenant = module.params.get("tenant")
317 l3out = module.params.get("l3out")
318 node_profile = module.params.get("node_profile")
319 interface_profile = module.params.get("interface_profile")
320 hsrp_interface_group = module.params.get("hsrp_interface_group")
321 group_id = module.params.get("group_id")
322 ip = module.params.get("ip")
323 mac = module.params.get("mac")
324 group_name = module.params.get("group_name")
325 description = module.params.get("description")
326 group_type = module.params.get("group_type")
327 ip_obtain_mode = module.params.get("ip_obtain_mode")
328 group_policy = module.params.get("group_policy")
329 state = module.params.get("state")
330
331 aci = ACIModule(module)
332
333 aci.construct_url(
334 root_class=dict(
335 aci_class="fvTenant",
336 aci_rn="tn-{0}".format(tenant),
337 module_object=tenant,
338 target_filter={"name": tenant},
339 ),
340 subclass_1=dict(
341 aci_class="l3extOut",
342 aci_rn="out-{0}".format(l3out),
343 module_object=l3out,
344 target_filter={"name": l3out},
345 ),
346 subclass_2=dict(
347 aci_class="l3extLNodeP",
348 aci_rn="lnodep-{0}".format(node_profile),
349 module_object=node_profile,
350 target_filter={"name": node_profile},
351 ),
352 subclass_3=dict(
353 aci_class="l3extLIfP",
354 aci_rn="lifp-[{0}]".format(interface_profile),
355 module_object=interface_profile,
356 target_filter={"name": interface_profile},
357 ),
358 subclass_4=dict(
359 aci_class="hsrpIfP",
360 aci_rn="hsrpIfP",
361 module_object=interface_profile,
362 target_filter={"name": interface_profile},
363 ),
364 subclass_5=dict(
365 aci_class="hsrpGroupP",
366 aci_rn="hsrpGroupP-{0}".format(hsrp_interface_group),
367 module_object=hsrp_interface_group,
368 target_filter={"name": hsrp_interface_group},
369 ),
370 child_classes=["hsrpRsGroupPol"],
371 )
372
373 aci.get_existing()
374
375 if state == "present":
376 aci.payload(
377 aci_class="hsrpGroupP",
378 class_config=dict(groupAf=group_type, groupId=group_id, groupName=group_name, ip=ip, ipObtainMode=ip_obtain_mode, mac=mac, descr=description),
379 child_configs=[dict(hsrpRsGroupPol=dict(attributes=dict(tnHsrpGroupPolName=group_policy)))] if group_policy is not None else [],
380 )
381
382 aci.get_diff(aci_class="hsrpGroupP")
383
384 aci.post_config()
385
386 elif state == "absent":
387 aci.delete_config()
388
389 aci.exit_json()
390
391
392 if __name__ == "__main__":
393 main()