将 curl 命令生成的字典转换为项目列表 - Ansible

Convert a dictionary resulted from curl command to item list - Ansible

使用 ansible 剧本和查找插件,如何将具有字典格式 key: value 的“curl https://ifconfig.io/all”的结果转换为项目列表

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."}

所以,我手动制作了它,将卷曲结果保存在本地文件中,然后将其微调为这种格式:

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

您得到的 return 不是字典,而是 key: value.

形式的字符串列表

该表格实际上是有效的 YAML 格式,因此您可以使用 map to apply a from_yaml filter to all those string, then, map again a dict2items and finally, 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

哇..它正在工作,谢谢 β.εηοιτ.βε:

这里是工作脚本:

- 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"
    }
]