Still create LVM even hosts dont have enough space

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

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.

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

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

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

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

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

  • 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

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