# Conditional evaluation of lists

**URL:** <https://forum.ansible.com/t/conditional-evaluation-of-lists/17863>\
**Category:** Ansible Project\
**Created:** [March 27, 2015, 10:12pm UTC](https://forum.ansible.com/t/conditional-evaluation-of-lists/17863 "2015-03-27T22:12:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev2](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Anand\_Buddhdev2](https://forum.ansible.com/u/Anand_Buddhdev2)\
**Post date:** [March 27, 2015, 10:12pm UTC](https://forum.ansible.com/t/conditional-evaluation-of-lists/17863/1 "2015-03-27T22:12:58Z")

</div>

I want to install some packages on a server. Additionally, if a certain condition is true, I want to add an extra package to the list of packages to be installed. What I would love is have this:

- name: install packages  
yum: name={{ item }}  
with\_items:
- package1
- package2  
{% if condition %}
- package3  
{% endif %}

Now, I know that ansible doesn’t parse playbooks with the full Jinja engine (though that would be so cool). I could of course achieve this with 2 tasks:

- name: install packages  
yum: name={{ item }}  
with\_items:

- package1

- package2

- name: install packages  
yum: name=package3

when: condition

But my question is: how can I achieve this with just one task?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 27, 2015, 10:18pm UTC](https://forum.ansible.com/t/conditional-evaluation-of-lists/17863/2 "2015-03-27T22:18:12Z")

</div>

try:

with\_items:  
&nbsp;&nbsp;- p1  
&nbsp;&nbsp;- p2  
&nbsp;&nbsp;- "{{ condition|ternary(p3,'') }}"

or

with\_items: "{{ ['p1','p2'] |union(condition|ternary(['p3'],)) }}"

---

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev2](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Anand\_Buddhdev2](https://forum.ansible.com/u/Anand_Buddhdev2)\
**Post date:** [March 27, 2015, 10:50pm UTC](https://forum.ansible.com/t/conditional-evaluation-of-lists/17863/3 "2015-03-27T22:50:51Z")

</div>

Hi Brian,

> with\_items:
> 
> - p1
> - p2
> - “{{ condition|ternary(p3,‘’) }}”

This yields an empty list item, and the yum module doesn’t like it.

> or
> 
> with\_items: “{{ [‘p1’,‘p2’] |union(condition|ternary([‘p3’],)) }}”

Using ansible 1.9.0.1, this fails with:

fatal: [10.0.0.63] =\> an unexpected type error occurred. Error was can only concatenate list (not “bool”) to list
