Finding unpartitioned drives

544 views
Skip to first unread message

KK

unread,
Sep 7, 2017, 2:14:58 AM9/7/17
to Ansible Project
Hi 

Am having trouble to find unpartitioned drives through ansible.
I want all the unpartitioned drives put to a variable

example:

I have sda,sdb and sdc where sda is partitioned and sdb,sdc is left unpartitioned. I want the playbook to register both sub,sac to a variable 

something like

    "unpartitioned_drives": {
       "stdout_lines": [
            "/dev/sdb",
            "/dev/sdc"

Currently am doing this with shell which I want to do with ansible 

  - name: Finding Unpartiotioned drives
    shell: for i in /dev/sd[b-z];do /sbin/partprobe -d -s $i | grep -v 1 | cut -d':' -f1;done
    register: unpartitioned_drives
  - debug: var=unpartitioned_drives


I tried doing this but all am getting is last drive but not both with this code.

  - name: setfact
    set_fact: unpartitioned_drives="{{ item.key }}"
    when: not item.value.partitions and item.key != "sr0" and item.key != "fd0"
    with_dict: "{{ ansible_devices }}"
  - debug: var=unpartitioned_drives



Output is 

TASK [debug] ***********************************************************************************************************************************************************************************************
    "changed": false,
    "unpartitioned_drives": "sdc"
}

Since the last item is sdc it only stores sdc. Can anyone help to use this to store all unpartitioned drives in a single variable.



Jean-Yves LENHOF

unread,
Sep 7, 2017, 5:34:47 AM9/7/17
to ansible...@googlegroups.com
Hi,

Have a look here to append result after each iteration of a loop
https://stackoverflow.com/questions/35605603/using-ansible-set-fact-to-create-a-dictionary-from-register-results

Be careful that your device can be unpartitioned, but can be used by lvm
directly (without partition)... So you probably need to remove the
results of the command pvs from your list

Regards,

JYL

KK

unread,
Sep 7, 2017, 4:16:24 PM9/7/17
to Ansible Project
Thanks JYL for the information. Is there any easy way to get hold of the drives rather than looping over the registered value.

Jean-Yves LENHOF

unread,
Sep 7, 2017, 4:55:00 PM9/7/17
to ansible...@googlegroups.com

ansible_devices could interesting to look after...

--
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/c870f711-2b1a-4cdf-ad30-81c879691566%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages