How to escape a variable?

5,087 views
Skip to first unread message

Vijay Korapaty

unread,
Aug 8, 2014, 3:06:04 PM8/8/14
to ansible...@googlegroups.com
I'm trying to execute the following task:

    - name: obtain image id
      local_action: shell docker inspect --format={{.Id}} {{ docker_image_name }} | cut  
      register: docker_image_id

where '{{.Id}}' should not be considered as a variable. I've tried escaping it in this fashion: {{ '{{' }} .Id {{ '}}' }} as described by the Jinja2 docs, but that results in it appearing as '{# .Id #}'. I'm assuming that it's still being considered as a variable.

Is there a way to force double curly brackets to not be considered as a variable?

Thanks.

Michael Peters

unread,
Aug 8, 2014, 5:35:55 PM8/8/14
to ansible...@googlegroups.com
Probably not the best way, but you could use a string inside of the
jinja templating

- name: obtain image id
local_action: shell docker inspect --format={{ '{{.Id}}' }} {{
docker_image_name }} | cut
register: docker_image_id


> --
> 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/53E50320.3090603%40korapaty.com.
> For more options, visit https://groups.google.com/d/optout.

Michael DeHaan

unread,
Aug 8, 2014, 7:32:46 PM8/8/14
to ansible...@googlegroups.com
Alternatively, you could also do:

\{\{.Id\}\}

Or

{% raw %}{{ .Id }}{% endraw %}

I probably prefer the raw/endraw most of the time.




Vijay Korapaty

unread,
Aug 8, 2014, 9:24:29 PM8/8/14
to ansible...@googlegroups.com
I'm not entirely sure what I changed, but {{ '{{' }} .Id {{ '}}' }} started working for me.

Thanks for the alternatives, I'll keep those in mind.

Conrado Buhrer

unread,
May 19, 2015, 3:34:45 PM5/19/15
to ansible...@googlegroups.com, ansib...@korapaty.com
I also bumped into this, it is a rare corner-case when one tries to use double curly braces in a command... Which I was also trying with docker inspect, there are two better solutions for getting the data rather than executing a command:

1 - use the docker_containers variable which is automagically created on executing the docker module


I prefer the second method because it'll let me find the right container more easily.

Since I'm a curious person, I'd like to know if there's a way to escape the double curly braces... because none of these methods worked for me on ansible 1.9.1
Reply all
Reply to author
Forward
0 new messages