# Conditionally running a filter in a chain

**URL:** <https://forum.ansible.com/t/conditionally-running-a-filter-in-a-chain/25322>\
**Category:** Ansible Project\
**Created:** [August 24, 2017, 2:36pm UTC](https://forum.ansible.com/t/conditionally-running-a-filter-in-a-chain/25322 "2017-08-24T14:36:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean-Francois\_Bibeau](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Jean-Francois\_Bibeau](https://forum.ansible.com/u/Jean-Francois_Bibeau)\
**Post date:** [August 24, 2017, 2:36pm UTC](https://forum.ansible.com/t/conditionally-running-a-filter-in-a-chain/25322/1 "2017-08-24T14:36:41Z")

</div>

Hi,

Trying to do the following, and struggling to find a way to do this.

I build a list of strings, and want to conditionally append a string at the end of the list, but only if the variable is defined.

This works fine:

`  
play\_hosts | union([optional\_ip])

`

Which appends optional\_ip to play\_hosts, however if optional\_ip is not defined, I would like to simply drop the union filter… If I change it to:

`play_hosts | union([optional_ip | default(None)])`

It appends an empty string. I was hoping I could maybe use “or omit” after the union but can’t quite get that to work.

Any suggestions?

Thanks!

---

<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:** [August 25, 2017, 8:23pm UTC](https://forum.ansible.com/t/conditionally-running-a-filter-in-a-chain/25322/2 "2017-08-25T20:23:01Z")

</div>

You could try to add a when to the task  
&nbsp;&nbsp;when: optional\_ip is defined

---

<div class="post-metadata">

**Author:** ![Daniel\_JD](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Daniel\_JD](https://forum.ansible.com/u/Daniel_JD)\
**Post date:** [August 26, 2017, 10:31am UTC](https://forum.ansible.com/t/conditionally-running-a-filter-in-a-chain/25322/3 "2017-08-26T10:31:37Z")

</div>

Isn’t play\_hosts a reserved ansible variable (deprecatd but still…). Maybe try another name.

---

<div class="post-metadata">

**Author:** ![Jean-Francois\_Bibeau](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Jean-Francois\_Bibeau](https://forum.ansible.com/u/Jean-Francois_Bibeau)\
**Post date:** [August 26, 2017, 1:09pm UTC](https://forum.ansible.com/t/conditionally-running-a-filter-in-a-chain/25322/4 "2017-08-26T13:09:12Z")

</div>

Thanks Kai, that's one option for sure.

---

<div class="post-metadata">

**Author:** ![Jean-Francois\_Bibeau](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Jean-Francois\_Bibeau](https://forum.ansible.com/u/Jean-Francois_Bibeau)\
**Post date:** [August 26, 2017, 1:10pm UTC](https://forum.ansible.com/t/conditionally-running-a-filter-in-a-chain/25322/5 "2017-08-26T13:10:01Z")

</div>

@Daniel - I'm using the Ansible play\_hosts variable and optionally joining another IP to that existing list.

---

<div class="post-metadata">

**Author:** ![Jean-Francois\_Bibeau](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Jean-Francois\_Bibeau](https://forum.ansible.com/u/Jean-Francois_Bibeau)\
**Post date:** [August 28, 2017, 1:49pm UTC](https://forum.ansible.com/t/conditionally-running-a-filter-in-a-chain/25322/6 "2017-08-28T13:49:29Z")

</div>

I like Kai’s suggestion and will likely end up breaking the tasks into multiple chunks using ‘when’. However for completeness’ sake, I also got the following to work:

`play_hosts | union(optional_ip is defined | ternary([optional_ip], []))`

(ie: union the variable if it’s defined, otherwise union empty list).
