Q: "Print the line from the file based on the UUID defined in a variable."
A: Fetch the file and select the line. For example, declare the path to store /etc/fstab on the controller
fstab_path: "/tmp/test/{{ inventory_hostname }}/fstab"
and fetch the file
- fetch:
src: /etc/fstab
dest: "{{ fstab_path }}"
flat: true
Given the variable with the UUID value
uuid_disk: 01865fce-8bb9-48ad-a9eb-1ff43a8db4a5
search the line
uuid_line: "{{ lookup('file', fstab_path).splitlines()|
select('search', uuid_disk) }}"
gives for example,
uuid_line:
- UUID=01865fce-8bb9-48ad-a9eb-1ff43a8db4a5 none swap sw 0 0
Example of a complete playbook for testing
- hosts: all
vars:
uuid_disk: 01865fce-8bb9-48ad-a9eb-1ff43a8db4a5
fstab_path: "/tmp/test/{{ inventory_hostname }}/fstab"
uuid_line: "{{ lookup('file', fstab_path).splitlines()|
select('search', uuid_disk) }}"
tasks:
- fetch:
src: /etc/fstab
dest: "{{ fstab_path }}"
flat: true
- debug:
var: uuid_line
The above solution prints the fstab lines of UUID(s) labeled swap. (This is how you get the UUID.) Vice-versa you can print the partitions used as swap in fstab. Let me rephrase the question
Q: "From /etc/fstab get UUID of swap partition(s). Then, display the partition(s)."
A: First gather the facts and create a dictionary UUID/partition. Then, parse /etc/fstab and get the UUID of the swap entries. Get the partition(s) of the UUID(s).
- Gather the facts
- setup:
gather_subset: devices
, get the partitions, and create the dictionary uuid
partitions: "{{ ansible_devices|
json_query('*.partitions')|
combine }}"
uuid: "{{ dict(partitions|
dict2items|
selectattr('value.uuid')|
json_query('[].[value.uuid, key]')) }}"
gives for example,
uuid:
01865fce-8bb9-48ad-a9eb-1ff43a8db4a5: sdb4
04dc9170-bdbc-4a22-abaf-b9e3cf1ba969: sda5
7074BA0A74B9D2D8: sda2
9a2199dd-0662-47b4-a957-adbcf5d350f4: sdb5
EC808480808452CE: sda1
F86C-A380: sdb2
c484594d-fd2e-4f57-9c14-74b8e397d8ed: sdb3
- Declare the path to store /etc/fstab on the controller
fstab_path: "/tmp/test/{{ inventory_hostname }}/fstab"
and fetch the file
- fetch:
src: /etc/fstab
dest: "{{ fstab_path }}"
flat: true
Use the filter community.general.jc to create the list of dictionaries fstab
fstab: "{{ lookup('file', fstab_path)|
community.general.jc('fstab') }}"
gives for example,
fstab:
- fs_file: /
fs_freq: 0
fs_mntops: errors=remount-ro
fs_passno: 1
fs_spec: UUID=c484594d-fd2e-4f57-9c14-74b8e397d8ed
fs_vfstype: ext4
- fs_file: /boot/efi
fs_freq: 0
fs_mntops: umask=0077
fs_passno: 1
fs_spec: UUID=F86C-A380
fs_vfstype: vfat
- fs_file: /export
fs_freq: 0
fs_mntops: defaults
fs_passno: 2
fs_spec: UUID=9a2199dd-0662-47b4-a957-adbcf5d350f4
fs_vfstype: ext4
- fs_file: none
fs_freq: 0
fs_mntops: sw
fs_passno: 0
fs_spec: UUID=01865fce-8bb9-48ad-a9eb-1ff43a8db4a5
fs_vfstype: swap
- fs_file: none
fs_freq: 0
fs_mntops: sw
fs_passno: 0
fs_spec: /usr/swap0
fs_vfstype: swap
Get the UUID(s) of the swap entries
uuid_swap: "{{ fstab|
selectattr('fs_vfstype', '==', 'swap')|
selectattr('fs_spec', 'match', 'UUID=')|
map(attribute='fs_spec')|
map('split', '=')|
map('last') }}"
gives
uuid_swap:
- 01865fce-8bb9-48ad-a9eb-1ff43a8db4a5
- Get the partition(s) of the UUID(s)
partition_swap: "{{ uuid_swap|map('extract', uuid) }}"
gives
partition_swap:
- sdb4
Example of a complete playbook for testing
- hosts: all
vars:
partitions: "{{ ansible_devices|
json_query('*.partitions')|
combine }}"
uuid: "{{ dict(partitions|
dict2items|
selectattr('value.uuid')|
json_query('[].[value.uuid, key]')) }}"
fstab_path: "/tmp/test/{{ inventory_hostname }}/fstab"
fstab: "{{ lookup('file', fstab_path)|
community.general.jc('fstab') }}"
uuid_swap: "{{ fstab|
selectattr('fs_vfstype', '==', 'swap')|
selectattr('fs_spec', 'match', 'UUID=')|
map(attribute='fs_spec')|
map('split', '=')|
map('last') }}"
partition_swap: "{{ uuid_swap|map('extract', uuid) }}"
tasks:
- setup:
gather_subset: devices
- debug:
var: uuid
- fetch:
src: /etc/fstab
dest: "{{ fstab_path }}"
flat: true
- debug:
var: fstab
- debug:
var: uuid_swap
- debug:
var: partition_swap