Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_interface_policy_hsrp.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": "certified"}
12
13 DOCUMENTATION = r"""
14 ---
15 module: aci_interface_policy_hsrp
16 short_description: Manage HSRP interface policies (hsrp:IfPol)
17 description:
18 - Manage HSRP interface policies on Cisco ACI fabrics.
19 options:
20 tenant:
21 description:
22 - The name of the Tenant the HSRP interface policy should belong to.
23 type: str
24 aliases: [ tenant_name ]
25 hsrp:
26 description:
27 - The HSRP interface policy name.
28 type: str
29 aliases: [ hsrp_interface, name ]
30 description:
31 description:
32 - The description of the HSRP interface.
33 type: str
34 aliases: [ descr ]
35 controls:
36 description:
37 - The interface policy controls.
38 type: list
39 elements: str
40 choices: [ bfd, bia ]
41 delay:
42 description:
43 - The administrative port delay of HSRP interface policy.
44 - This is only valid in range between 1 and 10000.
45 type: int
46 reload_delay:
47 description:
48 - The option for reload delay of HSRP interface policy.
49 - This is only valid in range between 1 and 10000.
50 type: int
51 state:
52 description:
53 - Use C(present) or C(absent) for adding or removing.
54 - Use C(query) for listing an object or multiple objects.
55 type: str
56 choices: [ absent, present, query ]
57 default: present
58 extends_documentation_fragment:
59 - cisco.aci.aci
60 - cisco.aci.annotation
61 - cisco.aci.owner
62
63 notes:
64 - The C(tenant) must exist before using this module in your playbook.
65 The M(cisco.aci.aci_tenant) can be used for this.
66 seealso:
67 - module: cisco.aci.aci_tenant
68 - name: APIC Management Information Model reference
69 description: More information about the internal APIC class B(hsrp:IfPol).
70 link: https://developer.cisco.com/docs/apic-mim-ref/
71 author:
72 - Shreyas Srish (@shrsr)
73 """
74
75 EXAMPLES = r"""
76 - name: Add a HSRP interface policy
77 cisco.aci.aci_interface_policy_hsrp:
78 host: apic
79 username: admin
80 password: SomeSecretPassword
81 tenant: production
82 hsrp: hsrp1
83 controls: bfd
84 delay: 50
85 reload_delay: 100
86 state: present
87 delegate_to: localhost
88
89 - name: Delete a HSRP interface policy
90 cisco.aci.aci_interface_policy_hsrp:
91 host: apic
92 username: admin
93 password: SomeSecretPassword
94 tenant: production
95 hsrp: hsrp1
96 state: absent
97 delegate_to: localhost
98
99 - name: Query a HSRP interface policy
100 cisco.aci.aci_interface_policy_hsrp:
101 host: apic
102 username: admin
103 password: SomeSecretPassword
104 tenant: production
105 hsrp: hsrp1
106 state: query
107 delegate_to: localhost
108 register: query_result
109
110 - name: Query all HSRP interface policies in tenant production
111 cisco.aci.aci_interface_policy_hsrp:
112 host: apic
113 username: admin
114 password: SomeSecretPassword
115 tenant: production
116 state: query
117 delegate_to: localhost
118 register: query_result
119 """
120
121 RETURN = r"""
122 current:
123 description: The existing configuration from the APIC after the module has finished
124 returned: success
125 type: list
126 sample:
127 [
128 {
129 "fvTenant": {
130 "attributes": {
131 "descr": "Production environment",
132 "dn": "uni/tn-production",
133 "name": "production",
134 "nameAlias": "",
135 "ownerKey": "",
136 "ownerTag": ""
137 }
138 }
139 }
140 ]
141 error:
142 description: The error information as returned from the APIC
143 returned: failure
144 type: dict
145 sample:
146 {
147 "code": "122",
148 "text": "unknown managed object class foo"
149 }
150 raw:
151 description: The raw output returned by the APIC REST API (xml or json)
152 returned: parse error
153 type: str
154 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
155 sent:
156 description: The actual/minimal configuration pushed to the APIC
157 returned: info
158 type: list
159 sample:
160 {
161 "fvTenant": {
162 "attributes": {
163 "descr": "Production environment"
164 }
165 }
166 }
167 previous:
168 description: The original configuration from the APIC before the module has started
169 returned: info
170 type: list
171 sample:
172 [
173 {
174 "fvTenant": {
175 "attributes": {
176 "descr": "Production",
177 "dn": "uni/tn-production",
178 "name": "production",
179 "nameAlias": "",
180 "ownerKey": "",
181 "ownerTag": ""
182 }
183 }
184 }
185 ]
186 proposed:
187 description: The assembled configuration from the user-provided parameters
188 returned: info
189 type: dict
190 sample:
191 {
192 "fvTenant": {
193 "attributes": {
194 "descr": "Production environment",
195 "name": "production"
196 }
197 }
198 }
199 filter_string:
200 description: The filter string used for the request
201 returned: failure or debug
202 type: str
203 sample: ?rsp-prop-include=config-only
204 method:
205 description: The HTTP method used for the request to the APIC
206 returned: failure or debug
207 type: str
208 sample: POST
209 response:
210 description: The HTTP response from the APIC
211 returned: failure or debug
212 type: str
213 sample: OK (30 bytes)
214 status:
215 description: The HTTP status from the APIC
216 returned: failure or debug
217 type: int
218 sample: 200
219 url:
220 description: The HTTP url used for the request to the APIC
221 returned: failure or debug
222 type: str
223 sample: https://10.11.12.13/api/mo/uni/tn-production.json
224 """
225
226 from ansible.module_utils.basic import AnsibleModule
227 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
228
229
230 def main():
231 argument_spec = aci_argument_spec()
232 argument_spec.update(aci_annotation_spec())
233 argument_spec.update(aci_owner_spec())
234 argument_spec.update(
235 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
236 hsrp=dict(type="str", aliases=["hsrp_interface", "name"]), # Not required for querying all objects
237 description=dict(type="str", aliases=["descr"]),
238 controls=dict(type="list", elements="str", choices=["bfd", "bia"]),
239 reload_delay=dict(type="int"),
240 delay=dict(type="int"),
241 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
242 )
243
244 module = AnsibleModule(
245 argument_spec=argument_spec,
246 supports_check_mode=True,
247 required_if=[
248 ["state", "absent", ["tenant", "hsrp"]],
249 ["state", "present", ["tenant", "hsrp"]],
250 ],
251 )
252
253 aci = ACIModule(module)
254
255 tenant = module.params.get("tenant")
256 hsrp = module.params.get("hsrp")
257 description = module.params.get("description")
258 controls = ",".join(module.params.get("controls")) if module.params.get("controls") else None
259
260 reload_delay = module.params.get("reload_delay")
261 if reload_delay is not None and reload_delay not in range(1, 10000):
262 module.fail_json(msg="Parameter 'reload_delay' is only valid in range between 1 and 10000.")
263
264 delay = module.params.get("delay")
265 if delay is not None and delay not in range(1, 10000):
266 module.fail_json(msg="Parameter 'delay' is only valid in range between 1 and 10000.")
267
268 state = module.params.get("state")
269
270 aci.construct_url(
271 root_class=dict(
272 aci_class="fvTenant",
273 aci_rn="tn-{0}".format(tenant),
274 module_object=tenant,
275 target_filter={"name": tenant},
276 ),
277 subclass_1=dict(
278 aci_class="hsrpIfPol",
279 aci_rn="hsrpIfPol-{0}".format(hsrp),
280 module_object=hsrp,
281 target_filter={"name": hsrp},
282 ),
283 )
284
285 aci.get_existing()
286
287 if state == "present":
288 aci.payload(
289 aci_class="hsrpIfPol",
290 class_config=dict(
291 name=hsrp,
292 descr=description,
293 ctrl=controls,
294 reloadDelay=reload_delay,
295 delay=delay,
296 ),
297 )
298
299 aci.get_diff(aci_class="hsrpIfPol")
300
301 aci.post_config()
302
303 elif state == "absent":
304 aci.delete_config()
305
306 aci.exit_json()
307
308
309 if __name__ == "__main__":
310 main()