# How to make an item in with\_items conditional?

**URL:** <https://forum.ansible.com/t/how-to-make-an-item-in-with-items-conditional/19992>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [December 11, 2015, 5:11pm UTC](https://forum.ansible.com/t/how-to-make-an-item-in-with-items-conditional/19992 "2015-12-11T17:11:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Abramowitz](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marc\_Abramowitz](https://forum.ansible.com/u/Marc_Abramowitz)\
**Post date:** [December 11, 2015, 5:11pm UTC](https://forum.ansible.com/t/how-to-make-an-item-in-with-items-conditional/19992/1 "2015-12-11T17:11:16Z")

</div>

Take this playbook:

- hosts: mt1-dummys101  
tasks:
- name: Install apt packages  
sudo: true  
apt: pkg={{ item }}  
with\_items:
- build-essential
- libyaml-dev
- python2.7-dev
- “{{ ‘silversearcher-ag’ if ansible\_distribution\_major\_version == ‘14’ else ‘’ }}”

How would I make it so that silversearcher-ag is installed only on Ubuntu 14 and is omitted for other operating systems?

The above gives:

TASK: [Install apt packages] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [mt1-dummys101] =\> (item=build-essential,libyaml-dev,python2.7-dev,) =\> {“failed”: true, “item”: “build-essential,libyaml-dev,python2.7-dev,”}  
msg: No package matching ‘’ is available

I also tried changing the empty string `''` to `omit` but that also fails:

TASK: [Install apt packages] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [mt1-dummys101] =\> (item=build-essential,libyaml-dev,python2.7-dev,\_\_omit\_place\_holder\_\_07ef1633a8f8d1dc718ffb780b3f0e54d7f7b02a) =\> {“failed”: true, “item”: “build-essential,libyaml-dev,python2.7-dev,\_\_omit\_place\_holder\_\_07ef1633a8f8d1dc718ffb780b3f0e54d7f7b02a”}  
msg: No package matching ‘\_\_omit\_place\_holder\_\_07ef1633a8f8d1dc718ffb780b3f0e54d7f7b02a’ is available

Any way to do this?

---

<div class="post-metadata">

**Author:** ![Dylan\_Martin](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@Dylan\_Martin](https://forum.ansible.com/u/Dylan_Martin)\
**Post date:** [December 11, 2015, 5:15pm UTC](https://forum.ansible.com/t/how-to-make-an-item-in-with-items-conditional/19992/2 "2015-12-11T17:15:40Z")

</div>

I’ve done it by having two lists in group vars files and doing a group\_by

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [December 11, 2015, 5:17pm UTC](https://forum.ansible.com/t/how-to-make-an-item-in-with-items-conditional/19992/3 "2015-12-11T17:17:13Z")

</div>

My first thought is to create a new task and use a when clause:

`

- hosts: mt1-dummys101  
tasks:

- name: Install apt packages  
sudo: true  
apt: pkg={{ item }}  
with\_items:

- build-essential

- libyaml-dev

- python2.7-dev

- name: Install apt packages for version 14

sudo: true  
apt: pkg={{ item }}  
with\_items: - silversearcher-ag  
when: ansible\_distribution\_major\_version == ‘14’

`

---

<div class="post-metadata">

**Author:** ![Marc\_Abramowitz](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marc\_Abramowitz](https://forum.ansible.com/u/Marc_Abramowitz)\
**Post date:** [December 11, 2015, 5:51pm UTC](https://forum.ansible.com/t/how-to-make-an-item-in-with-items-conditional/19992/4 "2015-12-11T17:51:17Z")

</div>

Yeah, this was what I went with for the time being. I’m curious if there are other ways, but this does seem to be a reasonable solution. Thanks!

---

<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:** [December 11, 2015, 8:13pm UTC](https://forum.ansible.com/t/how-to-make-an-item-in-with-items-conditional/19992/5 "2015-12-11T20:13:21Z")

</div>

with\_items:  
&nbsp;&nbsp;&nbsp;- "{{ (ansible\_distribution\_major\_version == '14')|ternary(  
'silversearcher-ag','' }}"

---

<div class="post-metadata">

**Author:** ![Marc\_Abramowitz](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marc\_Abramowitz](https://forum.ansible.com/u/Marc_Abramowitz)\
**Post date:** [December 11, 2015, 8:53pm UTC](https://forum.ansible.com/t/how-to-make-an-item-in-with-items-conditional/19992/6 "2015-12-11T20:53:22Z")

</div>

Thanks, but that doesn’t work.

E.g.: I used this slightly tweaked line:

- “{{ (ansible\_distribution\_major\_version == ‘12’)|ternary(‘silversearcher-ag’, ‘’) }}”

and if the condition is not true, then I get:

TASK: [Install apt packages] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [mt1-dummys101] =\> (item=build-essential,libyaml-dev,python2.7-dev,) =\> {“failed”: true, “item”: “build-essential,libyaml-dev,python2.7-dev,”}  
msg: No package matching ‘’ is available

It seems that for this kind of thing to work, there would either need to be:

- a way to tell with\_items to not add the item at all (by using “omit” perhaps, which doesn’t currently work in this context)
- “apt” module would ignore a blank string or other sentinel value like “omit”.

---

<div class="post-metadata">

**Author:** ![Marc\_Abramowitz](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marc\_Abramowitz](https://forum.ansible.com/u/Marc_Abramowitz)\
**Post date:** [December 11, 2015, 8:55pm UTC](https://forum.ansible.com/t/how-to-make-an-item-in-with-items-conditional/19992/7 "2015-12-11T20:55:51Z")

</div>

And I would think the former would be more preferable as it is more generally applicable (would work with all modules).

I think of “with\_items” as being analogous to a for loop in most programming languages. So I guess that means the feature that I am wishing I had here is analogous to a “continue” statement in those same programming languages.

---

<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:** [December 11, 2015, 9:04pm UTC](https://forum.ansible.com/t/how-to-make-an-item-in-with-items-conditional/19992/8 "2015-12-11T21:04:48Z")

</div>

ah, thought you wanted an empty string, misread from your example.

you'll have to use a when condition or dynamically construct the list,  
there is no way to have a list item return a 'delete this item', even  
None would return an empty element, but still and element.

list:  
- 1  
- 2  
&nbsp;&nbsp;- \<whatever\>

is already a list of 3 items, no value of \<whatever\> can remove the -  
in front of it as it is processed by YAML and \<whatever\> is by jinja2,  
when its already a 3 element list.

example of dynamic list  
with\_items: {{"[1,2]|union( condition|ternary(['stuff'], ))"}}

it uses union to add elements from either list with 'stuff' or empty  
list, which leaves the original 'unchanged'

---

<div class="post-metadata">

**Author:** ![Marc\_Abramowitz](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marc\_Abramowitz](https://forum.ansible.com/u/Marc_Abramowitz)\
**Post date:** [December 11, 2015, 9:34pm UTC](https://forum.ansible.com/t/how-to-make-an-item-in-with-items-conditional/19992/9 "2015-12-11T21:34:29Z")

</div>

Ah, that’s clever. Probably too clever for me to want to use on my colleagues, but clever nonetheless.

That could be an example if anyone ever writes a book called the “The Little Ansibler” in the style of “The Little Schemer”. 🙂
