1

I'm writing an Ansible role which will update the settings of an existing application configuration. The role must get the settings from the application's API. Alter a portion of the settings. Then POST the complete set back to the application.

 - name: Get Current Settings
  shell: curl -L -H "Accept: application/json" -u "{{auth}}" https://{{ server }}/api/settings
  register: settings

 - name: update settings
   <Do something to modify settings.stdout as JSON>
   register: newSettings  # Save
   when: settings.Changed = True

 - name: Post Settings
   shell: curl -L -u "{{auth}}" -X POST -H "Content-Type: application/json" -d {{newSettings}}
kenlukas
  • 3,616
  • 9
  • 25
  • 36
Wanderer
  • 544
  • 1
  • 7
  • 23

1 Answers1

1

I would consider using the URI module of Ansible.

Interacts with HTTP and HTTPS web services and supports Digest, Basic and WSSE HTTP authentication mechanisms.

You can do the first part with a GET:

- uri:
    url: http://www.example.com
    return_content: yes
  register: webpage

Do your data massaging and then do a POST:

- name: Example POST
  uri:
    url: https://your.example.com/rest/api/2/issue/
    method: POST
    user: your_username
    password: your_pass
    body: "{{ lookup('file','issue.json') }}"
    force_basic_auth: yes
    status_code: 201
    body_format: json

Edit:

As was pointed out in the comments you can use set_fact to set the value you change from the information you receive from the GET call.

- set_fact: one_fact="something" other_fact="{{ local_var }}"

Inside your put you would access the new fact like you would any other variable in Ansible

kenlukas
  • 3,616
  • 9
  • 25
  • 36
  • I will look at the URI module. However, it's the massaging I'm looking for help with. – Wanderer Nov 09 '18 at 19:25
  • 2
    Use `set_fact` to set a new variable with the result of your data massaging from the result of the `uri` – zigarn Nov 11 '18 at 11:36
  • I ended up using this: https://stackoverflow.com/questions/46025695/best-way-to-modify-json-in-ansible – Wanderer Nov 13 '18 at 16:15