How to remove every part, except the end from elements in a list in ansible?

Viewed 72

I have a problem to edit remove every part, except the end, of every elements in a list. The list looks like this at the beginning:

['/dev/sda1', '/dev/sdb1', '/dev/sdc1', '/mnt/xxx/yyy']

Now i want to edit it, so i only have the last part of every element left. So the list should be look like this at the end:

['/sda1', '/sdb1', '/sdc1', '/yyy']

I wanted to loop through this list and edit every element but i don't know how i can regex this. Does anybody has an idea? Thanks for everybody who look at this!

1 Answers

The task below does the job

    - debug:
        msg: "{{ my_list|map('regex_replace', my_regex, my_replace)|list }}"
      vars:
        my_list: ['/dev/sda1', '/dev/sdb1', '/dev/sdc1', '/mnt/xxx/yyy']
        my_regex: '^.*/(.*)$'
        my_replace: '/\1'

gives

  msg:
  - /sda1
  - /sdb1
  - /sdc1
  - /yyy
Related