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.

On Mon, Feb 19, 2018 at 9:52 AM, ishan jain <ishanja...@gmail.com> wrote:

> 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 <http://hosts.info>*
> [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
>
>
>
> --
> 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+unsubscr...@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
> <https://groups.google.com/d/msgid/ansible-project/8c53bf99-30ae-4cb4-96cc-1d6fdfd97d77%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Matt Martz
@sivel
sivel.net

-- 
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+unsubscr...@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/CAD8N0v-HrOn9EyYNF-ufOuZKT40pOpRQKUv7chxu4H1%3DpikKQA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to