Score:0

Ansible regular expression to match a string from a list of line

na flag

I'm trying to read a content of file from remote file. but i want the message to be displayed only matched UUID from fstab file. but when i ran below playbook it is throwing below error.

playbook:

- name: read content from a file
  hosts: all
  become: true
  tasks:
    - name: read a content
      slurp:
        src: /etc/fstab
      register: output
    - name: print
      debug:
       msg: "{{ (output['content'] | b64decode).split('\n') | regex_search('^\\S+/UUID\b.*', multiline=True) }}"

error:

TASK [print] *************************************************************************************************************
FAILED! => {"msg": "Unexpected templating type error occurred on ({{ (output['content'] | b64decode).split('\n') | regex_search('^\\\\S+/UUID\b.*', multiline=True) }}): expected string or bytes-like object"}
Priyanka avatar
na flag
I want to fetch the specific entry for the nfs share. for eg: 10.10.0.10:/nfsshareid /var/test nfs defaults 0 0 ------------> from here i want to grep with nfsshareid.
Priyanka avatar
na flag
getting the o/p like below when i'm trying to fetch with nfsshareid--------------- "msg": "<generator object select_or_reject at 0x7f4fbd87d9e8>"
Score:1
pt flag

First, when I run your playbook I don't get an error; I just get no output from the debug task:

"msg": ""

If you're looking for a list of lines that start with UUID, you could do this:

- name: read content from a file
  hosts: localhost
  tasks:
    - name: read a content
      slurp:
        src: /etc/fstab
      register: output
    - name: print
      debug:
       msg: >
         {{
           (output['content'] | b64decode).splitlines() |
           select("match", "UUID")
         }}

On my system, this produces:

ok: [localhost] => {
    "msg": [
        "UUID=2fbad834-42b2-48fd-868d-3eea12e144de /                       btrfs   subvol=root00,compress=zstd:1 0 0",
        "UUID=c72e509c-453a-4e62-8cea-23ad112765d3 /boot                   ext4    defaults        1 2",
        "UUID=F3B3-4F58          /boot/efi               vfat    umask=0077,shortname=winnt 0 2",
        "UUID=2fbad834-42b2-48fd-868d-3eea12e144de /home                   btrfs   subvol=home00,compress=zstd:1 0 0",
    ]
}

If you wanted a line matching a specific uuid, you could do this:

- name: read content from a file
  hosts: localhost
  tasks:
    - name: read a content
      slurp:
        src: /etc/fstab
      register: output
    - name: print
      debug:
       msg: >
         {{
           (output['content'] | b64decode).splitlines() |
           select("match", "UUID=c72e509c-453a-4e62-8cea-23ad112765d3") |
           first
         }}

On my system this produces:

ok: [localhost] => {
    "msg": "UUID=c72e509c-453a-4e62-8cea-23ad112765d3 /boot                   ext4    defaults        1 2\n"
}
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.