Template problem with bash array length syntax

3,226 views
Skip to first unread message

Dmitry Horbach

unread,
Aug 14, 2013, 11:41:51 AM8/14/13
to ansible...@googlegroups.com
Hi,

Having simple template file with bash valid content like:

${#ArrayName[@]}

will break template module:

- template: src=template dest=template2

TASK: [template src=template dest=template2] ********************************** 
fatal: [127.0.0.1] => {'msg': 'Missing end of comment tag', 'failed': True}
fatal: [127.0.0.1] => {'msg': 'Missing end of comment tag', 'failed': True}

Is there any workaround for this?

Brian Coca

unread,
Aug 14, 2013, 11:48:22 AM8/14/13
to ansible...@googlegroups.com
{#  #} is a jinja comment tag which is why it is gettitng confused here
try:
{{ '${#ArrayName[@]}' }} 


--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

Dmitry Horbach

unread,
Aug 14, 2013, 11:57:17 AM8/14/13
to ansible...@googlegroups.com
Works great! Thanks a lot Brian.

r...@backblaze.com

unread,
Aug 14, 2013, 2:48:45 PM8/14/13
to ansible...@googlegroups.com
You can also wrap an entire block text/code in {% raw %}  {% endraw %}:

{% raw %}
for (( 1=0; i<$#allowed_list[@]}; i++ )); do
    allowed=${allowed_list[$i]
    # do something here with $allowed
done
{% endraw %}

This allows you to avoid escaping all the dollar signs, as well...

-Ric

einar....@gmail.com

unread,
May 25, 2016, 10:22:04 AM5/25/16
to Ansible Project
It gets even more interesting if you get something like this:
define('AUTH_KEY',         'TccKCkdkq.}]I]I?$(.R@L)ASUysg% AB(a5j+`A8_ra6Vu7D*h2Q]||}.{#b}5Xq');

Because the ; makes the engine throw this error:
expected token 'end of print statement', got ';'

Dick Davies

unread,
Jun 10, 2016, 10:39:25 AM6/10/16
to ansible list
Hit something similar when pushing go templates out with the ansible
template command.

Use

{%raw%}
..... line noise goes here ....
{%endraw%}
> 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/0c9420f7-74d3-4aaf-a037-38b012001df3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages