problem with conditionals and matching a variable in dictionary list

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

This should work

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

Thank you, it has worked perfectly!

Regards,
Iryna

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)

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

But it can be related to the jinja version as indicated here: https://stackoverflow.com/questions/31895602/ansible-filter-a-list-by-its-attributes#comment66065595_31896249

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