Disk informatoin in target machine

104 views
Skip to first unread message

vinod...@gmail.com

unread,
Sep 30, 2018, 10:57:12 AM9/30/18
to Ansible Project
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}}"

Kai Stian Olstad

unread,
Sep 30, 2018, 11:56:13 AM9/30/18
to ansible...@googlegroups.com
On Sunday, 30 September 2018 16.57.12 CEST vinod...@gmail.com wrote:
> 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 ?

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


--
Kai Stian Olstad


Message has been deleted
Message has been deleted

vinod...@gmail.com

unread,
Sep 30, 2018, 12:46:58 PM9/30/18
to Ansible Project
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

Kai Stian Olstad

unread,
Sep 30, 2018, 1:28:40 PM9/30/18
to ansible...@googlegroups.com
On Sunday, 30 September 2018 18.46.58 CEST vinod...@gmail.com wrote:
> 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)

A typo, the -s should have been -S

Try this

vinoth kumar

unread,
Sep 30, 2018, 2:04:40 PM9/30/18
to ansible...@googlegroups.com
Forget to mention it. It gives me an error

--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5437548.uDZtN8urMM%40x1.
For more options, visit https://groups.google.com/d/optout.

vinod...@gmail.com

unread,
Oct 1, 2018, 2:40:24 AM10/1/18
to Ansible Project
Her is the error KAi, so I have changed to small 's'

 lsblk -Snlo name
lsblk: invalid option -- 'S'
Usage:
 lsblk [options] [<device> ...]
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 <list> exclude devices by major number (default: RAM disks)
 -I, --include <list> 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 <list>  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

Kai Stian Olstad

unread,
Oct 1, 2018, 5:59:57 AM10/1/18
to ansible...@googlegroups.com
On 01.10.2018 08:40, vinod...@gmail.com wrote:
> Her is the error KAi, so I have changed to small 's'
>
> lsblk -Snlo name
> lsblk: invalid option -- 'S'

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 |

vinod...@gmail.com

unread,
Oct 1, 2018, 11:14:06 AM10/1/18
to Ansible Project
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

Brad Van Orden

unread,
Oct 1, 2018, 11:43:13 AM10/1/18
to ansible...@googlegroups.com
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.

--
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 post to this group, send email to ansible...@googlegroups.com.

Kai Stian Olstad

unread,
Oct 1, 2018, 11:50:25 AM10/1/18
to ansible...@googlegroups.com
On Monday, 1 October 2018 17.14.05 CEST vinod...@gmail.com wrote:
> 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)'

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:

vinod...@gmail.com

unread,
Oct 2, 2018, 8:46:11 AM10/2/18
to Ansible Project
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


On Sunday, September 30, 2018 at 4:57:12 PM UTC+2, vinod...@gmail.com wrote:

Brad Van Orden

unread,
Oct 2, 2018, 10:50:22 AM10/2/18
to Ansible Project
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}}"

vinod...@gmail.com

unread,
Oct 2, 2018, 11:05:55 AM10/2/18
to Ansible Project
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.

Brad Van Orden

unread,
Oct 2, 2018, 11:31:44 AM10/2/18
to Ansible Project
I tried to update the task to do your multiple disks.  Did you give it a test run?

Kai Stian Olstad

unread,
Oct 2, 2018, 11:36:25 AM10/2/18
to ansible...@googlegroups.com
On Tuesday, 2 October 2018 14.46.11 CEST vinod...@gmail.com wrote:
> 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 %}"


--
Kai Stian Olstad


vinod...@gmail.com

unread,
Oct 3, 2018, 11:15:41 AM10/3/18
to Ansible Project
Ohhhh hoooooooooooooooooooooo, OMG, kai you are really great. thanks for supporting.

@ban thanks a lot for your time:

you both made my day


On Sunday, September 30, 2018 at 4:57:12 PM UTC+2, vinod...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages