Ansible解析json并将结果读入不同的变量
我已经设置了一个任务,它查询github api元端点并返回以下内容Ansible解析json并将结果读入不同的变量,json,parsing,ansible,Json,Parsing,Ansible,我已经设置了一个任务,它查询github api元端点并返回以下内容 { "verifiable_password_authentication": true, "github_services_sha": "f9e3a6b98d76d9964a6613d581164039b8d54d89", "hooks": [ "192.30.252.0/22", "185.199.108.0/22", "140.82.112.0/20" ], "git": [
{
"verifiable_password_authentication": true,
"github_services_sha": "f9e3a6b98d76d9964a6613d581164039b8d54d89",
"hooks": [
"192.30.252.0/22",
"185.199.108.0/22",
"140.82.112.0/20"
],
"git": [
"192.30.252.0/22",
"185.199.108.0/22",
"140.82.112.0/20",
"13.229.188.59/32",
"13.250.177.223/32",
"18.194.104.89/32",
"18.195.85.27/32",
"35.159.8.160/32",
"52.74.223.119/32"
],
"pages": [
"192.30.252.153/32",
"192.30.252.154/32",
"185.199.108.153/32",
"185.199.109.153/32",
"185.199.110.153/32",
"185.199.111.153/32"
],
"importer": [
"54.87.5.173",
"54.166.52.62",
"23.20.92.3"
]
}
我需要做的是获取3个钩子IP,并将它们分别读入各自的变量中
我已经尝试了一些我找到的解决方案,但似乎没有什么对我有效
我已经深入研究了json,所以只返回了3个IP,但是如何将它们分别取出并放入变量?我在变量名部分使用了j2语法,并且-TIL-看起来在该部分也允许使用jinja2语法 请参阅剧本以处理
挂钩
列出变量并分配给变量变量_1
、变量_2
、变量_3
等:
- hosts: localhost
gather_facts: false
vars:
counter: 1
hooks:
- 192.30.252.0/22
- 185.199.108.0/22
- 140.82.112.0/20
tasks:
- name: populate vars
set_fact:
variable_{{counter}}: "{{ item }}"
counter: "{{ counter | int + 1 }}"
with_items:
- "{{ hooks }}"
- name: print vars
debug:
msg: "variable_1: {{variable_1}}, variable_2: {{variable_2}}, variable_3: {{variable_3}}"
以及输出:
[root@optima-ansible ILIAS]# ansible-playbook 50257063.yml
PLAY [localhost] ***********************************************************************************************************************************************************************************************************************
TASK [populate vars] *******************************************************************************************************************************************************************************************************************
ok: [localhost] => (item=192.30.252.0/22)
ok: [localhost] => (item=185.199.108.0/22)
ok: [localhost] => (item=140.82.112.0/20)
TASK [print vars] **********************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "variable_1: 192.30.252.0/22, variable_2: 185.199.108.0/22, variable_3: 140.82.112.0/20"
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
[root@optima-ansible ILIAS]#
希望能有帮助
更新:
我注意到一件奇怪的事——也是直到——就是如果你把线颠倒过来:
variable_{{counter}}: "{{ item }}"
counter: "{{ counter | int + 1 }}"
致:
您仍然会得到相同的变量名,_1到_3,而我希望得到_2到_4
我猜ansible循环的行为与其他编程语言不同。在循环中使用GitHub Webhook IP
---
- name: Query Github Meta API and get Hook Ips
hosts: local
connection: local
vars:
counter: 1
tasks:
- name: Query API
uri:
url: https://api.github.com/meta
return_content: yes
register: response
- name: Populate Hook Variables
set_fact:
webhook_ip_{{counter}}: "{{ item }}"
counter: "{{ counter | int + 1 }}"
with_items:
- "{{ response['json']['hooks'] }}"
- name: print vars
debug:
msg: "Variable_1: {{ webhook_ip_1 }}, Variable_2: {{ webhook_ip_2 }}, Variable_3: {{ webhook_ip_3 }}"
非常感谢你的帮助。我基于这个答案的解决方案如下。很高兴它有帮助!如果有兴趣,请查看我添加的更新部分,了解我注意到的一件奇怪的事情
---
- name: Query Github Meta API and get Hook Ips
hosts: local
connection: local
vars:
counter: 1
tasks:
- name: Query API
uri:
url: https://api.github.com/meta
return_content: yes
register: response
- name: Populate Hook Variables
set_fact:
webhook_ip_{{counter}}: "{{ item }}"
counter: "{{ counter | int + 1 }}"
with_items:
- "{{ response['json']['hooks'] }}"
- name: print vars
debug:
msg: "Variable_1: {{ webhook_ip_1 }}, Variable_2: {{ webhook_ip_2 }}, Variable_3: {{ webhook_ip_3 }}"
- name: get request to github
uri:
url: "https://api.github.com/meta"
method: GET
return_content: yes
status_code: 200
headers:
Content-Type: "application/json"
#X-Auth-Token: "0010101010"
body_format: json
register: json_response
- name: GitHub webhook IPs
debug:
msg: "{{ item }}"
with_items: "{{ (json_response.content | from_json).hooks }}"