creating logical volume if does not exist

Relatively new to ansible. Trying to create a logical volume on some RHEL 7 servers. I have:

`

  • name: make-lv
    lvol:
    vg: vg_{{ inventory_hostname_short }}
    lv: lv_ekb
    size: 100%FREE
    state: present

`

which works fine if the logical volume does not exist, but fails when it does. How do I tell it not to fail if the logical volume already exists?

Thanks!

hi can you run with debug?

i use similar

  • name: manage_lvm | creating new LVM logical volume(s)
    lvol:
    vg: “{{ item[0][‘vgname’] }}”
    lv: “{{ item[1][‘lvname’] }}”
    size: “{{ item[1][‘size’] }}”
    shrink: no
    opts: “{{ item[1][‘opts’] | default(‘’) }}”
    state: “present”
    become: true
    register: lvm
    with_subelements:
  • “{{ lvm_groups }}”
  • lvnames
    when: >
    ((item[0][‘create’] is defined
    and item[0][‘create’]) and
    (item[1] is defined and
    item[1] != ‘None’) and
    (item[1][‘create’] is defined and
    item[1][‘create’]))

lvm_groups:

  • vgname: root_vg
    create: true
    lvnames:
  • lvname: root_lv
    size: 16G
    create: true
    filesystem: ext4
    mount: true
    mntp: /
  • lvname: tmp_lv
    size: 8G
    create: true
    filesystem: ext4
    mount: true
    mntp: /tmp
  • lvname: var_lv
    size: 4G
    create: true
    filesystem: ext4
    mount: true
    mntp: /var

I could not get code similar to yours to work. I did get it to work with these changes:

`

  • name: lv-test
    command: lvdisplay /dev/vg{{ inventory_hostname_short }}/lv_ekb
    register: lvdisp

`
-name: make-lv
lvol:
vg: vg_{{ inventory_hostname_short }}
lv: lv_ekb
size: 100%FREE
shrink: no
state: present
become: true
when: lvdisp.rc|int != 0

`
``

`

this seems to be working. Thanks so much for your help!

great! you are welcome!