# How to easily test/experiment with jinja templating in ansible?

**URL:** <https://forum.ansible.com/t/how-to-easily-test-experiment-with-jinja-templating-in-ansible/17108>\
**Category:** Ansible Project\
**Created:** [December 19, 2014, 3:50pm UTC](https://forum.ansible.com/t/how-to-easily-test-experiment-with-jinja-templating-in-ansible/17108 "2014-12-19T15:50:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dario\_Bertini](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@Dario\_Bertini](https://forum.ansible.com/u/Dario_Bertini)\
**Post date:** [December 19, 2014, 3:50pm UTC](https://forum.ansible.com/t/how-to-easily-test-experiment-with-jinja-templating-in-ansible/17108/1 "2014-12-19T15:50:09Z")

</div>

I’ve been bitten one too many times by the way that ansible interacts with jinja. I’d say its error handling is a little bit too lax, defaulting to return empty strings or similar things that then end up getting my playbooks to fail later down the line.

To prevent that, I know that I can have a better look at the available vars and facts gathered with the setup module

ansible -m setup

but often I’d like to see how I can use these with jinja: I’d like to have a REPL for it (or failing that, a shorter feedback loop)

debug tasks inside a playbook are not ideal also because I cannot use them to debug issues with templates used before tasks execution (e.g. to define a variable)

I was thinking of simply using

ansible -m debug -a “msg={{the\_jinja\_expression\_I\_want\_to\_test}}”

but I realized that the output is not quite what I’d expect:

ansible -m debug -a “msg={{hostvars[inventory\_hostname]}}”

for example returns

kalivm | success \>\> {  
“msg”: “{‘ansible\_ssh\_host’:”  
}

and

ansible -m debug -a “msg={{hostvars}}”

prints

kalivm | success \>\> {  
“msg”: “{‘kalivm’:”  
}

It seems to be a truncated json string, probably because not all the variables have yet been made available there in a simple ansible module invocation (they’d probably be available in a full playbook run)

Is this known behavior? How do you usually handle these things?

Thanks

---

<div class="post-metadata">

**Author:** ![Azul](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@Azul](https://forum.ansible.com/u/Azul)\
**Post date:** [December 22, 2014, 5:53pm UTC](https://forum.ansible.com/t/how-to-easily-test-experiment-with-jinja-templating-in-ansible/17108/2 "2014-12-22T17:53:01Z")

</div>

I usually attach a debugger,  
[http://michaeldehaan.net/post/35403909347/tips-on-using-debuggers-with-ansible](http://michaeldehaan.net/post/35403909347/tips-on-using-debuggers-with-ansible)

or set a small template action to dump the contents of the dictionary/variable whatever to a local file on my box.  
It’s easier to inspect it that way,

---

<div class="post-metadata">

**Author:** ![Marc\_Tamsky](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@Marc\_Tamsky](https://forum.ansible.com/u/Marc_Tamsky)\
**Post date:** [September 30, 2016, 8:07pm UTC](https://forum.ansible.com/t/how-to-easily-test-experiment-with-jinja-templating-in-ansible/17108/3 "2016-09-30T20:07:42Z")

</div>

I agree it’s not totally obvious.

The answer at [http://serverfault.com/a/695798](http://serverfault.com/a/695798) helped me figure out that it needed to be quoted.

quote your test expressions and you get the output you expected:

# ansible --version

ansible 1.9.6  
configured module search path = None

# ansible -m debug -a msg=“‘{{hostvars[inventory\_hostname]}}’”

localhost | success \>\> {  
“msg”: “{‘inventory\_hostname’: ‘127.0.0.1’, ‘group\_names’: [‘ungrouped’], ‘inventory\_hostname\_short’: ‘127’}”  
}

---

<div class="post-metadata">

**Author:** ![Azul](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@Azul](https://forum.ansible.com/u/Azul)\
**Post date:** [October 1, 2016, 12:03am UTC](https://forum.ansible.com/t/how-to-easily-test-experiment-with-jinja-templating-in-ansible/17108/4 "2016-10-01T00:03:25Z")

</div>

and 2 years later …  
: )
