Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/ndo_node_profile.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2025, Samita Bhattacharjee (@samiib) <[email protected]>
5
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 ANSIBLE_METADATA = {
13 "metadata_version": "1.1",
14 "status": ["preview"],
15 "supported_by": "community",
16 }
17
18 DOCUMENTATION = r"""
19 ---
20 module: ndo_node_profile
21 short_description: Manage Node Profiles on Cisco Nexus Dashboard Orchestrator (NDO).
22 description:
23 - Manage Node Profiles on Cisco Nexus Dashboard Orchestrator (NDO).
24 - This module is only supported on ND v3.1 (NDO v4.3) and later.
25 author:
26 - Samita Bhattacharjee (@samiib)
27 options:
28 template:
29 description:
30 - The name of the template.
31 - The template must be a Fabric Resource Policy template.
32 - This parameter or O(template_id) is required.
33 type: str
34 template_id:
35 description:
36 - The ID of the template.
37 - The template must be a Fabric Resource Policy template.
38 - This parameter or O(template) is required.
39 type: str
40 name:
41 description:
42 - The name of the Node Profile.
43 type: str
44 aliases: [ node_profile ]
45 uuid:
46 description:
47 - The UUID of the Node Profile.
48 - This parameter is required when the O(name) needs to be updated.
49 type: str
50 aliases: [ node_profile_uuid ]
51 description:
52 description:
53 - The description of the Node Profile.
54 type: str
55 nodes:
56 description:
57 - The list of node IDs to associate with the Node Profile.
58 - Each element can either be a single node ID or a range of IDs.
59 - This parameter is required when O(state=present).
60 type: list
61 elements: str
62 node_setting_uuid:
63 description:
64 - The UUID of the node setting to associate with the Node Profile.
65 - This parameter or O(node_setting) is required when O(state=present).
66 type: str
67 node_setting:
68 description:
69 - The node setting to associate with the Node Profile.
70 - This parameter or O(node_setting_uuid) is required when O(state=present).
71 type: dict
72 suboptions:
73 name:
74 description:
75 - The name of the node setting.
76 type: str
77 required: true
78 template:
79 description:
80 - The name of the fabric policy template that contains the node setting.
81 - This parameter or O(node_setting.template_id) is required.
82 type: str
83 template_id:
84 description:
85 - The ID of the fabric policy template that contains the node setting.
86 - This parameter or O(node_setting.template) is required.
87 type: str
88 state:
89 description:
90 - Use C(absent) for removing.
91 - Use C(query) for listing an object or multiple objects.
92 - Use C(present) for creating or updating.
93 type: str
94 choices: [ absent, query, present ]
95 default: query
96 notes:
97 - The O(template) and O(node_setting) must exist before using this module in your playbook.
98 Use M(cisco.mso.ndo_template) to create the Fabric Resource Policy template.
99 Use M(cisco.mso.ndo_node_setting) to create the Node Setting.
100 seealso:
101 - module: cisco.mso.ndo_template
102 - module: cisco.mso.ndo_node_setting
103 extends_documentation_fragment: cisco.mso.modules
104 """
105
106 EXAMPLES = r"""
107 - name: Create a new Node Profile
108 cisco.mso.ndo_node_profile:
109 host: mso_host
110 username: admin
111 password: SomeSecretPassword
112 template: fabric_resource_template
113 name: node_profile_1
114 nodes: [101, 102, 103-105]
115 node_setting:
116 name: node_setting_1
117 template: fabric_template
118 state: present
119 register: create_node_profile_1
120
121 - name: Update the name and Node Setting of a Node Policy using UUID
122 cisco.mso.ndo_node_profile:
123 host: mso_host
124 username: admin
125 password: SomeSecretPassword
126 template_id: "{{ create_fabric_resource.current.templateId }}"
127 name: node_profile_1_updated
128 nodes: [101, 102, 103-105]
129 node_setting_uuid: "{{ create_node_setting_2.current.uuid }}"
130 state: present
131 register: update_node_profile_1
132
133 - name: Query an existing Node Profile using UUID
134 cisco.mso.ndo_node_profile:
135 host: mso_host
136 username: admin
137 password: SomeSecretPassword
138 template: fabric_resource_template
139 uuid: "{{ create_node_profile_1.current.uuid }}"
140 state: query
141 register: query_with_uuid
142
143 - name: Query an existing Node Profile using name
144 cisco.mso.ndo_node_profile:
145 host: mso_host
146 username: admin
147 password: SomeSecretPassword
148 template: fabric_resource_template
149 name: node_profile_1_updated
150 state: query
151 register: query_with_name
152
153 - name: Query all Node Profiles
154 cisco.mso.ndo_node_profile:
155 host: mso_host
156 username: admin
157 password: SomeSecretPassword
158 template: fabric_resource_template
159 state: query
160 register: query_all
161
162 - name: Delete an existing Node Profile using UUID
163 cisco.mso.ndo_node_profile:
164 host: mso_host
165 username: admin
166 password: SomeSecretPassword
167 template: fabric_resource_template
168 uuid: "{{ create_node_profile_1.current.uuid }}"
169 state: absent
170
171 - name: Delete an existing Node Profile using Name
172 cisco.mso.ndo_node_profile:
173 host: mso_host
174 username: admin
175 password: SomeSecretPassword
176 template: fabric_resource_template
177 name: node_profile_1_updated
178 state: absent
179 """
180
181 RETURN = r"""
182 """
183
184 from ansible.module_utils.basic import AnsibleModule
185 from ansible_collections.cisco.mso.plugins.module_utils.mso import (
186 MSOModule,
187 mso_argument_spec,
188 )
189 from ansible_collections.cisco.mso.plugins.module_utils.template import (
190 MSOTemplate,
191 KVPair,
192 )
193 from ansible_collections.cisco.mso.plugins.module_utils.utils import (
194 append_update_ops_data,
195 )
196 import copy
197
198
199 def main():
200 argument_spec = mso_argument_spec()
201 argument_spec.update(
202 template=dict(type="str"),
203 template_id=dict(type="str"),
204 name=dict(type="str", aliases=["node_profile"]),
205 uuid=dict(type="str", aliases=["node_profile_uuid"]),
206 description=dict(type="str"),
207 nodes=dict(type="list", elements="str"),
208 node_setting_uuid=dict(type="str"),
209 node_setting=dict(
210 type="dict",
211 options=dict(
212 name=dict(type="str", required=True),
213 template=dict(type="str"),
214 template_id=dict(type="str"),
215 ),
216 required_one_of=[
217 ["template", "template_id"],
218 ],
219 mutually_exclusive=[
220 ("template", "template_id"),
221 ],
222 ),
223 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
224 )
225
226 module = AnsibleModule(
227 argument_spec=argument_spec,
228 supports_check_mode=True,
229 mutually_exclusive=[
230 ("template", "template_id"),
231 ("node_setting_uuid", "node_setting"),
232 ],
233 required_if=[
234 ["state", "absent", ["name", "uuid"], True],
235 ["state", "present", ["name", "uuid"], True],
236 ["state", "present", ["node_setting_uuid", "node_setting"], True],
237 ["state", "present", ["nodes"]],
238 ],
239 required_one_of=[["template", "template_id"]],
240 )
241
242 mso = MSOModule(module)
243
244 template_name = module.params.get("template")
245 template_id = module.params.get("template_id")
246 name = module.params.get("name")
247 uuid = module.params.get("uuid")
248 description = module.params.get("description")
249 nodes = module.params.get("nodes")
250 node_setting_uuid = module.params.get("node_setting_uuid")
251 node_setting = module.params.get("node_setting")
252 state = module.params.get("state")
253
254 ops = []
255 match = None
256
257 mso_template = MSOTemplate(mso, "fabric_resource", template_name, template_id)
258 mso_template.validate_template("fabricResource")
259 object_description = "Node Profile"
260 path = "/fabricResourceTemplate/template/nodeProfiles"
261 node_profile_path = None
262 reference_collection = {
263 "policy": {
264 "name": "policyName",
265 "reference": "policy",
266 "type": "nodePolicyGroup",
267 "template": "policyTemplateName",
268 "templateId": "policyTemplateId",
269 },
270 }
271
272 existing_node_profiles = mso_template.template.get("fabricResourceTemplate", {}).get("template", {}).get("nodeProfiles") or []
273
274 if name or uuid:
275 match = mso_template.get_object_by_key_value_pairs(
276 object_description,
277 existing_node_profiles,
278 [KVPair("uuid", uuid) if uuid else KVPair("name", name)],
279 )
280 if match:
281 node_profile_path = "{0}/{1}".format(path, match.index)
282 mso_template.update_config_with_template_and_references(match.details, reference_collection)
283 mso.existing = mso.previous = copy.deepcopy(match.details)
284 else:
285 mso.existing = mso.previous = [
286 mso_template.update_config_with_template_and_references(profile, reference_collection) for profile in existing_node_profiles
287 ]
288
289 if state == "present":
290 if uuid and not mso.existing:
291 mso.fail_json(msg="{0} with the UUID: '{1}' not found".format(object_description, uuid))
292
293 if node_setting:
294 fabric_policy_template = MSOTemplate(mso, "fabric_policy", node_setting.get("template"), node_setting.get("template_id"))
295 fabric_policy_template.validate_template("fabricPolicy")
296 node_setting_uuid = fabric_policy_template.get_node_settings_object(uuid=None, name=node_setting.get("name"), fail_module=True).details.get("uuid")
297
298 mso_values = dict(
299 name=name,
300 description=description,
301 nodes=nodes,
302 policy=node_setting_uuid,
303 )
304
305 if match:
306 proposed_payload = copy.deepcopy(match.details)
307 append_update_ops_data(ops, proposed_payload, node_profile_path, mso_values)
308 mso.sanitize(proposed_payload, collate=True)
309 else:
310 mso.sanitize(mso_values)
311 ops.append(dict(op="add", path="{0}/-".format(path), value=mso.sent))
312
313 elif state == "absent":
314 if match:
315 ops.append(dict(op="remove", path=node_profile_path))
316
317 if not module.check_mode and ops:
318 response = mso.request(mso_template.template_path, method="PATCH", data=ops)
319 node_profiles = response.get("fabricResourceTemplate", {}).get("template", {}).get("nodeProfiles") or []
320 match = mso_template.get_object_by_key_value_pairs(
321 object_description,
322 node_profiles,
323 [KVPair("uuid", uuid) if uuid else KVPair("name", name)],
324 )
325 if match:
326 mso_template.update_config_with_template_and_references(match.details, reference_collection)
327 mso.existing = match.details # When the state is present
328 else:
329 mso.existing = {} # When the state is absent
330 elif module.check_mode and state != "query": # When the state is present/absent with check mode
331 mso_template.update_config_with_template_and_references(mso.proposed, reference_collection)
332 mso.existing = mso.proposed if state == "present" else {}
333
334 mso.exit_json()
335
336
337 if __name__ == "__main__":
338 main()