turgun
turgun

Reputation: 59

Ansible: Check disk size and create volume group according its size

on 1st node sdb size is 1GB on 2nd node sdb size is 600M Just I couldn't check size of disk. I'm getting an error (Showing 0 value for size) when I execute the following playbook.

---
- hosts: all
  become: yes
  tasks:

    - debug:
        msg: "{{ ansible_devices.sdb.size }} is less than 800"
      when:  ansible_devices.sdb.size|int  < 800


$ ansible-playbook lvm-new.yml

ansible-playbook lvm-new.yml

PLAY [all] ***********************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************
ok: [servera]
ok: [serverb]

TASK [debug] *********************************************************************************************************************************************************************
ok: [serverb] => {
    "msg": "600.00 MB is less than 800"
}
ok: [servera] => {
    "msg": "1.00 GB is less than 800"
}

PLAY RECAP ***********************************************************************************************************************************************************************
servera                    : ok=2    changed=0    unreachable=0    failed=0   
serverb                    : ok=2    changed=0    unreachable=0    failed=0 

Can you assist to solve this task?

Upvotes: 1

Views: 8152

Answers (1)

turgun
turgun

Reputation: 59

I've solved by the following way.

---
- hosts: all
  become: yes
  tasks:
    - block:
        - parted: device=/dev/sdb unit=MiB
          register: sdb_info
        - debug:
            msg: '{{ sdb_info.disk.size }}'
        - debug:
            msg: "{{ sdb_info.disk.size }} greater than 800"
          when: " sdb_info.disk.size  > 800"
        - debug:
            msg: "{{ sdb_info.disk.size }} less than 800"
          when: " sdb_info.disk.size  < 800"

ansible-playbook lvm-new.yml

PLAY [all] *************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [servera]
ok: [serverb]

TASK [parted] **********************************************************************************************************************************************************
fatal: [serverb]: FAILED! => {"changed": false, "err": "Error: Could not stat device /dev/sdb - No such file or directory.\n", "msg": "Error while getting device information with parted script: '/sbin/parted -s -m /dev/sdb -- unit 'MiB' print'", "out": "", "rc": 1}
ok: [servera]

TASK [debug] ***********************************************************************************************************************************************************
ok: [servera] => {
    "msg": "1024.0"
}

TASK [debug] ***********************************************************************************************************************************************************
ok: [servera] => {
    "msg": "1024.0 greater than 800"
}

TASK [debug] ***********************************************************************************************************************************************************
skipping: [servera]

TASK [debug] ***********************************************************************************************************************************************************
ok: [serverb] => {
    "msg": "No disk detected"
}

PLAY RECAP *************************************************************************************************************************************************************
servera                    : ok=4    changed=0    unreachable=0    failed=0   
serverb                    : ok=2    changed=0    unreachable=0    failed=1   

Upvotes: 3

Related Questions