Hello,
I have a problem with when conditionals when trying to match a variable that makes part of a dictionary list.
It worked perfectly until Ansible version update till 2.3.
Since in new version there are the following warnings:
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}
I tried to fix the syntax, but the condition is no longer matched.
Normally it should not have worked even earlier because it must match a value in the following list:
[{u'name': u'user1'}, {u'name': u'user2'}, {u'name': u'user3'},…]
, so it should have returned an error message type mismatch.
For the moment I cannot find a way to match this variable at all.
Steps to reproduce:
The following play is executed with ansible-playbook play.yml -e username=user1
------------
<b>- hosts: testvm
vars:
system_users:
- name : user1
- name : user2
- name : user3
tasks:
- name: Create new user
user:
name: "{{ username }}"
shell: "/bin/bash"
state: present
groups: wheel
when: </b>**'"{{ username }}" in "{{ system_users }}"****'**
------------
This play must create a new user only if this user is listed in system_users:
------------
<b># ansible-playbook play.yml -e username=user1
PLAY [testvm] ******************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [testvm]
TASK [testvm] ***********************************************************************************************************************************************************************************************************************
changed: [testvm]
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
testvm : ok=2 changed=1 unreachable=0 failed=0
</b>
------------
But when I try to change the syntax as indicated below, this task is skipped:
------------
<b> - name: Create new user
user:
name: "{{ username }}"
shell: "/bin/bash"
state: present
groups: wheel
when: </b>**username in system_users**
------------
------------
<b>TASK [Create new user] ***********************************************************************************************************************************************************************************************************************
task path: /etc/ansible/playbooks/play.yml:15
skipping: [testvm] => {
"changed": false,
"skip_reason": "Conditional result was False",
"skipped": true
}
META: ran handlers
META: ran handlers
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
29254.monit.sewan.fr : ok=1 changed=0 unreachable=0 failed=0 </b>
------------
I have tried to open an issue https://github.com/ansible/ansible/issues/31043 where they have advised me to rather do it in the following way:
------------
<b>- name: Create new user
user:
name: "{{ username }}"
shell: "/bin/bash"
state: present
groups: wheel
when: username in item.value
with_dict: </b>**"{{ system_users }}****"**
------------
But it didn't work:
------------
__TASK [Create new user] ***********************************************************************************************************************************************************************************************************************__
**fatal: [testvm]: FAILED! => {"failed": true, "msg": "with_dict expects a dict"}**
__PLAY RECAP ***********************************************************************************************************************************************************************************************************************************__
**testvm : ok=1 changed=0 unreachable=0 failed=1**
------------
I guess it's beceause of the fact that it's not a dictionary but a list of dictionary.
Have somebody already faced this issue?
Regards,
Iryna