Still create LVM even hosts dont have enough space

11 views
Skip to first unread message

ursa Brown

unread,
Jan 25, 2021, 9:15:56 AM1/25/21
to Ansible Project
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

Antony Stone

unread,
Jan 25, 2021, 9:22:31 AM1/25/21
to ansible...@googlegroups.com
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.

--
René Descartes walks in to a bar.
The barman asks him "Do you want a drink?"
Descartes says "I think not," and disappears.

Please reply to the list;
please *don't* CC me.

ursa Brown

unread,
Jan 25, 2021, 9:27:28 AM1/25/21
to Ansible Project
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

Jorge Rúa

unread,
Jan 25, 2021, 9:38:24 AM1/25/21
to ansible...@googlegroups.com
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
  





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f2252d83-0154-4413-8e04-3cec6c4c0b84n%40googlegroups.com.

ursa Brown

unread,
Jan 25, 2021, 9:41:49 AM1/25/21
to Ansible Project
Can you please show me how to do that? will this show an error? before creating the LVM?

Jorge Rúa

unread,
Jan 25, 2021, 10:37:49 AM1/25/21
to ansible...@googlegroups.com
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


ursa Brown

unread,
Jan 25, 2021, 11:28:36 AM1/25/21
to Ansible Project
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

Jorge Rúa

unread,
Jan 25, 2021, 12:14:47 PM1/25/21
to ansible...@googlegroups.com
- 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

ursa Brown

unread,
Jan 25, 2021, 1:32:21 PM1/25/21
to Ansible Project
I was thinking of Block Rescue Always here. Thank you for your input.
Reply all
Reply to author
Forward
0 new messages