使用 ansible playbook 和查找插件,如何將curl https://ifconfig.io/all具有字典格式的“”的結果轉換key: value為專案串列
country_code: US
encoding: gzip
forwarded: 29.493.593.012
host: XXX.XXX.X53.2XX
ifconfig_hostname: ifconfig.io
ip: XXX.XXX.X53.2XX
lang: ""
method: GET
mime: '/'
port: 27404
referer: ""
ua: Python-urllib/3.6
所需的輸出:此格式的專案串列:
[
{
"key": "country_code",
"value": "US"
},
{
"key": "encoding",
"value": "gzip"
},
{
"key": "forwarded",
"value": "29.493.593.012"
},
{
"key": "host",
"value": "XXX.XXX.X53.2XX"
},
{
"key": "ifconfig_hostname",
"value": "ifconfig.io"
},
{
"key": "ip",
"value": "XXX.XXX.X53.2XX"
},
{
"key": "lang",
"value": "''"
},
{
"key": "method",
"value": "GET"
},
{
"key": "mime",
"value": "''"
},
{
"key": "port",
"value": "28180"
},
{
"key": "referer",
"value": "''"
},
{
"key": "ua",
"value": "Python-urllib/3.6"
}
]
我使用的腳本是:
- name: Get url contents
vars:
curl_res: "{{ query('url','https://ifconfig.io/all') }}"
debug:
msg: "{{ item }}"
loop: "{{ curl_res | dict2items }}"
但我有這個錯誤:
TASK [Get url contents] *****************************************************************************************************************
fatal: [servera]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ curl_res | dict2items }}): dict2items requires a dictionary, got <class 'list'> instead."}
因此,我手動制作,將 curl 結果保存在本地檔案中,然后將其微調為以下格式:
cat files/curl_result
{
"country_code": "US",
"encoding": "gzip",
"forwarded": "29.493.593.012",
"host": "XXX.XXX.X53.2XX",
"ifconfig_hostname": "ifconfig.io",
"ip": "XXX.XXX.X53.2XX",
"lang": "''",
"method": "GET",
"mime": "''",
"port": "28580",
"referer": "''",
"ua": "Python-urllib/3.6"
}
然后,我在我的游戲中運行了這個任務:
- name: extract from file
vars:
items_curl_res: "{{ lookup('file', 'curl_result') | from_json | dict2items }}"
debug:
var: items_curl_res
uj5u.com熱心網友回復:
您得到的回傳不是字典,而是表單中的字串串列key: value。
該表單實際上是一種有效的 YAML 格式,因此您可以使用過濾器對所有這些字串map應用from_yaml過濾器,然后map是串列dict2items,最后flatten是串列。
結束這個任務:
- debug:
var: >-
query('url','https://ifconfig.io/all')
| map('from_yaml')
| map('dict2items')
| flatten
該任務將產生:
query('url','https://ifconfig.io/all') | map('from_yaml') | map('dict2items') | flatten:
- key: country_code
value: BE
- key: encoding
value: gzip
- key: forwarded
value: 93.184.216.34
- key: host
value: 93.184.216.34
- key: ifconfig_hostname
value: ifconfig.io
- key: ip
value: 93.184.216.34
- key: lang
value: ''
- key: method
value: GET
- key: mime
value: ''
- key: port
value: 24294
- key: referer
value: ''
- key: ua
value: ansible-httpget
uj5u.com熱心網友回復:
哇..它正在作業,謝謝β.εηοιτ.βε:
這里的作業腳本:
- name: Get url contents
debug:
msg: "{{ query('url','https://ifconfig.io/all') | map('from_yaml') | map('dict2items') | flatten }}"
和輸出:
"msg": [
{
"key": "country_code",
"value": "US"
},
{
"key": "encoding",
"value": "gzip"
},
{
"key": "forwarded",
"value": "XXX.XX3.8XX.X9X"
},
{
"key": "host",
"value": "XXX.XX3.8XX.X9X"
},
{
"key": "ifconfig_hostname",
"value": "ifconfig.io"
},
{
"key": "ip",
"value": "3XX.X7X.XX4.XX0"
},
{
"key": "lang",
"value": ""
},
{
"key": "method",
"value": "GET"
},
{
"key": "mime",
"value": ""
},
{
"key": "port",
"value": 33470
},
{
"key": "referer",
"value": ""
},
{
"key": "ua",
"value": "Python-urllib/3.6"
}
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/453489.html
