Debian Codegraph @

ansible_collections/community/general/plugins/modules/aix_devices.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2017, 2018 Kairo Araujo <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 author:
11 - Kairo Araujo (@kairoaraujo)
12 module: aix_devices
13 short_description: Manages AIX devices
14 description:
15 - This module discovers, defines, removes and modifies attributes of AIX devices.
16 deprecated:
17 removed_in: 15.0.0
18 why: The module is not actively maintained.
19 alternative: >-
20 Use C(ibm.power_aix.devices) instead.
21 See U(https://ibm.github.io/ansible-power-aix/modules/devices.html) for details.
22 extends_documentation_fragment:
23 - community.general._attributes
24 attributes:
25 check_mode:
26 support: full
27 diff_mode:
28 support: none
29 options:
30 attributes:
31 description:
32 - A list of device attributes.
33 type: dict
34 device:
35 description:
36 - The name of the device.
37 - V(all) is valid to rescan C(available) all devices (AIX C(cfgmgr) command).
38 type: str
39 force:
40 description:
41 - Forces action.
42 type: bool
43 default: false
44 recursive:
45 description:
46 - Removes or defines a device and children devices.
47 type: bool
48 default: false
49 state:
50 description:
51 - Controls the device state.
52 - V(available) (alias V(present)) rescan a specific device or all devices (when O(device) is not specified).
53 - V(removed) (alias V(absent) removes a device.
54 - V(defined) changes device to Defined state.
55 type: str
56 choices: [available, defined, removed]
57 default: available
58 """
59
60 EXAMPLES = r"""
61 - name: Scan new devices
62 community.general.aix_devices:
63 device: all
64 state: available
65
66 - name: Scan new virtual devices (vio0)
67 community.general.aix_devices:
68 device: vio0
69 state: available
70
71 - name: Removing IP alias to en0
72 community.general.aix_devices:
73 device: en0
74 attributes:
75 delalias4: 10.0.0.100,255.255.255.0
76
77 - name: Removes ent2
78 community.general.aix_devices:
79 device: ent2
80 state: removed
81
82 - name: Put device en2 in Defined
83 community.general.aix_devices:
84 device: en2
85 state: defined
86
87 - name: Removes ent4 (inexistent).
88 community.general.aix_devices:
89 device: ent4
90 state: removed
91
92 - name: Put device en4 in Defined (inexistent)
93 community.general.aix_devices:
94 device: en4
95 state: defined
96
97 - name: Put vscsi1 and children devices in Defined state.
98 community.general.aix_devices:
99 device: vscsi1
100 recursive: true
101 state: defined
102
103 - name: Removes vscsi1 and children devices.
104 community.general.aix_devices:
105 device: vscsi1
106 recursive: true
107 state: removed
108
109 - name: Changes en1 mtu to 9000 and disables arp.
110 community.general.aix_devices:
111 device: en1
112 attributes:
113 mtu: 900
114 arp: 'off'
115 state: available
116
117 - name: Configure IP, netmask and set en1 up.
118 community.general.aix_devices:
119 device: en1
120 attributes:
121 netaddr: 192.168.0.100
122 netmask: 255.255.255.0
123 state: up
124 state: available
125
126 - name: Adding IP alias to en0
127 community.general.aix_devices:
128 device: en0
129 attributes:
130 alias4: 10.0.0.100,255.255.255.0
131 state: available
132 """
133
134 RETURN = r""" # """
135
136 from ansible.module_utils.basic import AnsibleModule
137
138
139 def _check_device(module, device):
140 """
141 Check if device already exists and the state.
142 Args:
143 module: Ansible module.
144 device: device to be checked.
145
146 Returns: bool, device state
147
148 """
149 lsdev_cmd = module.get_bin_path("lsdev", True)
150 rc, lsdev_out, err = module.run_command([lsdev_cmd, "-C", "-l", device])
151
152 if rc != 0:
153 module.fail_json(msg="Failed to run lsdev", rc=rc, err=err)
154
155 if lsdev_out:
156 device_state = lsdev_out.split()[1]
157 return True, device_state
158
159 device_state = None
160 return False, device_state
161
162
163 def _check_device_attr(module, device, attr):
164 """
165
166 Args:
167 module: Ansible module.
168 device: device to check attributes.
169 attr: attribute to be checked.
170
171 Returns:
172
173 """
174 lsattr_cmd = module.get_bin_path("lsattr", True)
175 rc, lsattr_out, err = module.run_command([lsattr_cmd, "-El", device, "-a", f"{attr}"])
176
177 hidden_attrs = ["delalias4", "delalias6"]
178
179 if rc == 255:
180 if attr in hidden_attrs:
181 current_param = ""
182 else:
183 current_param = None
184
185 return current_param
186
187 elif rc != 0:
188 module.fail_json(msg=f"Failed to run lsattr: {err}", rc=rc, err=err)
189
190 current_param = lsattr_out.split()[1]
191 return current_param
192
193
194 def discover_device(module, device):
195 """Discover AIX devices."""
196 cfgmgr_cmd = module.get_bin_path("cfgmgr", True)
197
198 if device is not None:
199 device = f"-l {device}"
200
201 else:
202 device = ""
203
204 changed = True
205 msg = ""
206 if not module.check_mode:
207 rc, cfgmgr_out, err = module.run_command([cfgmgr_cmd, device])
208 changed = True
209 msg = cfgmgr_out
210
211 return changed, msg
212
213
214 def change_device_attr(module, attributes, device, force):
215 """Change AIX device attribute."""
216
217 attr_changed = []
218 attr_not_changed = []
219 attr_invalid = []
220 chdev_cmd = module.get_bin_path("chdev", True)
221
222 for attr in list(attributes.keys()):
223 new_param = attributes[attr]
224 current_param = _check_device_attr(module, device, attr)
225
226 if current_param is None:
227 attr_invalid.append(attr)
228
229 elif current_param != new_param:
230 if force:
231 cmd = [chdev_cmd, "-l", device, "-a", f"{attr}={attributes[attr]}", f"{force}"]
232 else:
233 cmd = [chdev_cmd, "-l", device, "-a", f"{attr}={attributes[attr]}"]
234
235 if not module.check_mode:
236 rc, chdev_out, err = module.run_command(cmd)
237 if rc != 0:
238 module.exit_json(msg="Failed to run chdev.", rc=rc, err=err)
239
240 attr_changed.append(attributes[attr])
241 else:
242 attr_not_changed.append(attributes[attr])
243
244 if len(attr_changed) > 0:
245 changed = True
246 attr_changed_msg = f"Attributes changed: {','.join(attr_changed)}. "
247 else:
248 changed = False
249 attr_changed_msg = ""
250
251 if len(attr_not_changed) > 0:
252 attr_not_changed_msg = f"Attributes already set: {','.join(attr_not_changed)}. "
253 else:
254 attr_not_changed_msg = ""
255
256 if len(attr_invalid) > 0:
257 attr_invalid_msg = f"Invalid attributes: {', '.join(attr_invalid)} "
258 else:
259 attr_invalid_msg = ""
260
261 msg = f"{attr_changed_msg}{attr_not_changed_msg}{attr_invalid_msg}"
262
263 return changed, msg
264
265
266 def remove_device(module, device, force, recursive, state):
267 """Puts device in defined state or removes device."""
268
269 state_opt = {"removed": "-d", "absent": "-d", "defined": ""}
270
271 recursive_opt = {True: "-R", False: ""}
272
273 recursive = recursive_opt[recursive]
274 state = state_opt[state]
275
276 changed = True
277 msg = ""
278 rmdev_cmd = module.get_bin_path("rmdev", True)
279
280 if not module.check_mode:
281 if state:
282 rc, rmdev_out, err = module.run_command([rmdev_cmd, "-l", device, f"{recursive}", f"{force}"])
283 else:
284 rc, rmdev_out, err = module.run_command([rmdev_cmd, "-l", device, f"{recursive}"])
285
286 if rc != 0:
287 module.fail_json(msg="Failed to run rmdev", rc=rc, err=err)
288
289 msg = rmdev_out
290
291 return changed, msg
292
293
294 def main():
295 module = AnsibleModule(
296 argument_spec=dict(
297 attributes=dict(type="dict"),
298 device=dict(type="str"),
299 force=dict(type="bool", default=False),
300 recursive=dict(type="bool", default=False),
301 state=dict(type="str", default="available", choices=["available", "defined", "removed"]),
302 ),
303 supports_check_mode=True,
304 )
305
306 force_opt = {
307 True: "-f",
308 False: "",
309 }
310
311 attributes = module.params["attributes"]
312 device = module.params["device"]
313 force = force_opt[module.params["force"]]
314 recursive = module.params["recursive"]
315 state = module.params["state"]
316
317 result = dict(
318 changed=False,
319 msg="",
320 )
321
322 if state == "available" or state == "present":
323 if attributes:
324 # change attributes on device
325 device_status, device_state = _check_device(module, device)
326 if device_status:
327 result["changed"], result["msg"] = change_device_attr(module, attributes, device, force)
328 else:
329 result["msg"] = f"Device {device} does not exist."
330
331 else:
332 # discovery devices (cfgmgr)
333 if device and device != "all":
334 device_status, device_state = _check_device(module, device)
335 if device_status:
336 # run cfgmgr on specific device
337 result["changed"], result["msg"] = discover_device(module, device)
338
339 else:
340 result["msg"] = f"Device {device} does not exist."
341
342 else:
343 result["changed"], result["msg"] = discover_device(module, device)
344
345 elif state == "removed" or state == "absent" or state == "defined":
346 if not device:
347 result["msg"] = "device is required to removed or defined state."
348
349 else:
350 # Remove device
351 check_device, device_state = _check_device(module, device)
352 if check_device:
353 if state == "defined" and device_state == "Defined":
354 result["changed"] = False
355 result["msg"] = f"Device {device} already in Defined"
356
357 else:
358 result["changed"], result["msg"] = remove_device(module, device, force, recursive, state)
359
360 else:
361 result["msg"] = f"Device {device} does not exist."
362
363 else:
364 result["msg"] = f"Unexpected state {state}."
365 module.fail_json(**result)
366
367 module.exit_json(**result)
368
369
370 if __name__ == "__main__":
371 main()