问题描述
我正在尝试 Ansible 获取所有Kong路由名称,然后循环遍历这些名称以获取每个路由的插件ID。 然后遍历该列表,然后PATCH和插件设置。 从下面我可以获取Kong路由名称,然后可以遍历这些路由名称以获取列出插件响应的变量。 我现在遇到的问题是使用json查询提取插件ID。
有帮助吗?
---
- name: Get Kong route names"
uri:
url: "https://kong-api-gateway.bleh.com/routes"
method: GET
return_content: yes
headers:
Content-Type: "application/json"
register: result
- set_fact: api_names="{{ result.content | from_json | json_query('data[*].name') | list }}"
- debug:
msg: item
loop: "{{api_names}}"
- name: Get Kong route plugin IDs"
uri:
url: "https://kong-api-gateway.bleh.com/routes/{{item}}/plugins"
method: GET
return_content: yes
headers:
Content-Type: "application/json"
register: plugin_get
loop: "{{api_names}}"
- debug:
var: plugin_get
- set_fact: plugin_id="{{ plugin_get.content | from_json | json_query("data[*].id") | list }}"
- debug:
msg: item
loop: "{{plugin_id}}"
- name: Set plugin IDs"
uri:
url: "https://kong-api-gateway.bleh.com/routes/{{ item.0 }}/plugins/{{ item.1 }}"
method: PATCH
return_content: yes
body_format: json
validate_certs: no
body:
- [ enabled,true ]
with_together:
- "{{api_names}}"
- "{{plugin_id}}"
这是 ansible
中的 json 输出结果ok: [127.0.0.1] => {
"plugin_get": {
"changed": false,"msg": "All items completed","results": [
{
"access_control_allow_origin": "*","ansible_loop_var": "item","cache_control": "private","changed": false,"connection": "close","content": "{\"next\":null,\"data\":[{\"created_at\":1602095924,\"id\":\"3f75fe73-793e-4a8e-982f-583523dab7bd\",\"tags\":null,\"enabled\":false,\"protocols\":[\"grpc\",\"grpcs\",\"http\",\"https\"],\"name\":\"request-termination\",\"consumer\":null,\"service\":null,\"route\":{\"id\":\"632d3629-2197-48de-9b09-fab68c4caf55\"},\"config\":{\"status_code\":503,\"content_type\":null,\"body\":null,\"message\":\"API disabled for Security\"}}]}","content_length": "377","content_type": "application/json; charset=utf-8","cookies": {
"403f4eb216679cf1237771f186164832": "0dcc7f3969bcf24f2fd2ec6b458d2504"
},"cookies_string": "403f4eb216679cf1237771f186164832=0dcc7f3969bcf24f2fd2ec6b458d2504","date": "Sun,25 Oct 2020 11:17:27 GMT","elapsed": 0,"Failed": false,"invocation": {
"module_args": {
"attributes": null,"backup": null,"body": null,"body_format": "raw","client_cert": null,"client_key": null,"content": null,"creates": null,"delimiter": null,"dest": null,"directory_mode": null,"follow": false,"follow_redirects": "safe","force": false,"force_basic_auth": false,"group": null,"headers": {
"Content-Type": "application/json"
},"http_agent": "ansible-httpget","method": "GET","mode": null,"owner": null,"regexp": null,"remote_src": null,"removes": null,"return_content": true,"selevel": null,"serole": null,"setype": null,"seuser": null,"src": null,"status_code": [
200
],"timeout": 30,"unix_socket": null,"unsafe_writes": null,"url": "https://kong-api-gateway.bleh.com/routes/branch-bank-table_route/plugins","url_password": null,"url_username": null,"use_proxy": true,"validate_certs": true
}
},"item": "branch-bank-table_route","json": {
"data": [
{
"config": {
"body": null,"content_type": null,"message": "API disabled for Security","status_code": 503
},"consumer": null,"created_at": 1602095924,"enabled": false,"id": "3f75fe73-793e-4a8e-982f-583523dab7bd","name": "request-termination","protocols": [
"grpc","grpcs","http","https"
],"route": {
"id": "632d3629-2197-48de-9b09-fab68c4caf55"
},"service": null,"tags": null
}
],"next": null
},"msg": "OK (377 bytes)","redirected": false,"server": "kong/2.1.4","set_cookie": "403f4eb216679cf1237771f186164832=0dcc7f3969bcf24f2fd2ec6b458d2504; path=/; HttpOnly; Secure","status": 200,"x_kong_admin_latency": "7"
},{
"access_control_allow_origin": "*",\"data\":[{\"created_at\":1602095878,\"id\":\"00f06ea0-908d-4581-9d47-d99b3fa5117c\",\"route\":{\"id\":\"7b601406-e866-4c2d-a2be-bc330f8fab14\"},"cookies": {
"403f4eb216679cf1237771f186164832": "88e8fecde4bcffb8f5b32164b4da4b6b"
},"cookies_string": "403f4eb216679cf1237771f186164832=88e8fecde4bcffb8f5b32164b4da4b6b",25 Oct 2020 11:17:28 GMT","url": "https://kong-api-gateway.bleh.com/routes/branch-ofv-rawtowsq_route/plugins","item": "branch-ofv-rawtowsq_route","created_at": 1602095878,"id": "00f06ea0-908d-4581-9d47-d99b3fa5117c","route": {
"id": "7b601406-e866-4c2d-a2be-bc330f8fab14"
},"set_cookie": "403f4eb216679cf1237771f186164832=88e8fecde4bcffb8f5b32164b4da4b6b; path=/; HttpOnly; Secure","x_kong_admin_latency": "6"
}
]
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)