We’ve been using an (openstack based) cloud provider, that can’t guarantee a stable device name for an attached volume.
This was causing problems when used in /etc/fstab; on reboot, if the device name was incorrect, the instance would hang.
It’s pretty straight forward to use the UUID instead, with ansible:
- name: Mount vol
become: yes
mount:
path: "{{ mount_point }}"
src: "UUID={{ ansible_devices[device_name].partitions[device_name + '1'].uuid }}"
fstype: ext4
state: mounted
but we still needed the device_name
in group vars. Our provider explained that a stable id was provided, in /dev/disk/by-id, which could be used directly for most tasks:
- name: Create a new primary partition
parted:
device: "/dev/disk/by-id/{{ device_id }}"
number: 1
state: present
become: yes
- name: Create ext4 filesystem on vol
become: yes
filesystem:
fstype: ext4
dev: "/dev/disk/by-id/{{ device_id }}-part1"
But how do you get from the id, to the device name?
$ ls /dev/disk/by-id/
virtio-c11c38e5-7021-48d2-a virtio-c11c38e5-7021-48d2-a-part1
"ansible_devices": {
"vda": {
...
},
"vdb": {
...
},
"vdc": {
...
"links": {
"ids": [
"virtio-c11c38e5-7021-48d2-a"
],
...
},
...
}
}
This seemed like a job for json_query
but, after a fruitless hour or two, I gave up and used this (slightly hacky) solution suggested on SO:
- name: Get device name
set_fact:
device_name: "{{ item.key }}"
with_dict: "{{ ansible_devices }}"
when: "(item.value.links.ids[0] | default()) == device_id"
no_log: yes