Ansible J2 unicode

18 views
Skip to first unread message

Tom Davison

unread,
Nov 5, 2019, 3:11:42 PM11/5/19
to Ansible Development
Hi everyone.

When trying to template out a weblogic wl.properties file, I am running into a Jinja2 template problem.

Here is a snippet of code I am running into issues with:

################################################################################
# ADDITIONAL MACHINE PROPERTIES
################################################################################
ADDITIONAL_MACHINES={% for server in my_servers %}{{ server }}{% if not loop.last %},{% endif %}{% endfor %}

{% for more_servers in my_servers %}
{{ [more_servers] }}LISTEN_ADDRESS={{ ansible_nodename }}
{% endfor %}

BAD OUTPUT:
################################################################################
# ADDITIONAL MACHINE PROPERTIES
################################################################################
ADDITIONAL_MACHINES=myserver02,myserver03,myserver04,myserver05,myserver06,myserver07,myserver08
[u'myserver02']LISTEN_ADDRESS=ansible_host
[u'myserver03']LISTEN_ADDRESS=ansible_host
[u'myserver04']LISTEN_ADDRESS=ansible_host
[u'myserver05']LISTEN_ADDRESS=ansible_host
[u'myserver06']LISTEN_ADDRESS=ansible_host
[u'myserver07']LISTEN_ADDRESS=ansible_host
[u'myserver08']LISTEN_ADDRESS=ansible_host


It appears that the square brackets around the j2 variable more_servers cause rendering issues and makes python inject the unicode "u" as well as single quotes into the final output. 

EXPECTED OUTPUT:
################################################################################
# ADDITIONAL MACHINE PROPERTIES
################################################################################
ADDITIONAL_MACHINES=myserver02,myserver03,myserver04,myserver05,myserver06,myserver07,myserver08
[myserver02]LISTEN_ADDRESS=ansible_host
[myserver03]LISTEN_ADDRESS=ansible_host
[myserver04]LISTEN_ADDRESS=ansible_host
['myserver05]LISTEN_ADDRESS=ansible_host
[myserver06]LISTEN_ADDRESS=ansible_host
[myserver07]LISTEN_ADDRESS=ansible_host
[myserver08]LISTEN_ADDRESS=ansible_host


I have tried many different filters to get this to work, but just cannot get it. Filters include: to_json, to_nice_json, to_yaml, to_nice_yaml,join, replace.

Any help would be greatly appreciated! 

 - Tom

Matt Martz

unread,
Nov 5, 2019, 3:22:05 PM11/5/19
to Tom Davison, Ansible Development
By doing this:

{{ [more_servers] }}

You are creating a python list, and then printing it.

I think you instead want:

[{{ more_servers }}]

--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-devel/c0eb5d63-a2a4-4276-90f6-93db66c74cdd%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

Tom Davison

unread,
Nov 5, 2019, 3:25:13 PM11/5/19
to Ansible Development
Matt,

you sir are a life saver! That is exactly what I am looking for.

Thanks much!!
To unsubscribe from this group and stop receiving emails from it, send an email to ansibl...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages