---
- name: Create VG and LVOL for shrink_lv testing
  hosts: all
  become: true
  gather_facts: true


  vars:
    __part_size: 80GB
    __partition_devices:
      # - /dev/sdb
      # - /dev/sdc
      - /dev/sdd
      - /dev/sde

    __vg_name   : test-vg02
    __lv_name   : test-lv02
    __lv_size   : 120g
    __lv_mount  : "/lvol/{{ __lv_name }}"
    __lv_fstype : ext4


  tasks:
    - name: Debug __partition_devices
      ansible.builtin.debug:
        msg:
          - "{{ __partition_devices }}"
          - "{{ __partition_devices | join(',') }}"
          - "{{ __partition_devices | \
                map('regex_replace', '(?P<device>sd.*)', '\\g<device>' + '1', multiline=True) }}"
        verbosity: 1


    - name: Install LVM2
      ansible.builtin.package:
        name  : lvm2
        state : present


    - name: Create partitions on all drives
      community.general.parted:
        device   : "{{ item }}"
        number   : 1
        flags    : ['lvm']
        state    : present
        part_end : "{{ __part_size }}"
      loop: "{{ __partition_devices }}"


    - name: Create VG
      community.general.lvg:
        vg  : "{{ __vg_name }}"
        pvs : "{{ __partition_devices | \
                  map('regex_replace', '(?P<device>sd.*)', '\\g<device>' + '1', multiline=True) }}"


    - name: Create LV
      community.general.lvol:
        vg    : "{{ __vg_name }}"
        lv    : "{{ __lv_name }}"
        size  : "{{ __lv_size }}"
        force : true


    - name: Create lvol mount point
      ansible.builtin.file:
        path  : "{{ __lv_mount }}"
        state : directory
        mode  : "0755"


    - name: Create filesystem on {{ __lv_name }}
      community.general.filesystem:
        dev    : "/dev/{{ __vg_name }}/{{ __lv_name }}"
        fstype : "{{ __lv_fstype }}"


    - name: Mount {{ __lv_mount }}
      ansible.posix.mount:
        path   : "{{ __lv_mount }}"
        src    : "/dev/{{ __vg_name }}/{{ __lv_name }}"
        fstype : "{{ __lv_fstype }}"
        state  : mounted


...
