Syntax for running commands with braces in them

125 views
Skip to first unread message

Ryan Groten

unread,
Apr 12, 2016, 11:36:23 AM4/12/16
to Ansible Project
I am trying to execute a task that runs the following command (which includes a number of quote, brackets, colons, etc):

command: curl -XPUT "http://localhost:9200/_cluster/settings" -d'{ "transient" : { "cluster.routing.allocation.enable" : "none" } }'

I tried escaping all the quotes and {}, but that doesn't seem to work.  

command: 'curl -XPUT "http://localhost:9200/_cluster/settings" -d\'\{ "transient" : \{ "cluster.routing.allocation.enable" : "none" \} \}''

I also tried treating the whole command as a jinja string (I think?)

command: {{ 'curl -XPUT "http://localhost:9200/_cluster/settings" -d'{ "transient" : { "cluster.routing.allocation.enable" : "none" } }'' }}

Thanks,
Ryan

Matt Martz

unread,
Apr 12, 2016, 11:42:09 AM4/12/16
to ansible...@googlegroups.com
I know this isn't really a direct answer to your question, but why not use the `uri` module instead?

- uri:
    method: PUT
    body:
      transient:
        cluster.routing.allocation.enable: "none"
    body_format: json

--
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/ef4f9432-a75e-4ab1-91f1-487c8e0a68a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Ryan Groten

unread,
Apr 12, 2016, 1:02:54 PM4/12/16
to Ansible Project
Should have known, there's always a module to do what I want better.  Thanks very much I'll try this out!

Merri Jensen

unread,
Apr 12, 2016, 2:56:00 PM4/12/16
to Ansible Project
I ran into this problem too. If you don't happen to use the 'uri' module, it's the double quotes that need to be escaped, not the curly's...

command: curl -XPUT "http://localhost:9200/_cluster/settings" -d'{ \"transient\" : { \"cluster.routing.allocation.enable\" : \"none\" } }'

You need them for -H's too.
-H \"Content-Type: application/json\"

HTH,
-MJensen

Ryan Groten

unread,
Apr 12, 2016, 4:12:46 PM4/12/16
to Ansible Project
I must be close, but it's not working with uri module.  Here's what I put in the role.  Setting the uri_body as a fact beforehand and using "to_json" was an attempt to get around the error I'm getting, but that doesn't help either.

- name: Set uri_body (workaround)
  set_fact
:
    uri_body
:
     
transient:
        cluster
.routing.allocation.enable: "none"

- name: Disable shard allocation to prevent es from rebalancing missing shards
  uri
:

    url
: "http://localhost:9200/_cluster/settings"
    method
:
PUT
    body
: "{{ uri_body | to_json }}"
    body_format
: json

When I run it I get this:

fatal: [vm2.example.com]: FAILED! => {"changed": false, "failed": true, "module_stderr": "", "module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible-tmp-1460491268.69-8119459959976/uri\", line 3363, in <module>\r\n    main()\r\n  File \"/tmp/ansible-tmp-1460491268.69-8119459959976/uri\", line 374, in main\r\n    dict_headers['Content-Type'] = 'application/json'\r\nTypeError: 'NoneType' object does not support item assignment\r\n", "msg": "MODULE FAILURE", "parsed": false}


On Tuesday, 12 April 2016 09:42:09 UTC-6, Matt Martz wrote:

ac427

unread,
Apr 13, 2016, 2:44:19 PM4/13/16
to Ansible Project
I didn't quite follow the exact issue, but I was able to download a page with {: in file name . Below is my test

[vagrant@admin ansible]$ ansible-playbook -i hosts -u root  --tags=url -l admin base.yml

PLAY [base role] **************************************************************

GATHERING FACTS ***************************************************************
ok: [admin]

TASK: [base | get url with] ***************************************************
changed: [admin] => (item=http://localhost/hello{:.html)

PLAY RECAP ********************************************************************
admin                      : ok=2    changed=1    unreachable=0    failed=0  

[vagrant@admin ansible]$
[vagrant@admin ansible]$ cat roles/base/tasks/urltest.yml
- name : get url with
  get_url: url={{ item }} dest=/tmp
  tags: url
  with_items:
        - http://localhost/hello{:.html
[vagrant@admin ansible]$ ls /tmp/hello\{\:.html
/tmp/hello{:.html
[vagrant@admin ansible]$

ac427

unread,
Apr 13, 2016, 3:13:46 PM4/13/16
to Ansible Project
never mind. I should have read the whole thread .


On Tuesday, April 12, 2016 at 4:12:46 PM UTC-4, Ryan Groten wrote:

Ryan Groten

unread,
Apr 14, 2016, 10:26:51 AM4/14/16
to Ansible Project
Finally got it working, thanks everyone for the suggestions!  For reference here's what ended up working:
- name: Set uri endpoints
  set_fact
:
    es_disable_allocation
:

     
transient:
        cluster
.routing.allocation.enable: "none"

- name: Disable shard allocation to prevent es from rebalancing missing shards
  uri
:
    url
: "http://localhost:9200/_cluster/settings"
    method
:
PUT
    body
: "{{ es_disable_allocation|to_json }}"
Reply all
Reply to author
Forward
0 new messages