Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2025, Samita Bhattacharjee (@samiib)
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 = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: aci_switch_fabric_config
20 version_added: "2.13.0"
21 short_description: Manage Switch Fabric Policy Configuration of Leaf and Spine nodes (fabric:NodeConfig).
22 description:
23 - Manage Switch Fabric Policy Configuration of Leaf and Spine nodes (fabric:NodeConfig) on Cisco ACI fabrics.
24 - This module is only available for APIC version 6.0 and above.
25 options:
26 node_type:
27 description:
28 - The type of Node.
29 type: str
30 aliases: [ type, switch_type ]
31 choices: [ leaf, spine ]
32 node:
33 description:
34 - The ID of the Node.
35 - The value must be between 101 to 4000.
36 type: int
37 aliases: [ node_id ]
38 policy_group:
39 description:
40 - The name of the Leaf/Spine Fabric Policy Group to associate with the node.
41 - The Fabric Policy Group must exist for the settings to be applied.
42 type: str
43 aliases: [ fabric_policy_group, fabric_policy ]
44 state:
45 description:
46 - Use C(present) or C(absent) for adding or removing.
47 - Use C(query) for listing an object or multiple objects.
48 type: str
49 choices: [ absent, present, query ]
50 default: present
51 extends_documentation_fragment:
52 - cisco.aci.aci
53 - cisco.aci.annotation
54
55 seealso:
56 - module: cisco.aci.aci_fabric_switch_policy_group
57 - name: APIC Management Information Model reference
58 description: More information about the internal APIC class B(fabric:NodeConfig).
59 link: https://developer.cisco.com/docs/apic-mim-ref/
60 author:
61 - Samita Bhattacharjee (@samiib)
62 """
63
64 EXAMPLES = r"""
65 - name: Add Switch Fabric Policy Configuration to a Leaf node
66 cisco.aci.aci_switch_fabric_config:
67 host: apic
68 username: admin
69 password: SomeSecretPassword
70 node: 101
71 node_type: leaf
72 policy_group: ansible_leaf_fabric_policy
73 state: present
74 delegate_to: localhost
75
76 - name: Query Switch Fabric Policy Configuration for a specific node
77 cisco.aci.aci_switch_fabric_config:
78 host: apic
79 username: admin
80 password: SomeSecretPassword
81 node: 101
82 state: query
83 delegate_to: localhost
84 register: query_result
85
86 - name: Query all Switch Fabric Policy Configurations
87 cisco.aci.aci_switch_fabric_config:
88 host: apic
89 username: admin
90 password: SomeSecretPassword
91 state: query
92 delegate_to: localhost
93 register: query_result
94
95 - name: Remove a Switch Fabric Policy Configuration
96 cisco.aci.aci_switch_fabric_config:
97 host: apic
98 username: admin
99 password: SomeSecretPassword
100 node: 101
101 state: absent
102 delegate_to: localhost
103 """
104
105 RETURN = r"""
106 current:
107 description: The existing configuration from the APIC after the module has finished
108 returned: success
109 type: list
110 sample:
111 [
112 {
113 "fvTenant": {
114 "attributes": {
115 "descr": "Production environment",
116 "dn": "uni/tn-production",
117 "name": "production",
118 "nameAlias": "",
119 "ownerKey": "",
120 "ownerTag": ""
121 }
122 }
123 }
124 ]
125 error:
126 description: The error information as returned from the APIC
127 returned: failure
128 type: dict
129 sample:
130 {
131 "code": "122",
132 "text": "unknown managed object class foo"
133 }
134 raw:
135 description: The raw output returned by the APIC REST API (xml or json)
136 returned: parse error
137 type: str
138 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class "/></imdata>'
139 sent:
140 description: The actual/minimal configuration pushed to the APIC
141 returned: info
142 type: list
143 sample:
144 {
145 "fvTenant": {
146 "attributes": {
147 "descr": "Production environment"
148 }
149 }
150 }
151 previous:
152 description: The original configuration from the APIC before the module has started
153 returned: info
154 type: list
155 sample:
156 [
157 {
158 "fvTenant": {
159 "attributes": {
160 "descr": "Production",
161 "dn": "uni/tn-production",
162 "name": "production",
163 "nameAlias": "",
164 "ownerKey": "",
165 "ownerTag": ""
166 }
167 }
168 }
169 ]
170 proposed:
171 description: The assembled configuration from the user-provided parameters
172 returned: info
173 type: dict
174 sample:
175 {
176 "fvTenant": {
177 "attributes": {
178 "descr": "Production environment",
179 "name": "production"
180 }
181 }
182 }
183 filter_string:
184 description: The filter string used for the request
185 returned: failure or debug
186 type: str
187 sample: ?rsp-prop-include=config-only
188 method:
189 description: The HTTP method used for the request to the APIC
190 returned: failure or debug
191 type: str
192 sample: POST
193 response:
194 description: The HTTP response from the APIC
195 returned: failure or debug
196 type: str
197 sample: OK (30 bytes)
198 status:
199 description: The HTTP status from the APIC
200 returned: failure or debug
201 type: int
202 sample: 200
203 url:
204 description: The HTTP url used for the request to the APIC
205 returned: failure or debug
206 type: str
207 sample: https://10.11.12.13/api/mo/uni/tn-production.json
208 """
209
210 from ansible.module_utils.basic import AnsibleModule
211 from ansible_collections.cisco.aci.plugins.module_utils.aci import (
212 ACIModule,
213 aci_argument_spec,
214 aci_annotation_spec,
215 switch_config_spec,
216 )
217 from ansible_collections.cisco.aci.plugins.module_utils.constants import (
218 SWITCH_CONFIG_FORMAT_MAP,
219 )
220
221
222 def main():
223 moClass = "fabricNodeConfig"
224 argument_spec = aci_argument_spec()
225 argument_spec.update(aci_annotation_spec())
226 argument_spec.update(switch_config_spec(SWITCH_CONFIG_FORMAT_MAP[moClass]["type"]))
227 module = AnsibleModule(
228 argument_spec=argument_spec,
229 supports_check_mode=True,
230 required_if=[
231 ["state", "absent", ["node"]],
232 ["state", "present", ["node", "node_type", "policy_group"]],
233 ],
234 )
235
236 node = module.params.get("node")
237 node_type = module.params.get("node_type")
238 policy_group = module.params.get("policy_group")
239 state = module.params.get("state")
240
241 aci = ACIModule(module)
242 aci.construct_url(
243 root_class=dict(
244 aci_class=moClass,
245 target_filter=dict(node=node),
246 aci_rn=SWITCH_CONFIG_FORMAT_MAP[moClass]["rn"].format(node),
247 ),
248 )
249
250 aci.get_existing()
251
252 if state == "present":
253 aci.payload(
254 aci_class=moClass,
255 class_config=dict(
256 node=node,
257 assocGrp=SWITCH_CONFIG_FORMAT_MAP[moClass][node_type].format(policy_group),
258 ),
259 )
260
261 aci.get_diff(aci_class=moClass)
262
263 aci.post_config()
264
265 elif state == "absent":
266 aci.delete_config()
267
268 aci.exit_json()
269
270
271 if __name__ == "__main__":
272 main()