Proper escaping quotes and braces

6,760 views
Skip to first unread message

Anton Parkhomenko

unread,
Oct 31, 2013, 3:35:44 AM10/31/13
to ansible...@googlegroups.com
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?

Brian Coca

unread,
Oct 31, 2013, 10:48:02 AM10/31/13
to ansible...@googlegroups.com
First, ansible version and error output would be helpful, but taking a guess try this:

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

On Thu, Oct 31, 2013 at 3:35 AM, Anton Parkhomenko <chu...@gmail.com> wrote:
- name: test escaping
  command: mycommand subcommand --option='{"first-item": 2}'




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

Michael DeHaan

unread,
Oct 31, 2013, 8:08:10 PM10/31/13
to ansible...@googlegroups.com
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

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.




--
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.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Anton Parkhomenko

unread,
Nov 2, 2013, 10:38:19 AM11/2/13
to ansible...@googlegroups.com
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.

Michael DeHaan

unread,
Nov 2, 2013, 11:05:31 AM11/2/13
to ansible...@googlegroups.com
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.





--
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.

Anton Parkhomenko

unread,
Nov 2, 2013, 11:27:43 AM11/2/13
to ansible...@googlegroups.com
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.

Kahlil Hodgson

unread,
Nov 3, 2013, 1:23:09 AM11/3/13
to ansible...@googlegroups.com
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}"


Kahlil (Kal) Hodgson                       GPG: C9A02289
Head of Technology                         (m) +61 (0) 4 2573 0382
DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing.  You must remember that
the parts you are reassembling were disassembled by you.  Therefore,
if you can't get them together again, there must be a reason.  By all
means, do not use a hammer."  -- IBM maintenance manual, 1925

Michael DeHaan

unread,
Nov 3, 2013, 11:46:37 AM11/3/13
to ansible...@googlegroups.com
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.


Serega Sheypak

unread,
Apr 30, 2015, 4:53:07 AM4/30/15
to ansible...@googlegroups.com
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 }}"      


воскресенье, 3 ноября 2013 г., 17:46:37 UTC+1 пользователь Michael DeHaan написал:

Ulrich Hochholdinger

unread,
Apr 30, 2015, 5:18:36 AM4/30/15
to ansible...@googlegroups.com

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

Serega Sheypak

unread,
Apr 30, 2015, 6:54:08 AM4/30/15
to ansible...@googlegroups.com
Here is working solution

- 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

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/t5IvK04ywVk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages