Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2025, Tim Cragg (@timcragg) <[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_management_network_instance_profile
16 version_added: "2.13.0"
17 short_description: Manage external management network instance profiles (mgmt:InstP).
18 description:
19 - Manage external management network instance profiles on Cisco ACI fabrics.
20 options:
21 profile:
22 description:
23 - The name of the external management network instance profile.
24 type: str
25 aliases: [ name, profile_name ]
26 qos_class:
27 description:
28 - QoS priority class identifier.
29 - The APIC defaults to C(unspecified) when unset during creation.
30 type: str
31 aliases: [ qos, priority, prio ]
32 choices: [ level1, level2, level3, level4, level5, level6, unspecified ]
33 description:
34 description:
35 - The description for the external management network instance profile.
36 type: str
37 aliases: [ descr ]
38 subnets:
39 description:
40 - The list of subnets in CIDR format to associate with the external management network instance profile.
41 - When state is C(present) and a list of subnets is provided, any existing subnets will be removed from the object if they are not present in this list.
42 - When subnets is set to an empty list and state is C(present), all existing subnets will be removed from the profile.
43 type: list
44 elements: str
45 aliases: [ subnet_list, networks, network_list ]
46 state:
47 description:
48 - Use C(present) or C(absent) for adding or removing.
49 - Use C(query) for listing an object or multiple objects.
50 type: str
51 choices: [ absent, present, query ]
52 default: present
53 name_alias:
54 description:
55 - The alias for the current object. This relates to the nameAlias field in ACI.
56 type: str
57 extends_documentation_fragment:
58 - cisco.aci.aci
59 - cisco.aci.annotation
60
61 seealso:
62 - name: APIC Management Information Model reference
63 description: More information about the internal APIC class B(mgmt:InstP).
64 link: https://developer.cisco.com/docs/apic-mim-ref/
65 author:
66 - Tim Cragg (@timcragg)
67 """
68
69 EXAMPLES = r"""
70 - name: Add a new external management network instance profile
71 cisco.aci.aci_management_network_instance_profile:
72 host: apic
73 username: admin
74 password: SomeSecretPassword
75 name: lab_network_inst_profile
76 subnets:
77 - 10.20.30.0/24
78 - 192.168.10.0/24
79 state: present
80 delegate_to: localhost
81
82 - name: Delete all subnets from management network instance profile
83 cisco.aci.aci_management_network_instance_profile:
84 host: apic
85 username: admin
86 password: SomeSecretPassword
87 name: lab_network_inst_profile
88 subnets: []
89 state: present
90 delegate_to: localhost
91
92 - name: Remove an external management network instance profile
93 cisco.aci.aci_management_network_instance_profile:
94 host: apic
95 username: admin
96 password: SomeSecretPassword
97 name: lab_network_inst_profile
98 state: absent
99 delegate_to: localhost
100
101 - name: Query an external management network instance profile
102 cisco.aci.aci_management_network_instance_profile:
103 host: apic
104 username: admin
105 password: SomeSecretPassword
106 name: lab_network_inst_profile
107 state: query
108 delegate_to: localhost
109 register: query_result
110
111 - name: Query all external management network instance profiles
112 cisco.aci.aci_management_network_instance_profile:
113 host: apic
114 username: admin
115 password: SomeSecretPassword
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
228 from ansible_collections.cisco.aci.plugins.module_utils.constants import VALID_QOS_CLASSES
229
230
231 def main():
232 argument_spec = aci_argument_spec()
233 argument_spec.update(aci_annotation_spec())
234 argument_spec.update(
235 profile=dict(type="str", aliases=["name", "profile_name"]),
236 qos_class=dict(type="str", aliases=["qos", "priority", "prio"], choices=VALID_QOS_CLASSES),
237 description=dict(type="str", aliases=["descr"]),
238 subnets=dict(type="list", elements="str", aliases=["subnet_list", "networks", "network_list"]),
239 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
240 name_alias=dict(type="str"),
241 )
242
243 module = AnsibleModule(
244 argument_spec=argument_spec,
245 supports_check_mode=True,
246 required_if=[
247 ["state", "absent", ["profile"]],
248 ["state", "present", ["profile"]],
249 ],
250 )
251
252 profile = module.params.get("profile")
253 qos_class = module.params.get("qos_class")
254 description = module.params.get("description")
255 subnets = module.params.get("subnets")
256 state = module.params.get("state")
257 name_alias = module.params.get("name_alias")
258
259 aci = ACIModule(module)
260 aci.construct_url(
261 root_class=dict(
262 aci_class="mgmtInstP",
263 aci_rn="tn-mgmt/extmgmt-default/instp-{0}".format(profile),
264 module_object=profile,
265 target_filter={"name": profile},
266 ),
267 child_classes=["mgmtSubnet"],
268 )
269
270 aci.get_existing()
271
272 if state == "present":
273 child_configs = []
274 if subnets is not None:
275 for subnet in subnets:
276 child_configs.append(dict(mgmtSubnet=dict(attributes=dict(ip=subnet))))
277 if isinstance(aci.existing, list) and len(aci.existing) > 0:
278 for child in aci.existing[0].get("mgmtInstP", {}).get("children", []):
279 # Remove any existing subnet entries that are not in the requested subnet list
280 if child.get("mgmtSubnet") and child.get("mgmtSubnet").get("attributes", {}).get("ip") not in subnets:
281 child_configs.append(
282 {
283 "mgmtSubnet": {
284 "attributes": {
285 "ip": child.get("mgmtSubnet").get("attributes", {}).get("ip"),
286 "status": "deleted",
287 }
288 }
289 }
290 )
291
292 aci.payload(
293 aci_class="mgmtInstP",
294 class_config=dict(
295 descr=description,
296 name=profile,
297 nameAlias=name_alias,
298 prio=qos_class,
299 ),
300 child_configs=child_configs,
301 )
302
303 aci.get_diff(aci_class="mgmtInstP")
304
305 aci.post_config()
306
307 elif state == "absent":
308 aci.delete_config()
309
310 aci.exit_json()
311
312
313 if __name__ == "__main__":
314 main()