Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2025, Sabari Jaganathan (@sajagana)
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_fabric_node_decommission
16 version_added: "2.13.0"
17 short_description: Manage the Commissioning and Decommissioning of the Fabric Node (fabric:RsDecommissionNode)
18 description:
19 - Manage the Commissioning and Decommissioning of the Fabric Node on Cisco ACI fabrics.
20 options:
21 pod_id:
22 description:
23 - The Pod ID for the Fabric Node.
24 type: int
25 node_id:
26 description:
27 - The Node ID for the Fabric Node.
28 type: int
29 remove_from_controller:
30 description:
31 - Completely deletes the node configuration from the controller during decommissioning.
32 - To register the fabric node, use M(cisco.aci.aci_fabric_node).
33 - The APIC defaults to O(remove_from_controller=false) when unset during creation.
34 type: bool
35 state:
36 description:
37 - Use C(present) for decommissioning the Fabric Node.
38 - Use C(absent) for commissioning the Fabric Node.
39 - Use C(query) for listing an object or multiple objects.
40 type: str
41 choices: [ absent, present, query ]
42 default: present
43 extends_documentation_fragment:
44 - cisco.aci.aci
45 - cisco.aci.annotation
46 - cisco.aci.owner
47
48 seealso:
49 - name: APIC Management Information Model reference
50 description: More information about the internal APIC class B(fabric:RsDecommissionNode).
51 link: https://developer.cisco.com/docs/apic-mim-ref/
52 author:
53 - Sabari Jaganathan (@sajagana)
54 """
55
56 EXAMPLES = r"""
57 - name: Decommission the fabric node
58 cisco.aci.aci_fabric_node_decommission:
59 host: apic
60 username: admin
61 password: SomeSecretPassword
62 pod_id: 1
63 node_id: 201
64 state: present
65
66 - name: Query the decommissioned fabric node
67 cisco.aci.aci_fabric_node_decommission:
68 host: apic
69 username: admin
70 password: SomeSecretPassword
71 pod_id: 1
72 node_id: 201
73 state: query
74 register: query_one
75
76 - name: Query all decommissioned fabric nodes
77 cisco.aci.aci_fabric_node_decommission:
78 host: apic
79 username: admin
80 password: SomeSecretPassword
81 state: query
82 register: query_all
83
84 - name: Commission the fabric node
85 cisco.aci.aci_fabric_node_decommission:
86 host: apic
87 username: admin
88 password: SomeSecretPassword
89 pod_id: 1
90 node_id: 201
91 state: absent
92 """
93
94 RETURN = r"""
95 current:
96 description: The existing configuration from the APIC after the module has finished
97 returned: success
98 type: list
99 sample:
100 [
101 {
102 "fvTenant": {
103 "attributes": {
104 "descr": "Production environment",
105 "dn": "uni/tn-production",
106 "name": "production",
107 "nameAlias": "",
108 "ownerKey": "",
109 "ownerTag": ""
110 }
111 }
112 }
113 ]
114 error:
115 description: The error information as returned from the APIC
116 returned: failure
117 type: dict
118 sample:
119 {
120 "code": "122",
121 "text": "unknown managed object class foo"
122 }
123 raw:
124 description: The raw output returned by the APIC REST API (xml or json)
125 returned: parse error
126 type: str
127 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class "/></imdata>'
128 sent:
129 description: The actual/minimal configuration pushed to the APIC
130 returned: info
131 type: list
132 sample:
133 {
134 "fvTenant": {
135 "attributes": {
136 "descr": "Production environment"
137 }
138 }
139 }
140 previous:
141 description: The original configuration from the APIC before the module has started
142 returned: info
143 type: list
144 sample:
145 [
146 {
147 "fvTenant": {
148 "attributes": {
149 "descr": "Production",
150 "dn": "uni/tn-production",
151 "name": "production",
152 "nameAlias": "",
153 "ownerKey": "",
154 "ownerTag": ""
155 }
156 }
157 }
158 ]
159 proposed:
160 description: The assembled configuration from the user-provided parameters
161 returned: info
162 type: dict
163 sample:
164 {
165 "fvTenant": {
166 "attributes": {
167 "descr": "Production environment",
168 "name": "production"
169 }
170 }
171 }
172 filter_string:
173 description: The filter string used for the request
174 returned: failure or debug
175 type: str
176 sample: ?rsp-prop-include=config-only
177 method:
178 description: The HTTP method used for the request to the APIC
179 returned: failure or debug
180 type: str
181 sample: POST
182 response:
183 description: The HTTP response from the APIC
184 returned: failure or debug
185 type: str
186 sample: OK (30 bytes)
187 status:
188 description: The HTTP status from the APIC
189 returned: failure or debug
190 type: int
191 sample: 200
192 url:
193 description: The HTTP url used for the request to the APIC
194 returned: failure or debug
195 type: str
196 sample: https://10.11.12.13/api/mo/uni/tn-production.json
197 """
198
199 from ansible.module_utils.basic import AnsibleModule
200 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
201
202
203 def main():
204 argument_spec = aci_argument_spec()
205 argument_spec.update(aci_annotation_spec())
206 argument_spec.update(aci_owner_spec())
207 argument_spec.update(
208 pod_id=dict(type="int"),
209 node_id=dict(type="int"),
210 remove_from_controller=dict(type="bool"),
211 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
212 )
213
214 module = AnsibleModule(
215 argument_spec=argument_spec,
216 supports_check_mode=True,
217 required_if=[
218 ["state", "present", ["pod_id", "node_id"]],
219 ["state", "absent", ["pod_id", "node_id"]],
220 ],
221 )
222
223 pod_id = module.params.get("pod_id")
224 node_id = module.params.get("node_id")
225 remove_from_controller = module.params.get("remove_from_controller")
226 state = module.params.get("state")
227
228 aci = ACIModule(module)
229
230 target_dn = None
231 if pod_id and node_id:
232 target_dn = "topology/pod-{0}/node-{1}".format(pod_id, node_id)
233
234 aci.construct_url(
235 root_class=dict(
236 aci_class="fabricOOServicePol",
237 aci_rn="fabric/outofsvc",
238 ),
239 subclass_1=dict(
240 aci_class="fabricRsDecommissionNode",
241 aci_rn="rsdecommissionNode-[{0}]".format(target_dn) if target_dn else None,
242 target_filter={"tDn": target_dn},
243 ),
244 )
245
246 aci.get_existing()
247
248 if state == "present": # Decommission
249 aci.payload(
250 aci_class="fabricRsDecommissionNode",
251 class_config=dict(
252 tDn=target_dn,
253 removeFromController="yes" if remove_from_controller else "no",
254 ),
255 )
256 aci.get_diff(aci_class="fabricRsDecommissionNode")
257 aci.post_config()
258
259 elif state == "absent" and aci.existing: # Commission
260 # The aci.delete_config function removes the object directly from APIC, which can interrupt the commission or decommission process before it finishes.
261 # Because of that the Fabric Node may enter a bad state.
262 aci.payload(
263 aci_class="fabricRsDecommissionNode",
264 class_config=dict(
265 tDn=target_dn,
266 status="deleted",
267 ),
268 )
269 aci.get_diff(aci_class="fabricRsDecommissionNode")
270 aci.post_config()
271
272 aci.exit_json()
273
274
275 if __name__ == "__main__":
276 main()