Disk informatoin in target machine

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}}”

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)'

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

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)'

Forget to mention it. It gives me an error

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

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)'

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

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.

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)'

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

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}}”

`

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.

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

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"
  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}}"

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

So something like this:

  - name: "Vg creation"
    lvg:
      vg: "{{vg_name}}"
      pvs: "{% for disk in fdisk.stdout_lines %}/dev/{{ disk }}{% if not loop.last %},{% endif %}{% endfor %}"

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

@ban thanks a lot for your time:

you both made my day