# Strange behavior of Jinja2 expression for same input vars

**URL:** <https://forum.ansible.com/t/strange-behavior-of-jinja2-expression-for-same-input-vars/21930>\
**Category:** Ansible Project\
**Created:** [July 6, 2016, 9:59am UTC](https://forum.ansible.com/t/strange-behavior-of-jinja2-expression-for-same-input-vars/21930 "2016-07-06T09:59:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cosbug](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@cosbug](https://forum.ansible.com/u/cosbug)\
**Post date:** [July 6, 2016, 9:59am UTC](https://forum.ansible.com/t/strange-behavior-of-jinja2-expression-for-same-input-vars/21930/1 "2016-07-06T09:59:59Z")

</div>

Hi All,

I’m using a Jinja2 expression to exclude form one list the elements present in another list but it doesn’t work as expected.

Here is an extract from a larger playbook which captures the input and out put before and after ‘set\_fact’ statement:

`

- debug: var=excluded\_ids

- debug: var=instance\_ids

- name: Exclude them from instance\_ids  
set\_fact: instance\_ids1=“{% for item in instance\_ids if item not in excluded\_ids %}{{ item }}{% endfor %}”

- debug: var=instance\_ids1  
`

and here the strange output of **instance\_ids1** variable:

`  
TASK: [debug var=excluded\_ids] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“var”: {  
“excluded\_ids”: [  
“i-bae34407”  
]  
}  
}

TASK: [debug var=instance\_ids] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“var”: {  
“instance\_ids”: [  
“i-bae34407”,  
“i-d2e4436f”  
]  
}  
}

TASK: [Exclude them from instance\_ids] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {“ansible\_facts”: {“instance\_ids1”: “ **u, ud26f** ”}}

TASK: [debug var=instance\_ids1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“var”: {  
“instance\_ids1”: “ **u, ud26f** ”  
}  
}  
`

As you can see I get some rubbish in **instance\_ids1**.

But if I run a separate playbook defined like this:

`

---

<div class="post-metadata">

**Author:** ![Aneesh\_Joseph](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Aneesh\_Joseph](https://forum.ansible.com/u/Aneesh_Joseph)\
**Post date:** [July 6, 2016, 11:53am UTC](https://forum.ansible.com/t/strange-behavior-of-jinja2-expression-for-same-input-vars/21930/2 "2016-07-06T11:53:28Z")

</div>

have you tried the difference filter - [http://docs.ansible.com/ansible/playbooks\_filters.html](http://docs.ansible.com/ansible/playbooks_filters.html)

I’m not sure if you can combine a for loop and an if condition like that, I would have used something like this

`set_fact: instance_ids1="{{ instance_ids | difference(excluded_ids) }}"`

or

`  
set\_fact: instance\_ids1=“{% for item in instance\_ids %}{% if item not in excluded\_ids %}{{ item }}{% endif %}{% endfor %}”

`

---

<div class="post-metadata">

**Author:** ![cosbug](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@cosbug](https://forum.ansible.com/u/cosbug)\
**Post date:** [July 6, 2016, 2:08pm UTC](https://forum.ansible.com/t/strange-behavior-of-jinja2-expression-for-same-input-vars/21930/3 "2016-07-06T14:08:42Z")

</div>

Thanks Aneesh,

The first suggestion brings this:

`  
TASK: [Exclude them from instance\_ids] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {“ansible\_facts”: {“instance\_ids1”: “set([u’ ‘, u’d’, u’f’, u’,‘, u’2’, u’u’, u’6’])”}}

TASK: [debug var=instance\_ids1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“var”: {  
“instance\_ids1”: “set([u’ ‘, u’d’, u’f’, u’,‘, u’2’, u’u’, u’6’])”  
}  
}

`

the second one doesn’t work either (already tried before posting this 🙂 ).

Tested with Ansible version 1.9.4 & 2.2.0

---

<div class="post-metadata">

**Author:** ![Aneesh\_Joseph](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Aneesh\_Joseph](https://forum.ansible.com/u/Aneesh_Joseph)\
**Post date:** [July 7, 2016, 3:59am UTC](https://forum.ansible.com/t/strange-behavior-of-jinja2-expression-for-same-input-vars/21930/4 "2016-07-07T03:59:33Z")

</div>

Hey, tried both in 2.2.0 and they work for me

`

---

<div class="post-metadata">

**Author:** ![cosbug](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@cosbug](https://forum.ansible.com/u/cosbug)\
**Post date:** [July 7, 2016, 5:37am UTC](https://forum.ansible.com/t/strange-behavior-of-jinja2-expression-for-same-input-vars/21930/5 "2016-07-07T05:37:28Z")

</div>

Yes, they work both in a test script where both the excluded\_ids and instance\_ids variables are defined manually but not in original playbook in which these variables are registered as outputs from other modules/tasks  
despite the debug shows that their contents are the same.
