How to escape double-quotes in ansible variable

I have an ansible playbook which accepts a variable, passing a variable with quotes in it(its needed), that variable will used to query against DB

Playbook

- name: Execute clear script
  script: scripts/clear-documents.sh {{ids}}

Command

ansible-playbook playbooks/maintenance.yml -i hosts -t clear -e ids=["foo", "bar"]

in this process script receives the input as [foo, bar] instead of ["foo", "bar"]

I tried escaping using backslash but that did not help

ansible-playbook playbooks/maintenance.yml -i hosts -t clear -e ids=[\"foo\", \"bar\"]

Adding double quotes in playbook, makes the input like "[foo,bar]" and not ["foo", "bar"]

script: scripts/clear-documents.sh "{{ids}}"

I searched a lot but did not get any proper solution, is there a way to handler this

Note: ansible version - 2.2.3.0

The thing you are looking for is quote, in combination with @JGK's correct usage of -e ids='["foo", "bar"]' because you were not quoting them on the way into ansible, and then you were not quoting them on the way out of ansible in that shell: task

- shell: scripts/clear-documents.sh {{ ids | quote }}

How to escape backslash and double quote in Ansible (script module), Don't be confused by the fact that ansible-playbook prints debug messages in JSON encoded form, so some characters are escaped. set_fact:� I've been trying to create a variable using set_fact to be equal to the following: '{"query": { "match_all": {} } }' Unfortunately, Ansible always parses through and changes the double quotes to single quotes. I attempted surrounding them with single quotes, which would cause for both single quotes and the one double quote to show.

As is mentioned in How to escape backslash and double quote in Ansible (script module) you need to surround the {{ ids }} with ' ' in your shell command.

#!/usr/bin/env ansible-playbook
- hosts: localhost
  gather_facts: false
  become: false
  tasks:
  - name: Escape characters for fun and profit
    vars:
      string_list: '["one", "two"]'
    shell: "echo '{{ string_list }}'"
    register: output1

  - name: Print it out
    debug:
      msg: "{{ output1 }}"

  - name: Don't escape characters
    vars:
      string_list: '["one", "two"]'
    shell: "echo {{ string_list }}"
    register: output2

  - name: Print it out
    debug:
      msg: "{{ output2 }}"


PLAY [localhost] ************************************************************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Escape characters for fun and profit] *********************************************************************************************************************************************************************************************************************************************************************************************************************************
changed: [localhost]

TASK [Print it out] *********************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "changed": true,
        "cmd": "echo '[\"one\", \"two\"]'",
        "delta": "0:00:00.003400",
        "end": "2019-05-07 12:02:32.897856",
        "failed": false,
        "rc": 0,
        "start": "2019-05-07 12:02:32.894456",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "[\"one\", \"two\"]",
        "stdout_lines": [
            "[\"one\", \"two\"]"
        ]
    }
}

TASK [Don't escape characters] **********************************************************************************************************************************************************************************************************************************************************************************************************************************************
changed: [localhost]

TASK [Print it out] *********************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "changed": true,
        "cmd": "echo [\"one\", \"two\"]",
        "delta": "0:00:00.002990",
        "end": "2019-05-07 12:02:33.192049",
        "failed": false,
        "rc": 0,
        "start": "2019-05-07 12:02:33.189059",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "[one, two]",
        "stdout_lines": [
            "[one, two]"
        ]
    }
}

PLAY RECAP ******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=4    changed=2    unreachable=0    failed=0  

What's the proper way to escape double quotes in ansible? : ansible, I've been trying to create a variable using set_fact to be equal to the following: Unfortunately, Ansible always parses through and changes the … The difference between single quotes and double quotes is that in double quotes you can use escapes: foo : "a \t TAB and a \n NEWLINE" The list of allowed escapes can be found in the YAML Specification under “Escape Sequences” (YAML 1.1) or “Escape Characters” (YAML 1.2).

I have playbook with kubectl command, when I want to run this command it cannot avoid quotes and understand this directory not exist

--- 
    - 
      hosts: localhost

      vars_files: 
        - vars/main.yaml 


      tasks:     
        -
         shell:
           cmd: |
               kubectl exec -it -n {{ namespace }} {{ pod_name }} -- bash -c \"clickhouse-client --query "INSERT INTO customer FORMAT CSV" --user=test --password=test < /mnt/azure/azure/test/test.tbl\"
         register: output2

Here is the error:

fatal: [127.0.0.1]: FAILED! => {
    "changed": true,
    "cmd": "kubectl exec -it -n ch-test04 chi-test-dashboard-sharded1-dashboard03-3-0-0 -- bash -c \\\"clickhouse-client --query \"INSERT INTO customer FORMAT CSV\" --user=test --password=test < mnt/azure/azure/test/test.tbl\\\"\n",
    "delta": "0:00:00.002088",
    "end": "2020-04-23 13:30:00.456263",
    "invocation": {
        "module_args": {
            "_raw_params": "kubectl exec -it -n ch-test04 chi-test-dashboard-sharded1-dashboard03-3-0-0 -- bash -c \\\"clickhouse-client --query \"INSERT INTO customer FORMAT CSV\" --user=test --password=test < mnt/azure/azure/test/test.tbl\\\"\n",
            "_uses_shell": true,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true,
            "warn": true
        }
    },
    "msg": "non-zero return code",
    "rc": 2,
    "start": "2020-04-23 13:30:00.454175",
    "stderr": "/bin/sh: 1: cannot open mnt/azure/azure/test/test.tbl\": No such file",
    "stderr_lines": [
        "/bin/sh: 1: cannot open mnt/azure/azure/test/test.tbl\": No such file"
    ],
    "stdout": "",
    "stdout_lines": []
}

missing double quotes in variable � Issue #49282 � ansible/ansible , SUMMARY Ansible automatically escapes double quotes in variable. in both sides of word coreApi_visitortracking would not be escaped. Escape double quotes within double quotes with Regex[JS] " Also during my research I found a nice ansible galaxy code which allows you to dump all variables

YAML Syntax — Ansible Documentation, The difference between single quotes and double quotes is that in double quotes you can Specification under “Escape Sequences” (YAML 1.1) or “Escape Characters” (YAML 1.2). foo: "{{ variable }}/additional/string/literal" foo2: "{{ variable }}� How can you properly escape backslash in ansible playbooks? I am trying to do a search and replace for the backslash in the password, but I am not even able to add backslash as a string in the regex_replace jinja2 filter.

Quote and escape extra vars passed to ansible � 196d28e766 , Quote and escape extra vars passed to ansible. Arguments passed as extra variables to Ansible require double escaping. This can be seen when running the � Registered variables are similar to facts, with a few key differences. Like facts, registered variables are host-level variables. However, registered variables are only stored in memory. (Ansible facts are backed by whatever cache plugin you have configured.) Registered variables are only valid on the host for the rest of the current playbook run.

Escaping all Special Characters Using Ansible Shell, If you have to use quote or any special character or need to 'double escape' your command to run Ansible 'shell' module, make sure it's� Site.path is indeed a variable from a list of dictionaries, but this works well and is correct. The problem is that I need the result of the value to have double quotes . In the example you posted, same as mine, site.path will not be seen as a variable anymore because of the quotes. I also tried with 2 double quotes, with /, but without success.

Comments
  • Quoting hell ;-) -e ids=\"\[\\\"foo\",\"bar\\\"]\" or more readable -e ids='["foo","bar"]'
  • .. get a little closer to paradise (but just a little) -e 'ids="[\"foo\", \"bar\"]"'
  • Possible duplicate of How to escape backslash and double quote in Ansible (script module)