Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) Contributors to the Ansible project
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 - Laszlo Szomor (@lszomor)
12 module: lvg_rename
13 short_description: Renames LVM volume groups
14 description:
15 - This module renames volume groups using the C(vgchange) command.
16 extends_documentation_fragment:
17 - community.general._attributes
18 attributes:
19 check_mode:
20 support: full
21 diff_mode:
22 support: full
23 version_added: 7.1.0
24 options:
25 vg:
26 description:
27 - The name or UUID of the source VG.
28 - See V(vgrename(8\)) for valid values.
29 type: str
30 required: true
31 vg_new:
32 description:
33 - The new name of the VG.
34 - See V(lvm(8\)) for valid names.
35 type: str
36 required: true
37 seealso:
38 - module: community.general.lvg
39 notes:
40 - This module does not modify VG renaming-related configurations like C(fstab) entries or boot parameters.
41 """
42
43 EXAMPLES = r"""
44 - name: Rename a VG by name
45 community.general.lvg_rename:
46 vg: vg_orig_name
47 vg_new: vg_new_name
48
49 - name: Rename a VG by UUID
50 community.general.lvg_rename:
51 vg_uuid: SNgd0Q-rPYa-dPB8-U1g6-4WZI-qHID-N7y9Vj
52 vg_new: vg_new_name
53 """
54
55 from ansible.module_utils.basic import AnsibleModule
56
57 from ansible_collections.community.general.plugins.module_utils._lvm import vgrename_runner, vgs_runner
58
59 argument_spec = dict(
60 vg=dict(type="str", required=True),
61 vg_new=dict(type="str", required=True),
62 )
63
64
65 class LvgRename:
66 def __init__(self, module):
67 """
68 Orchestrates the lvg_rename module logic.
69
70 :param module: An AnsibleModule instance.
71 """
72 self.module = module
73 self.result = {"changed": False}
74 self.vg_list = []
75 self._vgs = vgs_runner(module)
76 self._vgrename = vgrename_runner(module)
77 self._load_params()
78
79 def run(self):
80 """Performs the module logic."""
81
82 self._load_vg_list()
83
84 old_vg_exists = self._is_vg_exists(vg=self.vg)
85 new_vg_exists = self._is_vg_exists(vg=self.vg_new)
86
87 if old_vg_exists:
88 if new_vg_exists:
89 self.module.fail_json(msg=f"The new VG name ({self.vg_new}) is already in use.")
90 else:
91 self._rename_vg()
92 else:
93 if new_vg_exists:
94 self.result["msg"] = f"The new VG ({self.vg_new}) already exists, nothing to do."
95 self.module.exit_json(**self.result)
96 else:
97 self.module.fail_json(msg=f"Both current ({self.vg}) and new ({self.vg_new}) VG are missing.")
98
99 self.module.exit_json(**self.result)
100
101 def _load_params(self):
102 """Load the parameters from the module."""
103
104 self.vg = self.module.params["vg"]
105 self.vg_new = self.module.params["vg_new"]
106
107 def _load_vg_list(self):
108 """Load the VGs from the system."""
109
110 with self._vgs("noheadings separator fields") as ctx:
111 dummy, vg_raw_list, dummy = ctx.run(separator=";", fields="vg_name,vg_uuid")
112
113 for vg_info in vg_raw_list.splitlines():
114 vg_name, vg_uuid = vg_info.strip().split(";")
115 self.vg_list.append(vg_name)
116 self.vg_list.append(vg_uuid)
117
118 def _is_vg_exists(self, vg):
119 """
120 Checks VG existence by name or UUID. It removes the '/dev/' prefix before checking.
121
122 :param vg: A string with the name or UUID of the VG.
123 :returns: A boolean indicates whether the VG exists or not.
124 """
125
126 vg_found = False
127 dev_prefix = "/dev/"
128
129 if vg.startswith(dev_prefix):
130 vg_id = vg[len(dev_prefix) :]
131 else:
132 vg_id = vg
133
134 vg_found = vg_id in self.vg_list
135
136 return vg_found
137
138 def _rename_vg(self):
139 """Renames the volume group."""
140
141 if self.module._diff:
142 self.result["diff"] = {"before": {"vg": self.vg}, "after": {"vg": self.vg_new}}
143
144 if self.module.check_mode:
145 self.result["msg"] = f"Running in check mode. The module would rename VG {self.vg} to {self.vg_new}."
146 self.result["changed"] = True
147 else:
148 with self._vgrename("vg vg_new") as ctx:
149 dummy, vg_rename_out, dummy = ctx.run()
150
151 self.result["msg"] = vg_rename_out
152 self.result["changed"] = True
153
154
155 def setup_module_object():
156 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
157 return module
158
159
160 def main():
161 module = setup_module_object()
162 lvg_rename = LvgRename(module=module)
163 lvg_rename.run()
164
165
166 if __name__ == "__main__":
167 main()