Skip to content

Instantly share code, notes, and snippets.

@andymcc
Last active May 30, 2017 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andymcc/2ddf98b50fa07e3f8b8648a440499fb8 to your computer and use it in GitHub Desktop.
Save andymcc/2ddf98b50fa07e3f8b8648a440499fb8 to your computer and use it in GitHub Desktop.
Test Jinja2 with set and include statements
root@jinja2fix:/opt/test_jinja# cat templates/setincl.j2
{% set myvar = "foo" %}
{{ myvar }}
{% include "include.j2" %}
root@jinja2fix:/opt/test_jinja# cat templates/include.j2
bar
root@jinja2fix:/opt/test_jinja# pip list | grep Jinja2
Jinja2 (2.9.4)
root@jinja2fix:/opt/test_jinja# ansible -vvvvvvv localhost -m template -a "src=setincl.j2 dest=/tmp/test.file"
Loading callback plugin minimal of type stdout, v2.0 from /opt/ansible-runtime/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc
looking for "setincl.j2" at "/opt/test_jinja/templates/setincl.j2"
localhost | FAILED! => {
"changed": false,
"failed": true,
"invocation": {
"module_args": {
"dest": "/tmp/test.file",
"src": "setincl.j2"
},
"module_name": "template"
},
"msg": "KeyError: 'undefined variable: 0'"
}
root@jinja2fix:/opt/test_jinja# cat templates/only_include.j2
{% include "include.j2" %}
root@jinja2fix:/opt/test_jinja# cat templates/only_set.j2
{% set myvar = "foo" %}
{{ myvar }}
root@jinja2fix:/opt/test_jinja# ansible localhost -m template -a "src=only_include.j2 dest=/tmp/test.file"
localhost | SUCCESS => {
"changed": true,
"checksum": "01e178c1c193fbdce41a91f0237a25570c00d565",
"dest": "/tmp/test.file",
"gid": 0,
"group": "root",
"md5sum": "33e1a5c113940af09d7c49f332f4aa64",
"mode": "0644",
"owner": "root",
"size": 8,
"src": "/root/.ansible/tmp/ansible-tmp-1484218998.83-128081920672250/source",
"state": "file",
"uid": 0
}
root@jinja2fix:/opt/test_jinja# ansible localhost -m template -a "src=only_set.j2 dest=/tmp/test.file"
localhost | SUCCESS => {
"changed": true,
"checksum": "f1d2d2f924e986ac86fdf7b36c94bcdf32beec15",
"dest": "/tmp/test.file",
"gid": 0,
"group": "root",
"md5sum": "d3b07384d113edec49eaa6238ad5ff00",
"mode": "0644",
"owner": "root",
"size": 4,
"src": "/root/.ansible/tmp/ansible-tmp-1484218970.14-224905931386348/source",
"state": "file",
"uid": 0
}
root@jinja2fix:/opt/test_jinja# pip install Jinja2==2.8
Collecting Jinja2==2.8
Using cached Jinja2-2.8-py2.py3-none-any.whl
Requirement already satisfied: MarkupSafe in /opt/ansible-runtime/lib/python2.7/site-packages (from Jinja2==2.8)
Installing collected packages: Jinja2
Found existing installation: Jinja2 2.9.4
Uninstalling Jinja2-2.9.4:
Successfully uninstalled Jinja2-2.9.4
Successfully installed Jinja2-2.8
root@jinja2fix:/opt/test_jinja# ansible localhost -m template -a "src=setincl.j2 dest=/tmp/test.file"
localhost | SUCCESS => {
"changed": true,
"checksum": "4e48e2c9a3d2ca8a708cb0cc545700544efb5021",
"dest": "/tmp/test.file",
"gid": 0,
"group": "root",
"md5sum": "f47c75614087a8dd938ba4acff252494",
"mode": "0644",
"owner": "root",
"size": 8,
"src": "/root/.ansible/tmp/ansible-tmp-1484217092.13-31545377976138/source",
"state": "file",
"uid": 0
}
root@jinja2fix:/opt/test_jinja# cat /tmp/test.file
foo
bar
@danielmcquillen
Copy link

@andymcc thanks for posting. Any known workarounds?

@adul3
Copy link

adul3 commented May 30, 2017

Got the same issue in a template with 4 {% set ... and {% include ...
Very anoying situation :(
Anyone have solution ?
Regards ;)

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