28

I want to execute the next command using ansible playbook:

curl -X POST -d@mesos-consul.json -H "Content-Type: application/json" http://marathon.service.consul:8080/v2/apps

How can I run it?

If I run:

- name: post to consul
  uri:
    url: http://marathon.service.consul:8080/v2/apps/
    method: POST
    body: "{{ lookup('file','mesos-consul.json') }}"
    body_format: json
    HEADER_Content-Type: "application/json"

I have the next fail:

fatal: [172.16.8.231]: FAILED! => {"failed": true, "msg": "ERROR! the file_name '/home/ikerlan/Ik4-Data-Platform/ansible/playbooks/Z_PONER_EN_MARCHA/dns-consul/mesos-consul.j2' does not exist, or is not readable"}

Asier Gomez
  • 6,034
  • 18
  • 52
  • 105

1 Answers1

56

The best way to do this is to use the URI module:

tasks:
- name: post to consul
  uri:
    url: http://marathon.service.consul:8080/v2/apps/
    method: POST
    body: "{{ lookup('file','mesos-consul.json') }}"
    body_format: json
    headers:
      Content-Type: "application/json"

Since your json file is on the remote machine the easiest way to execute is probably with the shell module:

- name: post to consul
  shell: 'curl -X POST -d@/full/path/to/mesos-consul.json -H "Content-Type: application/json" http://marathon.service.consul:8080/v2/apps'
conradkleinespel
  • 6,560
  • 10
  • 51
  • 87
MillerGeek
  • 3,057
  • 20
  • 23
  • Thanks @smiller171 but it tryes to execute the file 'mesos-consul.json' of the playbook, and not of the remote machine, I have a fail when I execute your module, it is in the description. – Asier Gomez Mar 07 '16 at 14:51
  • You can either use the [fetch module](http://docs.ansible.com/ansible/fetch_module.html) to retrieve the json file from the remote node, and then use the URI module, or you can use the "script" module to execute the curl comand on the remote node. I can update my answer to illustrate whichever you prefer. – MillerGeek Mar 07 '16 at 15:44
  • I think using script module is the easiest way to run the command, I don't know so much because I don't have so much experience wich ansible, so please I would like the easiest way to understand. Thanks @smiller171 – Asier Gomez Mar 07 '16 at 15:49
  • Updated with shell module alternative. – MillerGeek Mar 07 '16 at 16:24