Ansible regex to extract path from a list of strings excluding softlink notation

Viewed 288

I have to extract paths from a list of strings excluding the softlink maps.
Example list:

list1:
  - 'lrwxrwxrwx 1 usr grp   18 Nov 21 19:53 /path/dummy_link -> /some/file'
  - '-rw-r--r-- 1 usr grp    0 Nov 21 17:50 /path/file1.txt'
  - 'drwxr-xr-x 1 usr grp 4096 Nov 21 17:51 /path/dir1'

I am trying with map and regex_replace as below:

- debug:
    msg: "{{ list1 | map('regex_replace',regxp,bckref) | list }}"
  vars:
    regxp: '^.+(/path/.+)(.*?)$'
    bckref: '\1'

and the result I am getting is:

"msg": [
    "/path/dummy_link -> /some/file",
    "/path/file1.txt",
    "/path/dir1"
]

The expected output is:

"msg": [
    "/path/dummy_link",
    "/path/file1.txt",
    "/path/dir1"
]

What could be the regex to get rid of the substring -> /some/file which will not be present in all the lines?

1 Answers

You can use

{{ list1 | map('regex_replace','^.+(/path/\\S+).*','\\1') | list }}

Or, with regex_search:

{{ list1 | map('regex_search','/path/\\S+') | list }}

The ^.+(/path/\S+).* regex matches

  • ^ - start of string
  • .+ - one or more chars other than line break chars, as many as possible
  • (/path/\S+) - Group 1: /path/ + one or more non-whitespace chars
  • .* - zero or more chars other than line break chars, as many as possible.

The \1 replaces with the Group 1 value.

Related