# Still create LVM even hosts dont have enough space

**URL:** <https://forum.ansible.com/t/still-create-lvm-even-hosts-dont-have-enough-space/34502>\
**Category:** Ansible Project\
**Created:** [January 25, 2021, 2:15pm UTC](https://forum.ansible.com/t/still-create-lvm-even-hosts-dont-have-enough-space/34502 "2021-01-25T14:15:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ursa\_Brown](https://avatars.discourse-cdn.com/v4/letter/u/eb9ed0/32.png) [@ursa\_Brown](https://forum.ansible.com/u/ursa_Brown)\
**Post date:** [January 25, 2021, 2:15pm UTC](https://forum.ansible.com/t/still-create-lvm-even-hosts-dont-have-enough-space/34502/1 "2021-01-25T14:15:56Z")

</div>

I got 2 hosts,  
one has 50G and the other one has 30G.

I have no problem creating a 40G LVM on the hosts that has 50G. But how can I make my ansible script if the hosts has 30G. It will say not enough space then create 20G of lvm instead.

Thanks

---

<div class="post-metadata">

**Author:** ![Antony\_Stone](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Antony\_Stone](https://forum.ansible.com/u/Antony_Stone)\
**Post date:** [January 25, 2021, 2:22pm UTC](https://forum.ansible.com/t/still-create-lvm-even-hosts-dont-have-enough-space/34502/2 "2021-01-25T14:22:06Z")

</div>

Are you saying that you expect Ansible to create a 40Gb Logical Volume on a  
machine which has only 30Gb capacity?

If not, please explain with a little more detail what you are trying to get  
Ansible to do.

Regards,

Antony.

---

<div class="post-metadata">

**Author:** ![ursa\_Brown](https://avatars.discourse-cdn.com/v4/letter/u/eb9ed0/32.png) [@ursa\_Brown](https://forum.ansible.com/u/ursa_Brown)\
**Post date:** [January 25, 2021, 2:27pm UTC](https://forum.ansible.com/t/still-create-lvm-even-hosts-dont-have-enough-space/34502/3 "2021-01-25T14:27:28Z")

</div>

I have hosts that has 50G and 30G of SDB or disk2…  
My playbook only works if hosts has SDB is 50G, then it has no problem creating the 40G.

But it throwing errros because some hosts has 30G of SDB.

I want my playbook to still run, if the SB has only 30G then it will say, not enough disk, then will create a 20G.  
If the hosts has 50G, it will create the 40G lvm.

Thank you

---

<div class="post-metadata">

**Author:** ![Jorge\_Rua1](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jorge\_Rua1](https://forum.ansible.com/u/Jorge_Rua1)\
**Post date:** [January 25, 2021, 2:37pm UTC](https://forum.ansible.com/t/still-create-lvm-even-hosts-dont-have-enough-space/34502/4 "2021-01-25T14:37:57Z")

</div>

You can fetch secondary disk size and set the lvm size depending on it.  
This is what I would do in your case:

- Set default lvm size to a safe value that you know all your hosts will meet. ie : 20G
- Fetch secondary disk size. If it’s 50G or higher overwrite default lvm size to 40G

Regards

---

<div class="post-metadata">

**Author:** ![ursa\_Brown](https://avatars.discourse-cdn.com/v4/letter/u/eb9ed0/32.png) [@ursa\_Brown](https://forum.ansible.com/u/ursa_Brown)\
**Post date:** [January 25, 2021, 2:41pm UTC](https://forum.ansible.com/t/still-create-lvm-even-hosts-dont-have-enough-space/34502/5 "2021-01-25T14:41:49Z")

</div>

Can you please show me how to do that? will this show an error? before creating the LVM?

---

<div class="post-metadata">

**Author:** ![Jorge\_Rua1](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jorge\_Rua1](https://forum.ansible.com/u/Jorge_Rua1)\
**Post date:** [January 25, 2021, 3:37pm UTC](https://forum.ansible.com/t/still-create-lvm-even-hosts-dont-have-enough-space/34502/6 "2021-01-25T15:37:21Z")

</div>

Something like this would do the trick:

In this case I’m using loop0 instead of sdb.  
If loop0 size is higher than 2 GB (assuming no conversion to MB, that would be great too).  
It will set lv\_size var to 500M, and thar value will be used in lvol task otherwise it will use default lv size which is 400M.

HTH

- hosts: localhost  
become: True  
gather\_facts: False  
vars:  
disk: loop0  
vg\_name: vgname  
lv\_name: lvname  
default\_lv\_size: 400M  
tasks:

- name: Collect hardware facts  
setup:  
gather\_subset:

- hardware

- name: Register {{ disk }} size  
set\_fact:  
secondary\_disk\_size: hostvars[inventory\_hostname].ansible\_devices[‘{{ disk }}’].size.split(’ ')[0]

- name: Set lv size  
set\_fact:  
lv\_size: “500M”  
when:

- secondary\_disk\_size | int \> 2

- name: Create {{ lv\_name }} lv  
lvol:  
vg: “{{ vg\_name }}”  
lv: “{{ lv\_name }}”  
size: “{{ lv\_size | d (default\_lv\_size) }}”  
active: yes  
force: no  
state: present

---

<div class="post-metadata">

**Author:** ![ursa\_Brown](https://avatars.discourse-cdn.com/v4/letter/u/eb9ed0/32.png) [@ursa\_Brown](https://forum.ansible.com/u/ursa_Brown)\
**Post date:** [January 25, 2021, 4:28pm UTC](https://forum.ansible.com/t/still-create-lvm-even-hosts-dont-have-enough-space/34502/7 "2021-01-25T16:28:36Z")

</div>

hmmm, where to add the message? message to needs to show up like “hosts has 30G only creating 20G” before creating the 20G lvm?  
Thank you

---

<div class="post-metadata">

**Author:** ![Jorge\_Rua1](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jorge\_Rua1](https://forum.ansible.com/u/Jorge_Rua1)\
**Post date:** [January 25, 2021, 5:14pm UTC](https://forum.ansible.com/t/still-create-lvm-even-hosts-dont-have-enough-space/34502/8 "2021-01-25T17:14:21Z")

</div>

- hosts: localhost  
become: True  
gather\_facts: False  
vars:  
disk: loop0  
vg\_name: vgname  
lv\_name: lvname  
default\_lv\_size: 400M  
tasks:

- name: Collect hardware facts  
setup:  
gather\_subset:

- hardware

- name: Register {{ disk }} size  
set\_fact:  
secondary\_disk\_size: “{{ hostvars[inventory\_hostname].ansible\_devices[disk].size.split(’ ')[0] }}”

- name: Register {{ disk }} size  
debug:  
var: “{{ secondary\_disk\_size }}”

- name: Small secondary disk  
block:

- name: Set lv size and warn message  
set\_fact:  
lv\_size: “500M”  
warn\_message: “The lazy fox jumped over the brown dog”

- name: Show warning message  
debug:  
msg: “{{ warn\_message }}”  
when:

- secondary\_disk\_size | int \> 2

- name: Create {{ lv\_name }} lv  
lvol:  
vg: “{{ vg\_name }}”  
lv: “{{ lv\_name }}”  
size: “{{ lv\_size | d (default\_lv\_size) }}”  
active: yes  
force: no  
state: present

---

<div class="post-metadata">

**Author:** ![ursa\_Brown](https://avatars.discourse-cdn.com/v4/letter/u/eb9ed0/32.png) [@ursa\_Brown](https://forum.ansible.com/u/ursa_Brown)\
**Post date:** [January 25, 2021, 6:32pm UTC](https://forum.ansible.com/t/still-create-lvm-even-hosts-dont-have-enough-space/34502/9 "2021-01-25T18:32:20Z")

</div>

I was thinking of Block Rescue Always here. Thank you for your input.
