Score:0

How to print a specific line from file based on UUID defined in variable?

na flag

I wanted to print the specific line from /etc/fstab based on UUID defined in a variable. But when I'm trying with below code it is not giving proper results.

Code:

---
- name: check
  hosts: all
  become: true

  tasks:

    - name: set fact for uuid
      set_fact:
        uuid_disk: "{{ item.value.links.uuids }}"
      loop: "{{ ansible_devices | dict2items }}"
      when: ('swap' in (item.value.links.labels))

    - name: print
      debug:
        msg: "{{ uuid_disk | join() }}"

    - name: fstab
      slurp:
        src: /etc/fstab
      register: output

    - name: print
      debug:
        msg: "{{ item | select('match','UUID') | list }}"
      loop: "{{ (output.content | b64decode).splitlines() }}"

    - name: print
      debug:
        msg: "{{ item }}"
      loop: "{{ (output.content | b64decode).splitlines() }}"
      when:  (item | regex_search('^UUID')) in uuid_disk
Priyanka avatar
na flag
uuid_disk output is : "6e87632e-b052-4ef6-a91b-78f1be1a99c5"
U880D avatar
ca flag
Do you mean like `when: uuid_disk in item`?
Score:2
br flag

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

  1. 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
  1. 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
  1. 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
Score:1
ca flag

Regarding

How to print the specific line from file based on UUID defined in variable?

you may have a look into the following minimal example playbook

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    uuid_disk: 12345678-abcd-efgh-ijkl-123456789012

  tasks:

  - name: fstab
    slurp:
      src: /etc/fstab
    register: output

  - name: Print UUID, if there is any
    debug:
      msg: "{{ item | regex_search('[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}') }}"
    loop: "{{ (output.content | b64decode).splitlines() }}"

  - name: Print line where UUID match
    debug:
      msg: "{{ item }}"
    loop: "{{ (output.content | b64decode).splitlines() }}"
    when: uuid_disk in item

and Similar Q&A about

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.