I am trying with ansible to make 2 calls api. The first one is a GET which have to return a json response and match (or not) with a variable. The second one is a POST only if the GET match the variable.
- name: check if hostname exist
uri:
url: 'some_url'
headers:
Authorization: 'api-key'
Accept: 'application/json'
method: GET
validate_certs: yes
return_content: yes
body_format: json
register: result
- name: defined the new variable
register: resultmatch
when: "{{ ansible_hostname }} is in result.content"
- name: create the group with the hostname
uri:
url: ""
headers:
Authorization: 'api-key'
Accept: 'application/json'
method: POST
validate_certs: yes
body: "{
'{{ hostname }}': '{{ ansible_hostname }}'
}"
return_content: yes
body_format: json
register: add_secret
when: resultmatch is defined
I expect that the script create a new group when the hostname is setted into the variable. But it seems that the variable result is not seen as a json content and I have one other problem which is:
- name: defined the new variable
^ here