problem with conditionals and matching a variable in dictionary list

1,236 views
Skip to first unread message

Iryna Oliinykova

unread,
Sep 28, 2017, 11:19:28 AM9/28/17
to Ansible Project

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


------------
- 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: '"{{ username }}" in "{{ system_users }}"'

------------

This play must create a new user only if this user is listed in system_users:


------------
# 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
------------


But when I try to change the syntax as indicated below, this task is skipped:


------------
  - name: Create new user
    user:
      name: "{{ username }}"
      shell: "/bin/bash"
      state: present
      groups: wheel
    when: username in system_users

------------


------------

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
------------

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:

------------
- name: Create new user user: name: "{{ username }}" shell: "/bin/bash" state: present groups: wheel when: username in item.value with_dict: "{{ 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

Kai Stian Olstad

unread,
Sep 28, 2017, 12:01:20 PM9/28/17
to ansible...@googlegroups.com
On 28. sep. 2017 17:19, Iryna Oliinykova wrote:
> The following play is executed with ansible-playbook play.yml -e
> username=user1
>
> ------------
> *- 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: **'"{{ username }}" in "{{ system_users }}"**'*

This should work

when: username in system_user | map(attribute='name')

--
Kai Stian Olstad

Iryna Oliinykova

unread,
Sep 28, 2017, 12:07:10 PM9/28/17
to Ansible Project
Thank you, it has worked perfectly!

Regards,
Iryna

Brian Coca

unread,
Sep 28, 2017, 12:07:53 PM9/28/17
to Ansible Project
Not sure why the loop "doesn't work" .. it should but not enough
details to debug, in any case something similar to the following
should also 'work'.

when: system_user |selectattr(attribute='name', 'equalsto', username)


----------
Brian Coca

Iryna Oliinykova

unread,
Sep 28, 2017, 12:40:39 PM9/28/17
to Ansible Project

Le jeudi 28 septembre 2017 18:07:53 UTC+2, Brian Coca a écrit :
Not sure why the loop "doesn't work" .. it should but not enough
details to debug,

 It seems that it doesn't work because it isn't considered as a dictionary but rather as a list of dictionary.

in any case something similar to the following
should also 'work'.

when: system_user |selectattr(attribute='name', 'equalsto', username)


Unfortunately it doesn't work:

TASK [Create new user] ***********************************************************************************************************************************************************************************************************************
fatal: [29254.monit.sewan.fr]: FAILED! => {"failed": true, "msg": "The conditional check 'system_users |selectattr(attribute='name', 'equalto', username)' failed. The error was: template error while templating string: invalid syntax for function call expression. String: {% if system_users |selectattr(attribute='name', 'equalto', username) %} True {% else %} False {% endif %}\n\nThe error appears to have been in '/home/ioliinykova/Ansible/playbooks/temp.yml': line 15, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: Create new user\n    ^ here\n"}


And what I cannot understand is why it keeps working like this:
when: ''"{{ username }}" in "{{ system_users }}"'

It generates a warning but it works....


Regards,
Iryna Oliinykova






Reply all
Reply to author
Forward
0 new messages