How to fetch only the UUID by passing in the size of the disk?

712 Views Asked by At

I am trying to mount a disk using ansible automatically using the following command:

fdisk -l | grep "64G" | grep -Eo "/dev/sd."

This outputs /dev/sda1 and I am adding that to the /etc/fstab/. But I want to add the UUID instead.

Is there a method to fetch the UUID by passing the size of the disk? (I have only 1 disk attached with 64G of size so it returns only one output, want to know how to fetch the UUID instead of /dev/sda )

1

There are 1 best solutions below

0
On BEST ANSWER

You can use the blkid command passing in a specific device - e.g. blkid /dev/sda1

or you can browse the directory /dev/disk/by-uuid/, which has the uuids represented as symbolic links to the device nodes e.g. ls -l /dev/disk/by-uuid/ | grep /dev/sda1

with ansible though, I think you can access the uuids and the device tree from ansible facts, which are collected for each host and accessible under the ansible_facts dictionary

https://docs.ansible.com/ansible/latest/user_guide/playbooks_vars_facts.html#ansible-facts

so it may be possible to construct an ansible when conditional clause against the ansible_devices and attach that to a mount task