Brian.Terry
Brian.Terry

Reputation: 1

Convert curl API PUT to Ansbile Playbook

Currently, I use curl to send an HTTP PUT to my API:

curl -k -s -u icinga:icinga -H 'Accept: application/json' -X PUT 'https://localhost:5665/v1/objects/hosts/dbserver.example.com' -d '{ "templates": [ "generic-host" ], "attrs": { "zone": "vienna", "address": "xxx.xx.xx.x", "check_command": "hostalive", "vars.os" : "Linux", "vars.agent" : "ssh" } }' | python -m json.tool

This works like a charm.

I'm trying to convert this api call to an ansible playbook. I know that ansible offer the URI module, so I tried to use that, but perhaps something is not configured properly.

---
- name: Add new host
  uri:
      url: icinga2.example.com:5665/v1/objects/hosts/client.example.com
      method: PUT
      user: admin
      password: xxxxxxx
      body: { templates: [ "generic-host" ], attrs: { "zone": "vienna", 
"address": "172.x.x.xx", "check_command": "hostalive", "vars.os" : "Linux", "vars.agent" : "ssh" } }
      headers: "application/json"
  register: icinga_log
  when: inventory_hostname in groups ['vienna']
  with_items: "{{ groups['icinga-monitoring'] }}"

Upvotes: 0

Views: 791

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

Usually you could follow error messages that ansible produces and fix your syntax.
Try to start with this modification:

- name: Add new host
  uri:
    url: http://icinga2.example.com:5665/v1/objects/hosts/client.example.com
    method: PUT
    user: admin
    password: xxxxxxx
    body: '{ templates: [ "generic-host" ], attrs: { "zone": "vienna", "address": "172.x.x.xx", "check_command": "hostalive", "vars.os" : "Linux", "vars.agent" : "ssh" } }'
    body_format: json
    headers:
        Content-Type: application/json

Upvotes: 1

Related Questions