In an ansible playbook I want to loop a task over a set of usernames from one list only if the username or username followed by a suffix '.A' occurs in another list.
Sample playbook that fails:
---
- hosts: localhost
vars:
- users1:
- alice
- alice.A
- bob
- santa
- users2:
- alice
tasks:
- debug:
msg: "Do something for {{ item }} realname {{ item | regex_replace('^([a-z]+)\\.[aA]$', '\\1') }}"
loop: "{{ users1 }}"
when:
- "item | regex_replace('^([a-z]+)\\.[aA]$', '\\1') in users2"
This version skips alice.A while the regex_replace filter in the task itself returns alice as expected.
Actual output:
TASK [debug] ***************
ok: [localhost] => (item=alice) => {}
MSG:
Do something for alice realname alice
skipping: [localhost] => (item=alice.A)
skipping: [localhost] => (item=bob)
skipping: [localhost] => (item=santa)
Desired output:
TASK [debug] ***************
ok: [localhost] => (item=alice) => {}
MSG:
Do something for alice realname alice
Do something for alice.A realname alice
skipping: [localhost] => (item=bob)
skipping: [localhost] => (item=santa)
Is it possible to apply a filter to the item to be tested?
If not is there a way to use something like map() to duplicate the entries in the users2 list appending the '.A' suffix to each item?
When conditions I have tried that all fail:
when: ("item in users2 | flatten(1)") or
("item|regex_search('\.A$')")
when:
- "item | regex_replace('^([a-z]+\\.[a-z]+).[aA]$', '\\1') in users | flatten(1)"
when:
- "item in users | flatten(1) | map('regex_replace','^([a-z]+\\.[a-z]+)$', '\\1.A')"
when:
- "item | map('regex_replace','^([a-z]+\\.[a-z]+).[aA]$', '\\1') in users | flatten(1)"
when:
- "item|map('upper') in users | flatten(1)"
The last one just to check whether the item tested was modified by the filter or not. (It is not AFAICT.)