如果 GET 中不存在该变量,则发送 POST



我正在尝试使用 ansible 进行 2 次调用 api。 第一个是 GET,它必须返回一个 json 响应并与变量匹配(或不匹配(。 第二个是 POST 仅当 GET 与变量匹配时才是 POST。

- 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

我希望脚本在将主机名设置到变量中时创建一个新组。 但似乎可变结果不被视为 json 内容,我还有一个问题是:

  • 名称:在此处定义新变量
    ^

如果没有 ansible 模块,则无法注册。 此处没有要注册的输出。

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

如果需要定义新变量,请使用如下方法。

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

最新更新