Skip to content

Instantly share code, notes, and snippets.

@halberom
Created May 17, 2017 09:35
Show Gist options
  • Save halberom/9bfe009c9a3df5d64c7bb505a7700430 to your computer and use it in GitHub Desktop.
Save halberom/9bfe009c9a3df5d64c7bb505a7700430 to your computer and use it in GitHub Desktop.
ansible - example of using map + regex_replace to modify all items in a list
---
- hosts: localhost
connection: local
gather_facts: False
vars:
mylist:
- group_1
- group_2
tasks:
- debug:
msg: "{{ mylist | map('regex_replace', '(.*)', '\\1_foo') | list }}"
PLAY [localhost] **********************************************************************************************************************
TASK [debug] **************************************************************************************************************************
ok: [localhost] => {
"changed": false,
"msg": [
"group_1_foo",
"group_2_foo"
]
}
PLAY RECAP ****************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
@gobenji
Copy link

gobenji commented Mar 13, 2020

Indeed, I got strange results if the regex was not achored at the beginning: "^..."
Other than that, thanks for this useful pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment