Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading