0

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
Smily
  • 2,308
  • 2
  • 17
  • 41
hades11
  • 3
  • 1

1 Answers1

0

You cannot register without an ansible module. there is no output to be registered here.

- name: defined the new variable
  register: resultmatch
  when: "{{ ansible_hostname }} is in result.content"

Use as below if you need to define a new variable.

- name: defined the new variable
  set_fact:
    resultmatch: true
  when: "{{ ansible_hostname }} is in result.content"
Smily
  • 2,308
  • 2
  • 17
  • 41