diff --git a/changelogs/fragments/9698-lvg-remove-extra-pvs-parameter.yml b/changelogs/fragments/9698-lvg-remove-extra-pvs-parameter.yml new file mode 100644 index 0000000000..51c0423e8b --- /dev/null +++ b/changelogs/fragments/9698-lvg-remove-extra-pvs-parameter.yml @@ -0,0 +1,2 @@ +minor_changes: + - lvg - add ``remove_extra_pvs`` parameter to control if ansible should remove physical volumes which are not in the ``pvs`` parameter (https://github.com/ansible-collections/community.general/pull/9698). diff --git a/plugins/modules/lvg.py b/plugins/modules/lvg.py index b4e758aca5..b16cdd87a2 100644 --- a/plugins/modules/lvg.py +++ b/plugins/modules/lvg.py @@ -34,6 +34,7 @@ options: - List of comma-separated devices to use as physical devices in this volume group. - Required when creating or resizing volume group. - The module will take care of running pvcreate if needed. + - O(remove_extra_pvs) controls whether or not unspecified physical devices are removed from the volume group. type: list elements: str pesize: @@ -88,6 +89,12 @@ options: type: bool default: false version_added: 7.1.0 + remove_extra_pvs: + description: + - Remove physical volumes from the volume group which are not in O(pvs). + type: bool + default: true + version_added: 10.4.0 seealso: - module: community.general.filesystem - module: community.general.lvol @@ -383,6 +390,7 @@ def main(): force=dict(type='bool', default=False), reset_vg_uuid=dict(type='bool', default=False), reset_pv_uuid=dict(type='bool', default=False), + remove_extra_pvs=dict(type="bool", default=True), ), required_if=[ ['reset_pv_uuid', True, ['pvs']], @@ -399,6 +407,7 @@ def main(): vgoptions = module.params['vg_options'].split() reset_vg_uuid = module.boolean(module.params['reset_vg_uuid']) reset_pv_uuid = module.boolean(module.params['reset_pv_uuid']) + remove_extra_pvs = module.boolean(module.params["remove_extra_pvs"]) this_vg = find_vg(module=module, vg=vg) present_state = state in ['present', 'active', 'inactive'] @@ -494,6 +503,9 @@ def main(): devs_to_remove = list(set(current_devs) - set(dev_list)) devs_to_add = list(set(dev_list) - set(current_devs)) + if not remove_extra_pvs: + devs_to_remove = [] + if current_devs: if present_state: for device in current_devs: diff --git a/tests/integration/targets/lvg/tasks/main.yml b/tests/integration/targets/lvg/tasks/main.yml index 15af2d08c4..6bc6944f5f 100644 --- a/tests/integration/targets/lvg/tasks/main.yml +++ b/tests/integration/targets/lvg/tasks/main.yml @@ -24,6 +24,8 @@ - import_tasks: test_grow_reduce.yml + - import_tasks: test_remove_extra_pvs.yml + - import_tasks: test_pvresize.yml - import_tasks: test_active_change.yml diff --git a/tests/integration/targets/lvg/tasks/test_remove_extra_pvs.yml b/tests/integration/targets/lvg/tasks/test_remove_extra_pvs.yml new file mode 100644 index 0000000000..830986d8da --- /dev/null +++ b/tests/integration/targets/lvg/tasks/test_remove_extra_pvs.yml @@ -0,0 +1,40 @@ +--- +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +# test_grow_reduce already checks the base case with default parameters (remove additional PVs) + +- name: "Create volume group on first disk" + lvg: + vg: testvg + pvs: "{{ loop_device1 }}" + +- name: "get lvm facts" + setup: + +- debug: var=ansible_lvm + +- name: "Assert the testvg span only on first disk" + assert: + that: + - ansible_lvm.pvs[loop_device1].vg == "testvg" + - 'loop_device2 not in ansible_lvm.pvs or + ansible_lvm.pvs[loop_device2].vg == ""' + +- name: "Extend to second disk AND keep first disk" + lvg: + vg: testvg + pvs: "{{ loop_device2 }}" + remove_extra_pvs: false + +- name: "get lvm facts" + setup: + +- debug: var=ansible_lvm + +- name: "Assert the testvg spans on both disks" + assert: + that: + - ansible_lvm.pvs[loop_device1].vg == "testvg" + - ansible_lvm.pvs[loop_device2].vg == "testvg"