Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2017, 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_lvg
13 short_description: Manage LVM volume groups on AIX
14 description:
15 - This module creates, removes or resize volume groups on AIX LVM.
16 deprecated:
17 removed_in: 15.0.0
18 why: The module is not actively maintained.
19 alternative: >-
20 Use C(ibm.power_aix.lvg) instead.
21 See U(https://ibm.github.io/ansible-power-aix/modules/lvg.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 force:
31 description:
32 - Force volume group creation.
33 type: bool
34 default: false
35 pp_size:
36 description:
37 - The size of the physical partition in megabytes.
38 type: int
39 pvs:
40 description:
41 - List of comma-separated devices to use as physical devices in this volume group.
42 - Required when creating or extending (V(present) state) the volume group.
43 - If not informed reducing (V(absent) state) the volume group is removed.
44 type: list
45 elements: str
46 state:
47 description:
48 - Control if the volume group exists and volume group AIX state varyonvg V(varyon) or varyoffvg V(varyoff).
49 type: str
50 choices: [absent, present, varyoff, varyon]
51 default: present
52 vg:
53 description:
54 - The name of the volume group.
55 type: str
56 required: true
57 vg_type:
58 description:
59 - The type of the volume group.
60 type: str
61 choices: [big, normal, scalable]
62 default: normal
63 notes:
64 - AIX allows removing VG only if all LV/Filesystems are not busy.
65 - Module does not modify PP size for already present volume group.
66 """
67
68 EXAMPLES = r"""
69 - name: Create a volume group datavg
70 community.general.aix_lvg:
71 vg: datavg
72 pp_size: 128
73 vg_type: scalable
74 state: present
75
76 - name: Removing a volume group datavg
77 community.general.aix_lvg:
78 vg: datavg
79 state: absent
80
81 - name: Extending rootvg
82 community.general.aix_lvg:
83 vg: rootvg
84 pvs: hdisk1
85 state: present
86
87 - name: Reducing rootvg
88 community.general.aix_lvg:
89 vg: rootvg
90 pvs: hdisk1
91 state: absent
92 """
93
94 RETURN = r""" # """
95
96 from ansible.module_utils.basic import AnsibleModule
97
98
99 def _validate_pv(module, vg, pvs):
100 """
101 Function to validate if the physical volume (PV) is not already in use by
102 another volume group or Oracle ASM.
103
104 :param module: Ansible module argument spec.
105 :param vg: Volume group name.
106 :param pvs: Physical volume list.
107 :return: [bool, message] or module.fail_json for errors.
108 """
109
110 lspv_cmd = module.get_bin_path("lspv", True)
111 rc, current_lspv, stderr = module.run_command([lspv_cmd])
112 if rc != 0:
113 module.fail_json(msg="Failed executing 'lspv' command.", rc=rc, stdout=current_lspv, stderr=stderr)
114
115 for pv in pvs:
116 # Get pv list.
117 lspv_list = {}
118 for line in current_lspv.splitlines():
119 pv_data = line.split()
120 lspv_list[pv_data[0]] = pv_data[2]
121
122 # Check if pv exists and is free.
123 if pv not in lspv_list.keys():
124 module.fail_json(msg=f"Physical volume '{pv}' doesn't exist.")
125
126 if lspv_list[pv] == "None":
127 # Disk None, looks free.
128 # Check if PV is not already in use by Oracle ASM.
129 lquerypv_cmd = module.get_bin_path("lquerypv", True)
130 rc, current_lquerypv, stderr = module.run_command([lquerypv_cmd, "-h", f"/dev/{pv}", "20", "10"])
131 if rc != 0:
132 module.fail_json(
133 msg="Failed executing lquerypv command.", rc=rc, stdout=current_lquerypv, stderr=stderr
134 )
135
136 if "ORCLDISK" in current_lquerypv:
137 module.fail_json(f"Physical volume '{pv}' is already used by Oracle ASM.")
138
139 msg = f"Physical volume '{pv}' is ok to be used."
140 return True, msg
141
142 # Check if PV is already in use for the same vg.
143 elif vg != lspv_list[pv]:
144 module.fail_json(msg=f"Physical volume '{pv}' is in use by another volume group '{lspv_list[pv]}'.")
145
146 msg = f"Physical volume '{pv}' is already used by volume group '{lspv_list[pv]}'."
147 return False, msg
148
149
150 def _validate_vg(module, vg):
151 """
152 Check the current state of volume group.
153
154 :param module: Ansible module argument spec.
155 :param vg: Volume Group name.
156 :return: True (VG in varyon state) or False (VG in varyoff state) or
157 None (VG does not exist), message.
158 """
159 lsvg_cmd = module.get_bin_path("lsvg", True)
160 rc, current_active_vgs, err = module.run_command([lsvg_cmd, "-o"])
161 if rc != 0:
162 module.fail_json(msg=f"Failed executing '{lsvg_cmd}' command.")
163
164 rc, current_all_vgs, err = module.run_command([lsvg_cmd])
165 if rc != 0:
166 module.fail_json(msg=f"Failed executing '{lsvg_cmd}' command.")
167
168 if vg in current_all_vgs and vg not in current_active_vgs:
169 msg = f"Volume group '{vg}' is in varyoff state."
170 return False, msg
171
172 if vg in current_active_vgs:
173 msg = f"Volume group '{vg}' is in varyon state."
174 return True, msg
175
176 msg = f"Volume group '{vg}' does not exist."
177 return None, msg
178
179
180 def create_extend_vg(module, vg, pvs, pp_size, vg_type, force, vg_validation):
181 """Creates or extend a volume group."""
182
183 # Command option parameters.
184 force_opt = {True: "-f", False: ""}
185
186 vg_opt = {
187 "normal": "",
188 "big": "-B",
189 "scalable": "-S",
190 }
191
192 # Validate if PV are not already in use.
193 pv_state, msg = _validate_pv(module, vg, pvs)
194 if not pv_state:
195 changed = False
196 return changed, msg
197
198 vg_state, msg = vg_validation
199 if vg_state is False:
200 changed = False
201 return changed, msg
202
203 elif vg_state is True:
204 # Volume group extension.
205 changed = True
206 msg = ""
207
208 if not module.check_mode:
209 extendvg_cmd = module.get_bin_path("extendvg", True)
210 rc, output, err = module.run_command([extendvg_cmd, vg] + pvs)
211 if rc != 0:
212 changed = False
213 msg = f"Extending volume group '{vg}' has failed."
214 return changed, msg
215
216 msg = f"Volume group '{vg}' extended."
217 return changed, msg
218
219 elif vg_state is None:
220 # Volume group creation.
221 changed = True
222 msg = ""
223
224 if not module.check_mode:
225 mkvg_cmd = module.get_bin_path("mkvg", True)
226 rc, output, err = module.run_command([mkvg_cmd, vg_opt[vg_type], pp_size, force_opt[force], "-y", vg] + pvs)
227 if rc != 0:
228 changed = False
229 msg = f"Creating volume group '{vg}' failed."
230 return changed, msg
231
232 msg = f"Volume group '{vg}' created."
233 return changed, msg
234
235
236 def reduce_vg(module, vg, pvs, vg_validation):
237 vg_state, msg = vg_validation
238
239 if vg_state is False:
240 changed = False
241 return changed, msg
242
243 elif vg_state is None:
244 changed = False
245 return changed, msg
246
247 # Define pvs_to_remove (list of physical volumes to be removed).
248 if pvs is None:
249 # Remove VG if pvs are note informed.
250 # Remark: AIX will permit remove only if the VG has not LVs.
251 lsvg_cmd = module.get_bin_path("lsvg", True)
252 rc, current_pvs, err = module.run_command([lsvg_cmd, "-p", vg])
253 if rc != 0:
254 module.fail_json(msg=f"Failing to execute '{lsvg_cmd}' command.")
255
256 pvs_to_remove = []
257 for line in current_pvs.splitlines()[2:]:
258 pvs_to_remove.append(line.split()[0])
259
260 reduce_msg = f"Volume group '{vg}' removed."
261 else:
262 pvs_to_remove = pvs
263 reduce_msg = f"Physical volume(s) '{' '.join(pvs_to_remove)}' removed from Volume group '{vg}'."
264
265 # Reduce volume group.
266 if len(pvs_to_remove) <= 0:
267 changed = False
268 msg = "No physical volumes to remove."
269 return changed, msg
270
271 changed = True
272 msg = ""
273
274 if not module.check_mode:
275 reducevg_cmd = module.get_bin_path("reducevg", True)
276 rc, stdout, stderr = module.run_command([reducevg_cmd, "-df", vg] + pvs_to_remove)
277 if rc != 0:
278 module.fail_json(msg=f"Unable to remove '{vg}'.", rc=rc, stdout=stdout, stderr=stderr)
279
280 msg = reduce_msg
281 return changed, msg
282
283
284 def state_vg(module, vg, state, vg_validation):
285 vg_state, msg = vg_validation
286
287 if vg_state is None:
288 module.fail_json(msg=msg)
289
290 if state == "varyon":
291 if vg_state is True:
292 changed = False
293 return changed, msg
294
295 changed = True
296 msg = ""
297 if not module.check_mode:
298 varyonvg_cmd = module.get_bin_path("varyonvg", True)
299 rc, varyonvg_out, err = module.run_command([varyonvg_cmd, vg])
300 if rc != 0:
301 module.fail_json(msg="Command 'varyonvg' failed.", rc=rc, err=err)
302
303 msg = f"Varyon volume group {vg} completed."
304 return changed, msg
305
306 elif state == "varyoff":
307 if vg_state is False:
308 changed = False
309 return changed, msg
310
311 changed = True
312 msg = ""
313
314 if not module.check_mode:
315 varyonvg_cmd = module.get_bin_path("varyoffvg", True)
316 rc, varyonvg_out, stderr = module.run_command([varyonvg_cmd, vg])
317 if rc != 0:
318 module.fail_json(msg="Command 'varyoffvg' failed.", rc=rc, stdout=varyonvg_out, stderr=stderr)
319
320 msg = f"Varyoff volume group {vg} completed."
321 return changed, msg
322
323
324 def main():
325 module = AnsibleModule(
326 argument_spec=dict(
327 force=dict(type="bool", default=False),
328 pp_size=dict(type="int"),
329 pvs=dict(type="list", elements="str"),
330 state=dict(type="str", default="present", choices=["absent", "present", "varyoff", "varyon"]),
331 vg=dict(type="str", required=True),
332 vg_type=dict(type="str", default="normal", choices=["big", "normal", "scalable"]),
333 ),
334 supports_check_mode=True,
335 )
336
337 force = module.params["force"]
338 pp_size = module.params["pp_size"]
339 pvs = module.params["pvs"]
340 state = module.params["state"]
341 vg = module.params["vg"]
342 vg_type = module.params["vg_type"]
343
344 if pp_size is None:
345 pp_size = ""
346 else:
347 pp_size = f"-s {pp_size}"
348
349 vg_validation = _validate_vg(module, vg)
350
351 if state == "present":
352 if not pvs:
353 changed = False
354 msg = "pvs is required to state 'present'."
355 module.fail_json(msg=msg)
356 else:
357 changed, msg = create_extend_vg(module, vg, pvs, pp_size, vg_type, force, vg_validation)
358
359 elif state == "absent":
360 changed, msg = reduce_vg(module, vg, pvs, vg_validation)
361
362 elif state == "varyon" or state == "varyoff":
363 changed, msg = state_vg(module, vg, state, vg_validation)
364
365 else:
366 changed = False
367 msg = "Unexpected state"
368
369 module.exit_json(changed=changed, msg=msg, state=state)
370
371
372 if __name__ == "__main__":
373 main()