# \[Ansible 2.4\] include\_tasks and import\_tasks picking the first specified file in a ternary condition

**URL:** <https://forum.ansible.com/t/ansible-2-4-include-tasks-and-import-tasks-picking-the-first-specified-file-in-a-ternary-condition/26788>\
**Category:** Ansible Project\
**Created:** [February 19, 2018, 3:52pm UTC](https://forum.ansible.com/t/ansible-2-4-include-tasks-and-import-tasks-picking-the-first-specified-file-in-a-ternary-condition/26788 "2018-02-19T15:52:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [February 19, 2018, 3:52pm UTC](https://forum.ansible.com/t/ansible-2-4-include-tasks-and-import-tasks-picking-the-first-specified-file-in-a-ternary-condition/26788/1 "2018-02-19T15:52:02Z")

</div>

Hi,

I was trying out a few sample playbooks to understand the difference between static n dynamic imports, and i am stuck at a point where i am not sure what am i doing wrong.

**hosts.info**

`

[docker]  
docker1 ansible\_host=0.0.0.0

`

**include\_with\_vars.yaml**

`

---

<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:** [February 19, 2018, 4:26pm UTC](https://forum.ansible.com/t/ansible-2-4-include-tasks-and-import-tasks-picking-the-first-specified-file-in-a-ternary-condition/26788/2 "2018-02-19T16:26:25Z")

</div>

> \*include\_with\_vars.yaml\*  
> ---  
> - hosts: docker  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- include\_tasks: "{{ condition | ternary ('docker1.yaml',  
> 'docker2.yaml') }}"
> 
> \*import\_with\_vars.yaml\*  
> ---  
> - hosts: docker  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- import\_tasks: "{{ condition | ternary ('docker1.yaml',  
> 'docker2.yaml') }}"
> 
> (docker1.yaml prints "msg": "Tasks 1 Host 1" and docker2.yaml prints "msg":  
> "Tasks 2 Host 2")
> 
> No matter how i execute this, it always includes/import docker1.yaml

\<snip /\>

> [root@8a98ab1d7dea include]# ansible-playbook -i hosts.info  
> include\_with\_vars.yaml -e condition=false

Here condition is a string and not a bool so you need

{{ condition | bool | ternary ......

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 19, 2018, 4:27pm UTC](https://forum.ansible.com/t/ansible-2-4-include-tasks-and-import-tasks-picking-the-first-specified-file-in-a-ternary-condition/26788/3 "2018-02-19T16:27:30Z")

</div>

When supplying extra vars on the command line using key=value syntax, the value will always be a string. In python (and jinja2) any string with non-zero length is always truthy. So the strings “true” and “false” are both truthy.

You have a few options:

Use JSON:

-e ‘{“condition”: true}’

Use the |bool filter:

“{{ condition | bool | ternary (‘docker1.yaml’, ‘docker2.yaml’) }}”

I’d recommend including the |bool filter regardless, as that is going to ensure things are processed as you expect.

---

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [February 20, 2018, 9:37am UTC](https://forum.ansible.com/t/ansible-2-4-include-tasks-and-import-tasks-picking-the-first-specified-file-in-a-ternary-condition/26788/4 "2018-02-20T09:37:51Z")

</div>

Thanks a lot Kai and Matt,

I was looking for the problem in entirely different area.
