Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why ansible always replaces double quotes with single quotes in templates?

I am trying to generate Dockerfiles with Ansible template - see the role source and the template in Ansible Galaxy and Github

I need to genarate a standard Dockerfile line like:

...
VOLUME ["/etc/postgresql/9.4"]
...

However, when I put this in the input file:

...
instruction: CMD
value: "[\"/etc/postgresql/{{postgresql_version}}\"]"
...

It ends up rendered like:

...
VOLUME ['/etc/postgresql/9.4']
...

and I lose the " (which renders the Dockerfiles useless)

Any help ? How can I convince Jinja to not substitute " with ' ? I tried \" , |safe filter, even {% raw %} - it just keeps doing it!

Update: Here is how to reproduce the issue:

Go get the peruncs.docker role from galaxy.ansible.com or Github (link is given above) Write up a simple playbook (say demo.yml) with the below content and run: ansible-playbook -v demo.yml. The -v option will allow you to see the temp directory where the generated Dockerfile goes with the broken content, so you can examine it. Generating the docker image is not important to succeed, just try to get the Dockerfile right.

- name: Build docker image
  hosts: localhost
  vars:
        - somevar: whatever        
        - image_tag: "blabla/booboo"
        - docker_copy_files: []
        - docker_file_content:
             - instruction: CMD
               value: '["/usr/bin/runit", "{{somevar}}"]'
  roles:         
      - peruncs.docker

Thanks in advance!

like image 786
Hristo Stoyanov Avatar asked Aug 12 '15 15:08

Hristo Stoyanov


People also ask

What is the difference between single quotes and double quotes in shell script?

There is no such difference between the single quote (') and double quote(“) in PowerShell. It is similar to a programming language like Python.

How do you escape single quotes in Ansible?

In single quoted strings the single quote character can be escaped by prefixing it with another single quote, basically doubling it. Backslashes in single quoted strings do not need to be escaped.

Are single and double quotes interchangeable?

The short answer is that it depends on the country that you are writing in. In British and Australian English, one typically uses single quotes. If you're writing in North America, double quote marks are typically used.

What is the difference between single quote and double quote in C?

In C and C++ the single quote is used to identify the single character, and double quotes are used for string literals. A string literal “x” is a string, it is containing character 'x' and a null terminator '\0'. So “x” is two-character array in this case. In C++ the size of the character literal is char.


1 Answers

Something in Ansible appears to be recognizing that as valid Python, so it's getting transformed into a Python list and then serialized using Python's str(), which is why you end up with the single-quoted values.

An easy way to work around this is to stick a space at the beginning of the value, which seems to prevent it from getting converted into Python:

- name: Build docker image
  hosts: localhost
  vars:
        - somevar: whatever        
        - image_tag: "blabla/booboo"
        - docker_copy_files: []
        - docker_file_content:
             - instruction: CMD
               value: ' ["/usr/bin/runit", "{{somevar}}"]'
  roles:         
      - peruncs.docker

This results in:

CMD  ["/usr/bin/runit", "whatever"] 
like image 161
larsks Avatar answered Oct 08 '22 17:10

larsks