# Proper escaping quotes and braces

**URL:** <https://forum.ansible.com/t/proper-escaping-quotes-and-braces/13811>\
**Category:** Ansible Project\
**Created:** [October 31, 2013, 7:35am UTC](https://forum.ansible.com/t/proper-escaping-quotes-and-braces/13811 "2013-10-31T07:35:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anton\_Parkhomenko](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@Anton\_Parkhomenko](https://forum.ansible.com/u/Anton_Parkhomenko)\
**Post date:** [October 31, 2013, 7:35am UTC](https://forum.ansible.com/t/proper-escaping-quotes-and-braces/13811/1 "2013-10-31T07:35:44Z")

</div>

Hi,

I can’t write a syntactically correct playbook. How it would be right to write following command:

- name: test escaping

command: mycommand subcommand --option=‘{“first-item”: 2}’

Can you give me advice?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [October 31, 2013, 2:48pm UTC](https://forum.ansible.com/t/proper-escaping-quotes-and-braces/13811/2 "2013-10-31T14:48:02Z")

</div>

First, ansible version and error output would be helpful, but taking a  
guess try this:

- name: test escaping  
&nbsp;&nbsp;command: "mycommand subcommand --option={'first-item': 2}' "

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 1, 2013, 12:08am UTC](https://forum.ansible.com/t/proper-escaping-quotes-and-braces/13811/3 "2013-11-01T00:08:10Z")

</div>

Yep.

When you encounter problems it is always suggested to paste whatever error you had.

I’m guessing this is just the common “if you start a line or end a line with a quote, the whole thing must be quoted” which is one of the two gotchas on [http://ansibleworks.com/docs/YAMLSyntax.html](http://ansibleworks.com/docs/YAMLSyntax.html)

Ansible in version 1.4 or later will share some syntax tips, but in either case the exception should indicate you had a YAML syntax error.

---

<div class="post-metadata">

**Author:** ![Anton\_Parkhomenko](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@Anton\_Parkhomenko](https://forum.ansible.com/u/Anton_Parkhomenko)\
**Post date:** [November 2, 2013, 2:38pm UTC](https://forum.ansible.com/t/proper-escaping-quotes-and-braces/13811/4 "2013-11-02T14:38:19Z")

</div>

I’m sorry for not including debug output, I saw this error so many times that already thought that it’s pretty common. Ansible version is 1.3.3.

The error was:  
ERROR: Syntax Error while loading YAML script, playbook.yml  
Note: The error may actually appear before this position: line 53, column 77

- name: test escaping  
command: mycommand subcommand --option="{‘‘first-item’’: 2}"  
^

This actually shows that I have problems not with quotes and braces, but with colon. It’s one of number of non-working examples. It was solved by embracing colon with single quotes.  
And working solution is:

- name: test escaping  
command: mycommand subcommand --option="{first-item’:'2}"

I removed space after colon, and removed single quotes, but I want solution that includes them, so I’m consider question is still open.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 2, 2013, 3:05pm UTC](https://forum.ansible.com/t/proper-escaping-quotes-and-braces/13811/5 "2013-11-02T15:05:31Z")

</div>

This is the "you have a free colon in your output, you must quote the whole line, thing.

So basically what Brian suggested above will take care of it.

---

<div class="post-metadata">

**Author:** ![Anton\_Parkhomenko](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@Anton\_Parkhomenko](https://forum.ansible.com/u/Anton_Parkhomenko)\
**Post date:** [November 2, 2013, 3:27pm UTC](https://forum.ansible.com/t/proper-escaping-quotes-and-braces/13811/6 "2013-11-02T15:27:43Z")

</div>

Well, in that case I have syntactically correct YAML, but because Brian omitted outer single-quotes, it performs a wrong command. Verbose output:

changed: [192.168.1.107] =\>  
{“changed”: true,  
“cmd”: [“mycommand”, “subcommand”, “–option={first-item:”, **“2}”**],  
“delta”: “0:00:00.162754”,  
“end”: “2013-11-02 15:08:57.654332”,  
“rc”: 0, “start”: “2013-11-02 15:08:57.491578”,  
“stderr”: “upload: invalid arguments”,  
“stdout”: “mycommand subcommand[OPTIONS]\n --help display help”}

If I insert the outer quotes, the following error will appear:

ERROR: Syntax Error while loading YAML script, playbook.yml  
Note: The error may actually appear before this position: line 53, column 73

- name: test escaping  
command: mycommand subcommand --option=“{‘first-item’: 2}”"

^

Anyway, if I’ve missing something, this “something” is definetely about YAML syntax and not about Ansible and I should ask it somewhere else. Especially that my problem is already solved in other way.

Thanks.

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [November 3, 2013, 6:23am UTC](https://forum.ansible.com/t/proper-escaping-quotes-and-braces/13811/7 "2013-11-03T06:23:09Z")

</div>

Try using YAML “\>” quoting mechanism. This causes the block following the “\>” to be interpreted as a YAML scalar, and hence, ignores other YAML special characters like "{ " and ": "

- name: test escaping  
command: \>  
mycommand subcommand --option=“{first-item: 2}”

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 3, 2013, 4:46pm UTC](https://forum.ansible.com/t/proper-escaping-quotes-and-braces/13811/8 "2013-11-03T16:46:37Z")

</div>

When you said you inserted outer quotes, the example you showed did not show any outer quotes at all.

I mean quote the entire value.

Alternatively, see what Kahil wrote.

---

<div class="post-metadata">

**Author:** ![Serega\_Sheypak](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@Serega\_Sheypak](https://forum.ansible.com/u/Serega_Sheypak)\
**Post date:** [April 30, 2015, 8:53am UTC](https://forum.ansible.com/t/proper-escaping-quotes-and-braces/13811/9 "2015-04-30T08:53:07Z")

</div>

Hi, trying to execute task:

- name: Create spark-metrics DB  
shell: curl -X POST http://{{ influxdb.host }}:{{ influxdb\_client\_port }}/db?u=root&p=root -d “{‘name’: ‘spark-metrics’}”

Ansible dislikes “:”

- name: Create spark-metrics DB  
shell: curl -X POST http://{{ influxdb.host }}:{{ influxdb\_client\_port }}/db?u=root&p=root -d “{‘name’: ‘spark-metrics’}”  
^  
We could be wrong, but this one looks like it might be an issue with  
missing quotes. Always quote template expression brackets when they  
start a value. For instance:

with\_items:

- {{ foo }}

Should be written as:

with\_items:

- “{{ foo }}”

---

<div class="post-metadata">

**Author:** ![Ulrich\_Hochholdinger](https://avatars.discourse-cdn.com/v4/letter/u/839c29/32.png) [@Ulrich\_Hochholdinger](https://forum.ansible.com/u/Ulrich_Hochholdinger)\
**Post date:** [April 30, 2015, 9:18am UTC](https://forum.ansible.com/t/proper-escaping-quotes-and-braces/13811/10 "2015-04-30T09:18:31Z")

</div>

Hi,  
Alternatively you can use the “\> notation” :

shell: \>  
curl -X POST http://{{ influxdb.host }}:{{ influxdb\_client\_port }}/db?u=root&p=root -d “{‘name’: ‘spark-metrics’}”

Cheers  
Ulli

---

<div class="post-metadata">

**Author:** ![Serega\_Sheypak](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@Serega\_Sheypak](https://forum.ansible.com/u/Serega_Sheypak)\
**Post date:** [April 30, 2015, 10:53am UTC](https://forum.ansible.com/t/proper-escaping-quotes-and-braces/13811/11 "2015-04-30T10:53:42Z")

</div>

Here is working solution

```auto
- name: Create spark-metrics DB
  uri:
    url: "http://{{ influxdb.host }}:{{ influxdb_client_port }}/db?u=root&p=root"
    method: POST
    body: "{\"name\": \"spark-metrics\"}"
    status_code: 201

```
