Boolean variable being capitalized in templates? Best practice to work around?

10,181 views
Skip to first unread message

Philip Lombardi

unread,
Feb 10, 2015, 5:24:58 PM2/10/15
to ansible...@googlegroups.com
I have some variables defined in a role such as:

---
# file: roles/consul/defaults/main.yml
consul_server
: yes

And then I have a JSON template file with many settings for the software I am provisioning, but just for example:

{
 
"server": {{ consul_server | bool }}
}


When Ansible/Jinja2 process the template I end up with an invalid JSON object because 'True' is not a valid JSON keyword:

{
 
"server": True
}

I ended up doing this:

{
 
"server": {{ consul_server | bool | lower }}
}

Which works... but it gets sloppy when there are a ton of variables in a file.

So my question, what is the proper way to interpolate boolean values in a JSON file?

Thank you for reading. Looking forward to the response(s).

Regards,
Phil

Tom Bamford

unread,
Feb 11, 2015, 2:48:40 AM2/11/15
to ansible...@googlegroups.com
Hi Phil

Don't know if there is an official short way to do this, but there is a 'to_json' filter which seems to work on simple booleans, which could be slightly preferable to 'lower'?

Tom

--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b7b97826-dab2-40c9-8c37-75af8fef6a83%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Brian Coca

unread,
Feb 11, 2015, 8:38:13 AM2/11/15
to ansible...@googlegroups.com
the capitalized boolean happens because this is how valid booleans
work in jinja2/python, which is what ansible uses internally.

as @tom suggested there is a to_json filter which will output correct
json from ansible data structures/variables.



--
Brian Coca

Andrew Feller

unread,
Aug 11, 2016, 9:15:22 AM8/11/16
to Ansible Project
Maybe to_json is better than to_yaml and apparently have been superseded by to_nice_json / to_nice_yaml but the to_yaml / to_nice_yaml filters are crap, not well documented within jinja2, and require understanding the actual pyyaml library function interface to make work.  In short, all of it feels bad.
Reply all
Reply to author
Forward
0 new messages