# Disk informatoin in target machine

**URL:** <https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686>\
**Category:** Ansible Project\
**Created:** [September 30, 2018, 2:57pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686 "2018-09-30T14:57:12Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinod93pop](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@vinod93pop](https://forum.ansible.com/u/vinod93pop)\
**Post date:** [September 30, 2018, 2:57pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/1 "2018-09-30T14:57:12Z")

</div>

HI all,

I am trying to get the disk information of target machine.

1. I want to check the newly added disk which is fully free, means there is no partition created.
2. running a loop like /dev/sda to /dev/sdz and that should fail if patition is not created and print the me last failed disk. can someone help me ?

I have tried bellow playbook in Linux machine. let me know the changes.

Thanks in advance.

- hosts: one  
become: true  
tasks:
- shell: " lsblk | grep -i disk | awk ‘{print$1}’ "  
register: part
- debug:  
msg: “{{part.stdout\_lines}}”
- shell: “fdisk -l /dev/{{item}}1 | grep -i cylinders”  
register: new  
with\_items:
- “{{part.stdout\_lines}}”  
#until: ‘“cylinders” not in new.stdout’  
failed\_when: new.rc == 1  
ignore\_errors: true
- debug:  
msg: “{{item.new}}”

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 30, 2018, 3:56pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/2 "2018-09-30T15:56:01Z")

</div>

Ansible isn't design for this so it's somewhat cumbersome to make it work like that.  
Ansible is more about, create a partition on all drives that doesn't have one.

You could use this with the shell module, it will output all disk that doesn't have a partition.

bash -c 'comm -23 \<(lsblk -Snlo name) \<(lsblk -l | grep part | cut -c1-3 | uniq)'

---

<div class="post-metadata">

**Author:** ![vinod93pop](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@vinod93pop](https://forum.ansible.com/u/vinod93pop)\
**Post date:** [September 30, 2018, 4:46pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/3 "2018-09-30T16:46:58Z")

</div>

HI kai,

Thanks for your information,

sorry to ask this again, Here getting this below output, but not even a proper one , anything wrong I did?, /dev/sdi is free disk. is that right one, output will be in descending order?

bash -c ‘comm -23 \<(lsblk -snlo name) \<(lsblk -l | grep part | cut -c1-3 | uniq)’  
sr0  
comm: file 1 is not in sorted order  
sda1  
sda  
sdf1  
sdf  
sdh1  
sdh  
rootvg-lv\_swap1 (dm-0)  
sda2  
sda  
rootvg-lv\_swap2 (dm-1)  
sda2  
sda  
rootvg-lv\_swap3 (dm-2)  
sda2  
sda  
rootvg-lv\_swap4 (dm-3)  
sda2  
sda  
rootvg-lv\_swap5 (dm-4)  
sda2  
sda  
rootvg-lv\_swap6 (dm-5)  
sda2  
sda  
rootvg-lv\_swap7 (dm-6)  
sda2  
sda  
rootvg-lv\_swap8 (dm-7)  
sda2  
sda  
rootvg-lv\_root (dm-8)  
sda2  
sda  
u01vg-lv\_u01 (dm-9)  
sde1  
sde  
BKUPvg-lv\_BKUP (dm-10)  
sdd1  
sdd  
ARCHvg-lv\_ARCH (dm-11)  
sdc1  
sdc  
DATAvg-lv\_DATA (dm-12)  
sdb1  
sdb  
sdg1  
sdg  
rootvg-lv\_tmp (dm-13)  
sda2  
sda  
rootvg-lv\_var (dm-14)  
sda2  
sda  
rootvg-lv\_ikea (dm-15)  
sda2  
sda  
sdi

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 30, 2018, 5:28pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/4 "2018-09-30T17:28:31Z")

</div>

A typo, the -s should have been -S

Try this  
bash -c 'comm -23 \<(lsblk -Snlo name) \<(lsblk -l | grep part | cut -c1-3 | uniq)'

---

<div class="post-metadata">

**Author:** ![vinod93pop](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@vinod93pop](https://forum.ansible.com/u/vinod93pop)\
**Post date:** [September 30, 2018, 6:04pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/5 "2018-09-30T18:04:19Z")

</div>

Forget to mention it. It gives me an error

---

<div class="post-metadata">

**Author:** ![vinod93pop](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@vinod93pop](https://forum.ansible.com/u/vinod93pop)\
**Post date:** [October 1, 2018, 6:40am UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/6 "2018-10-01T06:40:24Z")

</div>

Her is the error KAi, so I have changed to small ‘s’

lsblk -Snlo name  
lsblk: invalid option – ‘S’  
Usage:  
lsblk [options] […]  
Options:  
-a, --all print all devices  
-b, --bytes print SIZE in bytes rather than in human readable format  
-d, --nodeps don’t print slaves or holders  
-D, --discard print discard capabilities  
-e, --exclude exclude devices by major number (default: RAM disks)  
-I, --include show only devices with specified major numbers  
-f, --fs output info about filesystems  
-h, --help usage information (this)  
-i, --ascii use ascii characters only  
-m, --perms output info about permissions  
-l, --list use list format ouput  
-n, --noheadings don’t print headings  
-o, --output output columns  
-P, --pairs use key=“value” output format  
-r, --raw use raw output format  
-s, --inverse inverse dependencies  
-t, --topology output info about topology  
-V, --version output version information and exit

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 1, 2018, 9:59am UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/7 "2018-10-01T09:59:47Z")

</div>

I probably have a newer version than you, this doesn't use -S

bash -c 'comm -23 \<(lsblk -l | grep disk | cut -f1 -d" ") \<(lsblk -l | grep part | cut -c1-3 | uniq)'

---

<div class="post-metadata">

**Author:** ![vinod93pop](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@vinod93pop](https://forum.ansible.com/u/vinod93pop)\
**Post date:** [October 1, 2018, 3:14pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/8 "2018-10-01T15:14:05Z")

</div>

HI kai,

It doesn’t give me a proper output instead gives me only disk in that server.

bash -c ‘comm -23 \<(lsblk -l | grep disk | cut -f1 -d" ") \<(lsblk -l)’  
sda  
sdb  
sdc  
sdd  
sde  
sdf  
sdg  
sdh  
sdi  
comm: file 2 is not in sorted order

Here sdi is my last disk which is fully free, how can conclude that the disk is free?

looking for your reply

---

<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:** [October 1, 2018, 3:42pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/9 "2018-10-01T15:42:52Z")

</div>

How about running from shell or command:

for DISK in $(lsblk -l | grep disk | cut -f1 -d" "); do for PART in $(parted --script /dev/${DISK} print | grep ‘^ [1-9]’ | awk ‘{ print $1 }’); do df -h /dev/${DISK}${PART} \> /dev/null 2\>&1;if [[${echo ${?}) -eq 0]]; then echo “/dev/${DISK}${PART} is a mounted partition”; else echo “/dev/${DISK}${PART} is not a mounted partition”;fi;done;done

That should at least show you what is or is not mounted.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 1, 2018, 3:50pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/10 "2018-10-01T15:50:09Z")

</div>

That's because you left out a lot of pipes and commands on the last lsblk, if you check my previous mail you'll see there are more.  
Anyway here is it again:

bash -c 'comm -23 \<(lsblk -l | grep disk | cut -f1 -d" ") \<(lsblk -l | grep part | cut -c1-3 | uniq)'

---

<div class="post-metadata">

**Author:** ![vinod93pop](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@vinod93pop](https://forum.ansible.com/u/vinod93pop)\
**Post date:** [October 2, 2018, 12:46pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/11 "2018-10-02T12:46:11Z")

</div>

HI kai,

Thanks a lot it was working for me.

I need one more help from you.

my playbook got 2 new disks, Everything works perfectly except creating pv and VG as below.

- name: “finding recent added device”  
shell: bash -c ‘comm -23 \<(lsblk -l | grep disk | cut -f1 -d" ") \<(lsblk -l | grep part | cut -c1-3 | uniq)’  
register: fdisk
- debug:  
msg: “New disks are {{fdisk.stdout\_lines}}”
- name: “creating disk from raw”  
parted:  
device: “/dev/{{item}}”  
number: 1  
flags: [lvm]  
state: present  
part\_start: 0%  
part\_end: 100%  
unit: ‘%’  
with\_items:
- “{{fdisk.stdout\_lines}}”
- name: “Vg creation”  
lvg:  
vg: “{{vg\_name}}”  
pvs: “/dev/{{item}}1”  
with\_items:
- “{{fdisk.stdout\_lines}}”

TASK [LVM : Vg creation] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [XXXXXXXXXXXXX] =\> (item=sdh)  
failed: [XXXXXXXXXXXX] (item=sdi) =\> {“changed”: true, “cmd”: “vgcreate test /dev/sdi1”, “delta”: “0:00:00.047863”, “end”: “2018-10-02 11:19:49.557057”, “item”: “sdi”, “msg”: “non-zero return code”, “rc”: 5, “start”: “2018-10-02 11:19:49.509194”, “stderr”: " A volume group called test already exists.“, “stderr\_lines”: [” A volume group called test already exists."], “stdout”: “”, “stdout\_lines”: }  
to retry, use: --limit @/home/viraj4/ansible/lv.retry

---

<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:** [October 2, 2018, 2:50pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/12 "2018-10-02T14:50:22Z")

</div>

This isn’t tested. I ran it successfully for a single known added disk of /dev/vdb. But take a look and it might help:

`

- name: “finding recent added device”  
shell: bash -c ‘comm -23 \<(lsblk -l | grep disk | cut -f1 -d" ") \<(lsblk -l | grep part | cut -c1-3 | uniq)’  
register: fdisk
- debug:  
msg: “New disks are {{fdisk.stdout\_lines}}”
- name: partition check  
parted:  
device: “/dev/{{item}}”  
unit: GiB  
register: “{{ item }}”\_info  
with\_items:
- “{{fdisk.stdout\_lines}}”
- name: partition drive  
parted:  
device: “/dev/{{item}}”  
number: 1  
label: gpt  
part\_start: 0%  
part\_end: 100%  
name: “{{ item }}”-pv  
state: present  
with\_items:
- “{{fdisk.stdout\_lines}}”  
when: {{item}}\_info.disk.table == “unknown”
- name: make-volg  
lvg:  
vg: “{{vg\_name}}”  
pvs: “/dev/{{item}}1”  
pesize: 32  
with\_items:
- “{{fdisk.stdout\_lines}}”

`

---

<div class="post-metadata">

**Author:** ![vinod93pop](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@vinod93pop](https://forum.ansible.com/u/vinod93pop)\
**Post date:** [October 2, 2018, 3:05pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/13 "2018-10-02T15:05:55Z")

</div>

Hi Brad,  
Thanks for the info  
Yes I have the similar playbook, it works for me fine in single disk, but I am looking for multiple disks to be added.

---

<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:** [October 2, 2018, 3:31pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/14 "2018-10-02T15:31:44Z")

</div>

I tried to update the task to do your multiple disks. Did you give it a test run?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 2, 2018, 3:36pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/15 "2018-10-02T15:36:09Z")

</div>

> HI kai,
> 
> Thanks a lot it was working for me.
> 
> I need one more help from you.
> 
> my playbook got 2 new disks, Everything works perfectly except creating pv  
> and VG as below.

Are you trying to create one VG with both disk in the VG?

> - name: "finding recent added device"  
> &nbsp;&nbsp;shell: bash -c 'comm -23 \<(lsblk -l | grep disk | cut -f1 -d" ") \<(lsblk  
> -l | grep part | cut -c1-3 | uniq)'  
> &nbsp;&nbsp;register: fdisk  
> - debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "New disks are {{fdisk.stdout\_lines}}"  
> - name: "creating disk from raw"  
> &nbsp;&nbsp;parted:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;device: "/dev/{{item}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number: 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flags: [lvm]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;part\_start: 0%  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;part\_end: 100%  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unit: '%'  
> &nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{fdisk.stdout\_lines}}"  
> - name: "Vg creation"  
> &nbsp;&nbsp;lvg:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vg: "{{vg\_name}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pvs: "/dev/{{item}}1"  
> &nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{fdisk.stdout\_lines}}"

If so, you need to add both disk separated with comma in pvs: according to the documentation.

So something like this:

&nbsp;&nbsp;- name: "Vg creation"  
&nbsp;&nbsp;&nbsp;&nbsp;lvg:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vg: "{{vg\_name}}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pvs: "{% for disk in fdisk.stdout\_lines %}/dev/{{ disk }}{% if not loop.last %},{% endif %}{% endfor %}"

---

<div class="post-metadata">

**Author:** ![vinod93pop](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@vinod93pop](https://forum.ansible.com/u/vinod93pop)\
**Post date:** [October 3, 2018, 3:15pm UTC](https://forum.ansible.com/t/disk-informatoin-in-target-machine/28686/16 "2018-10-03T15:15:41Z")

</div>

Ohhhh hoooooooooooooooooooooo, OMG, kai you are really great. thanks for supporting.

@ban thanks a lot for your time:

you both made my day
