对于Ansible Playbook,我需要从API调用中获得一些结果,以检查ID是否已经存在。
这应该是一个程序:
创建配置。
我想出了一个解决方案来检查变量result中的JSON是否包含字段"ID:" "output-1"。如果是这样的话,它将变量id_exists的值设置为true,然后可以在以下任务中用作条件。
不幸的是,我不知道使用哪个Ansible模块在JSON中搜索和注册一个变量。也许有人能帮我找到解决办法?
不可接受的例子:
- name: "Call API to get JSON data"
ansible.builtin.uri:
url: https://example.com/api/outputs
method: get
register: result
- name: "Debug: Print all IDs"
ansible.builtin.debug:
msg: "{{ result.json | json_query(jmesquery) }}"
vars:
jmesquery: "items[*].{ID: id}"
## Task: Check if JSON result contains the ID output-1
## DON'T KNOW HOW :(
- name: "Create new config"
url: https://example.com/api/outputs
method: post
headers:
Content-Type: application/json
Authorization: "ApiKey {{ api_key }}"
body_format: json
body: |
{
"id": "output-1",
"name": "Name",
"is_default": false,
"type": "testtype",
"config_yaml": "verification_mode: advanced"
}
when: id_exists == false
- name: "Update existing config"
ansible.builtin.uri:
url: https://example.com/api/outputs/output-1
method: put
headers:
Content-Type: application/json
Authorization: "ApiKey {{ api_key }}"
body_format: json
body: |
{
"name": "New Name",
"is_default": false,
"type": "testtype",
"config_yaml": "verification_mode: advanced"
}
when: id_exists == trueJSON示例:
{
"items": [
{
"id": "output-1",
"name": "Output 1",
"is_default": true,
"type": "testtype",
"config_yaml": "verification_mode: basic"
},
{
"id": "output-2",
"name": "Output 2",
"is_default": false,
"type": "testtype",
"config_yaml": "verification_mode: basic"
},
{
"id": "output-3",
"name": "Output 3",
"is_default": false,
"type": "testtype",
"config_yaml": "verification_mode: basic"
}
]
}非常感谢!
发布于 2022-11-29 15:02:16
如果我理解正确的话,下面的内容应该符合你的期望。我添加了几个示例作为调试任务,让您使用结果并自行决定如何调用API (我自己无法运行示例)。
下面的剧本
---
- hosts: localhost
gather_facts: false
vars:
# Faking a return from you API based on example json (minified for legibility)
result: {"items":[{"id":"output-1","name":"Output 1","is_default":true,"type":"testtype","config_yaml":"verification_mode: basic"},{"id":"output-2","name":"Output 2","is_default":false,"type":"testtype","config_yaml":"verification_mode: basic"},{"id":"output-3","name":"Output 3","is_default":false,"type":"testtype","config_yaml":"verification_mode: basic"}]}
# What to find in haystack
needle_id: output-1
# Note: yes this will work with a register as well as long as it exists
# at time you evaluate the var.
# Note: having a key called `items` forces us to use the bracket notation
# to disambiguate from the `items()` python function
existing_id: "{{ result['items'] | selectattr('id', '==', needle_id) }}"
tasks:
- name: play a task if id exists
debug:
msg: id exists
when: existing_id | length > 0
- name: play a task if id does not exist
debug:
msg: id does not exist
when: existing_id | length == 0
- name: play single tasks which will be smart
vars:
debug_text: "{{ 'does not exist' if existing_id | length == 0 else 'exists' }}"
debug:
msg: "id {{ debug_text }}"
- name: show the found entry (or empty if not)
debug:
var: existing_id | first | d({})给予:
$ ansible-playbook uri.yml
PLAY [localhost] ***********************************************************************************************************************************************************************************************************************
TASK [play a task if id exists] ********************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "id exists"
}
TASK [play a task if id does not exist] ************************************************************************************************************************************************************************************************
skipping: [localhost]
TASK [play single tasks which will be smart] *******************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "id exists"
}
TASK [show the found entry (or empty if not)] ******************************************************************************************************************************************************************************************
ok: [localhost] => {
"existing_id | first | d({})": {
"config_yaml": "verification_mode: basic",
"id": "output-1",
"is_default": true,
"name": "Output 1",
"type": "testtype"
}
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0https://stackoverflow.com/questions/74613249
复制相似问题