# creating logical volume if does not exist

**URL:** <https://forum.ansible.com/t/creating-logical-volume-if-does-not-exist/28559>\
**Category:** Ansible Project\
**Tags:** rhel\
**Created:** [September 12, 2018, 12:27pm UTC](https://forum.ansible.com/t/creating-logical-volume-if-does-not-exist/28559 "2018-09-12T12:27:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brad2](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Brad2](https://forum.ansible.com/u/Brad2)\
**Post date:** [September 12, 2018, 12:27pm UTC](https://forum.ansible.com/t/creating-logical-volume-if-does-not-exist/28559/1 "2018-09-12T12:27:04Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![cava\_cavamagie](https://avatars.discourse-cdn.com/v4/letter/c/e68b1a/32.png) [@cava\_cavamagie](https://forum.ansible.com/u/cava_cavamagie)\
**Post date:** [September 12, 2018, 12:42pm UTC](https://forum.ansible.com/t/creating-logical-volume-if-does-not-exist/28559/2 "2018-09-12T12:42:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brad2](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Brad2](https://forum.ansible.com/u/Brad2)\
**Post date:** [September 12, 2018, 3:36pm UTC](https://forum.ansible.com/t/creating-logical-volume-if-does-not-exist/28559/3 "2018-09-12T15:36:24Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![cava\_cavamagie](https://avatars.discourse-cdn.com/v4/letter/c/e68b1a/32.png) [@cava\_cavamagie](https://forum.ansible.com/u/cava_cavamagie)\
**Post date:** [September 12, 2018, 3:37pm UTC](https://forum.ansible.com/t/creating-logical-volume-if-does-not-exist/28559/4 "2018-09-12T15:37:51Z")

</div>

great! you are welcome!
