Repository: ansible-collections/community.general

Issue:
lvol: Ansible on 2nd and later runs expanding the Logical Volume size by doubling it, rather than locking it at 50% of the VG
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
<!--- Explain the problem briefly below -->
Issue is caused by Ansible on 2nd and later runs expanding the Logical Volume size by doubling it, rather than locking it at 50% of the VG.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
lvol

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```
ansible 2.9.7
  config file = None
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /root/.local/lib/python2.7/site-packages/ansible
  executable location = /root/bin/ansible
  python version = 2.7.5 (default, Nov 27 2019, 09:57:45) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39.0.1)]
```

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below

```

##### OS / ENVIRONMENT
<!--- Provide all relevant information below, e.g. target OS versions, network device firmware, etc. -->
OS version: OracleLinux 7.x

##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->

<!--- Paste example playbooks or commands between quotes below -->
```
- name: 'Ensure partitions exist'
  parted:
    align: 'optimal'
    device: '{{ item }}'
    label: 'gpt'
    name: 'nvme'
    number: '1'
    part_end: '100%'
    part_start: '0%'
    state: 'present'
  loop:
     - '/dev/nvme0n1'
     - '/dev/nvme1n1'
- name: 'Ensure vg_nvme exists'
  lvg:
    pvs: /dev/nvme0n1p1,/dev/nvme1n1p1
    state: 'present'
    vg: 'vg_nvme'
- name: 'Ensure lv_nvme exists'
  lvol:
    active: 'yes'
    lv: 'lv_nvme'
    opts: '--type raid1'
    resizefs: 'yes'
    shrink: 'yes'
    size: '50%VG'
    state: 'present'
    vg: 'vg_nvme'
- name: 'Ensure filesystem exists'
  filesystem:
    dev: '/dev/mapper/vg_nvme-lv_nvme'
    fstype: 'xfs'
- name: 'Ensure filesystem mounted'
  mount:
    backup: 'yes'
    dump: '1'
    fstype: 'xfs'
    opts: 'defaults'
    passno: '2'
    path: '/nvme'
    src: '/dev/mapper/vg_nvme-lv_nvme'
    state: 'mounted'


```

<!--- HINT: You can paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->


##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes -->
```
first run:
[root@vmdenseio216-test ~]# df -hP /nvme
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_nvme-lv_nvme 3.0T 33M 3.0T 1% /nvme
[root@vmdenseio216-test ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg_nvme 2 1 0 wz--n- 11.64t 5.82t
[root@vmdenseio216-test ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_nvme vg_nvme rwi-aor--- 2.91t 0.52
[root@vmdenseio216-test ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p1 vg_nvme lvm2 a-- 5.82t 2.91t
/dev/nvme1n1p1 vg_nvme lvm2 a-- 5.82t 2.91t
[root@vmdenseio216-test ~]#

Second run:
[root@vmdenseio216-test ~]# df -hT /nvme
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg_nvme-lv_nvme xfs 5.9T 33M 5.9T 1% /nvme
[root@vmdenseio216-test ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg_nvme 2 1 0 wz--n- 11.64t 0
[root@vmdenseio216-test ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_nvme vg_nvme rwi-aor--- 5.82t 0.51
[root@vmdenseio216-test ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p1 vg_nvme lvm2 a-- 5.82t 0
/dev/nvme1n1p1 vg_nvme lvm2 a-- 5.82t 0
[root@vmdenseio216-test ~]#

```




Discussion:
Files identified in the description:
* [`plugins/modules/system/lvol.py`](https://github.com/ansible-collections/community.general/blob/master/plugins/modules/system/lvol.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @MorrisA @abulimov @bcoca @d-little @flynn1973 @gforster @jhoekx @kairoaraujo @marvin-sinister @mator @molekuul @ramooncamacho @wtcross
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
didn't looked at the code yet, still a quick comment, that xfs filesystem does not support shrinking...
Looks like this is the same problem as https://github.com/ansible/ansible/issues/29201 (lvol module is not idempotent)

There was a PR https://github.com/ansible/ansible/pull/69136 that would fix this 3 years old problem... and it was a simple fix
Used the new collection modules [community.general.lvg, community.general.lvol ] but still am facing same issue.

RAID: --type raid1

first run:
[root@vmdenseio216 ~]# vgs
  VG      #PV #LV #SN Attr   VSize  VFree
  vg_nvme   2   1   0 wz--n- 11.64t 5.82t
[root@vmdenseio216 ~]#

Second Run:
[root@vmdenseio216 ~]# vgs
  VG      #PV #LV #SN Attr   VSize  VFree
  vg_nvme   2   1   0 wz--n- 11.64t    0
[root@vmdenseio216 ~]#
[root@vmdenseio216 ~]# pvs
  PV             VG      Fmt  Attr PSize PFree
  /dev/nvme0n1p1 vg_nvme lvm2 a--  5.82t    0
  /dev/nvme1n1p1 vg_nvme lvm2 a--  5.82t    0
[root@vmdenseio216 ~]#

Version details:
ansible 2.9.7
  config file = /srv/os_mgmt/ansible.cfg
  configured module search path = [u'/root/.ansible/collections/ansible_collections/community/general/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Aug  7 2019, 08:19:52) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39.0.1)]

The fix is still alive and kicking in this repo: #229 - it needs reviews and testing though.

Resolving patch:
diff --git a/plugins/modules/system/lvol.py b/plugins/modules/system/lvol.py
--- a/plugins/modules/system/lvol.py
+++ b/plugins/modules/system/lvol.py
@@ -473,6 +473,10 @@ def main():
                 size_requested = size_percent * this_vg['size'] / 100
             else:  # size_whole == 'FREE':
                 size_requested = size_percent * this_vg['free'] / 100
+
+            # Round down to the next lowest whole physical extent
+            size_requested -= (size_requested % this_vg['ext_size'])
+
             if '+' in size:
                 size_requested += this_lv['size']
             if this_lv['size'] < size_requested:


Test patch: