Syntax for running commands with braces in them

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

I know this isn’t really a direct answer to your question, but why not use the uri module instead?

Should have known, there’s always a module to do what I want better. Thanks very much I’ll try this out!

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

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}

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]$

never mind. I should have read the whole thread .

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 }}”
    `