ishan_jain
(ishan jain)
February 19, 2018, 3:52pm
1
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
`
*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 ......
sivel
(sivel)
February 19, 2018, 4:27pm
3
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.
ishan_jain
(ishan jain)
February 20, 2018, 9:37am
4
Thanks a lot Kai and Matt,
I was looking for the problem in entirely different area.