[Ansible 2.4] include_tasks and import_tasks picking the first specified file in a ternary condition

269 views
Skip to first unread message

ishan jain

unread,
Feb 19, 2018, 10:52:03 AM2/19/18
to Ansible Project
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. 


[docker]
docker1 ansible_host=0.0.0.0


include_with_vars.yaml
---
- hosts: docker
  tasks:
    - include_tasks: "{{ condition | ternary ('docker1.yaml', 'docker2.yaml') }}"

import_with_vars.yaml
---
- hosts: docker
  tasks:
    - 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

[root@8a98ab1d7dea include]# ansible --version
ansible 2.4.3.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible-2.4.3.0-py2.7.egg/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
[root@8a98ab1d7dea include]# ansible-playbook -i hosts.info include_with_vars.yaml -e condition=false

PLAY [docker] ***************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************
ok: [docker1]

TASK [include_tasks] ********************************************************************************************************************************************************************************
included: /opt/include/docker1.yaml for docker1

TASK [debug] ****************************************************************************************************************************************************************************************
ok: [docker1] => {
    "msg": "Tasks 1 Host 1"
}

PLAY RECAP ******************************************************************************************************************************************************************************************
docker1                    : ok=3    changed=0    unreachable=0    failed=0

[root@8a98ab1d7dea include]# ansible-playbook -i hosts.info include_with_vars.yaml -e condition=true

PLAY [docker] ***************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************
ok: [docker1]

TASK [include_tasks] ********************************************************************************************************************************************************************************
included: /opt/include/docker1.yaml for docker1

TASK [debug] ****************************************************************************************************************************************************************************************
ok: [docker1] => {
    "msg": "Tasks 1 Host 1"
}

PLAY RECAP ******************************************************************************************************************************************************************************************
docker1                    : ok=3    changed=0    unreachable=0    failed=0

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

PLAY [docker] ***************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************
ok: [docker1]

TASK [debug] ****************************************************************************************************************************************************************************************
ok: [docker1] => {
    "msg": "Tasks 1 Host 1"
}

PLAY RECAP ******************************************************************************************************************************************************************************************
docker1                    : ok=2    changed=0    unreachable=0    failed=0

[root@8a98ab1d7dea include]# ansible-playbook -i hosts.info import_with_vars.yaml -e condition=true

PLAY [docker] ***************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************
ok: [docker1]

TASK [debug] ****************************************************************************************************************************************************************************************
ok: [docker1] => {
    "msg": "Tasks 1 Host 1"
}

PLAY RECAP ******************************************************************************************************************************************************************************************
docker1                    : ok=2    changed=0    unreachable=0    failed=0



Kai Stian Olstad

unread,
Feb 19, 2018, 11:26:47 AM2/19/18
to ansible...@googlegroups.com
On Monday, 19 February 2018 16.52.02 CET ishan jain wrote:
> *include_with_vars.yaml*
> ---
> - hosts: docker
> tasks:
> - include_tasks: "{{ condition | ternary ('docker1.yaml',
> 'docker2.yaml') }}"
>
> *import_with_vars.yaml*
> ---
> - hosts: docker
> tasks:
> - 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 ......


--
Kai Stian Olstad

Matt Martz

unread,
Feb 19, 2018, 11:28:22 AM2/19/18
to ansible...@googlegroups.com
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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8c53bf99-30ae-4cb4-96cc-1d6fdfd97d77%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

ishan jain

unread,
Feb 20, 2018, 4:37:51 AM2/20/18
to Ansible Project
Thanks a lot Kai and Matt,

I was looking for the problem in entirely different area.

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages