# problem with conditionals and matching a variable in dictionary list

**URL:** <https://forum.ansible.com/t/problem-with-conditionals-and-matching-a-variable-in-dictionary-list/25626>\
**Category:** Ansible Project\
**Created:** [September 28, 2017, 3:19pm UTC](https://forum.ansible.com/t/problem-with-conditionals-and-matching-a-variable-in-dictionary-list/25626 "2017-09-28T15:19:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iryna\_Oliinykova](https://avatars.discourse-cdn.com/v4/letter/i/da6949/32.png) [@Iryna\_Oliinykova](https://forum.ansible.com/u/Iryna_Oliinykova)\
**Post date:** [September 28, 2017, 3:19pm UTC](https://forum.ansible.com/t/problem-with-conditionals-and-matching-a-variable-in-dictionary-list/25626/1 "2017-09-28T15:19:27Z")

</div>

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`

```auto
------------
<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:

```auto
------------
<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:

```auto
------------

```

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

```

```auto

```

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 28, 2017, 4:00pm UTC](https://forum.ansible.com/t/problem-with-conditionals-and-matching-a-variable-in-dictionary-list/25626/2 "2017-09-28T16:00:50Z")

</div>

This should work

&nbsp;&nbsp;&nbsp;when: username in system\_user | map(attribute='name')

---

<div class="post-metadata">

**Author:** ![Iryna\_Oliinykova](https://avatars.discourse-cdn.com/v4/letter/i/da6949/32.png) [@Iryna\_Oliinykova](https://forum.ansible.com/u/Iryna_Oliinykova)\
**Post date:** [September 28, 2017, 4:07pm UTC](https://forum.ansible.com/t/problem-with-conditionals-and-matching-a-variable-in-dictionary-list/25626/3 "2017-09-28T16:07:09Z")

</div>

Thank you, it has worked perfectly!

Regards,  
Iryna

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 28, 2017, 4:07pm UTC](https://forum.ansible.com/t/problem-with-conditionals-and-matching-a-variable-in-dictionary-list/25626/4 "2017-09-28T16:07:32Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Iryna\_Oliinykova](https://avatars.discourse-cdn.com/v4/letter/i/da6949/32.png) [@Iryna\_Oliinykova](https://forum.ansible.com/u/Iryna_Oliinykova)\
**Post date:** [September 28, 2017, 4:40pm UTC](https://forum.ansible.com/t/problem-with-conditionals-and-matching-a-variable-in-dictionary-list/25626/5 "2017-09-28T16:40:39Z")

</div>

> 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](http://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](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
