Skip to content

Instantly share code, notes, and snippets.

@dlethin
Last active October 6, 2018 00:41
Show Gist options
  • Save dlethin/4328c9f58e8b4f5fc180f487907dfff0 to your computer and use it in GitHub Desktop.
Save dlethin/4328c9f58e8b4f5fc180f487907dfff0 to your computer and use it in GitHub Desktop.
Weird behavior with ansible jinja2 evaluating a variable it shouldn't need and failing
; start
{% if False %}
; not rendered
{{ outer }}
{% endif %}
; end
- name: "test me"
hosts: localhost
become: no
gather_facts: no
vars:
outer: "{{ inner }}"
test_case: "working"
tasks:
- name: "duh"
template:
src: "{{playbook_dir}}/{{test_case}}.txt.j2"
dest: "{{playbook_dir}}/{{test_case}}.txt"
; start
{% if False %}
; not rendered
{{ inner }}
{% endif %}
; end
@dlethin
Copy link
Author

dlethin commented Oct 6, 2018

Why does test_case working work and but failing does not. In the working test case the template has a reference to inner which does not exist but it does not fail. However in the failing test case, outer is defined, but it has a reference to inner.

Weird.

$ ansible-playbook -c local weird.yml
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [test me] ****************************************************************************************************************************************

TASK [duh] ********************************************************************************************************************************************
ok: [localhost]

PLAY RECAP ********************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0

$ cat working.txt
; start
; end
$ ansible-playbook -c local weird.ymtest_case=failing
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [test me] ****************************************************************************************************************************************

TASK [duh] ********************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'inner' is undefined"}
	to retry, use: --limit @/Users/douglethin/Downloads/4328c9f58e8b4f5fc180f487907dfff0-d34fc3db5daef4c3822c6fedc46e64659afd90ea/weird.retry

PLAY RECAP ********************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment