The special variable ansible_role_names
contains a list with roles names in my play. For example
"ansible_role_names": [
"geerlingguy.nfs",
"ANXS.openssh",
"robertdebock.bootstrap",
"robertdebock.squid"
]
However what I want to access is this list with everything before the dot removed.
"roles": [
"nfs",
"openssh",
"bootstrap",
"squid" ]
Q: "How to manipulate each item in an array when using Ansible?"
A: It's possible to map
the regex_replace filter. For example the play
- set_fact:
my_list: "{{ ansible_role_names|
map('regex_replace', regex, replace)|
list }}"
vars:
regex: '^(.*)\.(.*)$'
replace: '\2'
- debug:
var: my_list
gives
"my_list": [
"nfs",
"openssh",
"bootstrap",
"squid"
]
regex: '^(.*)\.(.*)$'
^
matches the beginning of the string(.*)
matches any character in capture group No.1\.
matches .
(.*)
matches any character in capture group No.2$
matches the end of the stringreplace: '\2'
\2
matches previously defined capture group No.2