【问题标题】:Ansible + JMESPath: Return name of key when contains matches against nested dataAnsible + JMESPath:当包含与嵌套数据的匹配项时返回键的名称
【发布时间】:2020-03-03 12:29:31
【问题描述】:

源数据是 JSON 字典,其中顶级键名不可预测。当其中的嵌套数据与我拥有的字符串匹配时,我需要该 TL 键的名称。

我正在尝试使用 Ansible 2.9 中的 json_query 过滤器。

我尝试了各种 JMESPath 查询并搜索了他们的文档,但我发现的所有内容似乎都需要提前知道这些键的名称。

这里是示例数据 - 来自 ansible ios_facts 模块针对接口的输出:

{
        "GigabitEthernet0/9": {
            "description": "server",
            "ipv4": [],
            "macaddress": "b8zz.xxxx.7709",
            "type": "Gigabit Ethernet"
        },
        "Vlan13": {
            "description": null,
            "ipv4": [
                {
                    "address": "10.20.30.19",
                    "subnet": "24"
                }
            ],
            "macaddress": "b8zz.xxx.yy41",
            "type": "EtherSVI"
        }
}

这是我正在使用的剧本:

---
- name: Cisco IOS - Mgmt Int
  connection: network_cli
  gather_facts: false
  hosts: switch01
  vars:
     netdb: "{{ hostvars[inventory_hostname]['ansible_net_interfaces'] }}"
  tasks:
    - name: get ios facts
      ios_facts:
        gather_subset: interfaces
    - name: json_query
      set_fact:
        mgmtinf: "{{ netdb | json_query(query) }}"
      vars:
        # this gets me true/false
        query: "[Vlan13.ipv4[0].address.contains(@, '10.20.30.19')]"
    - name: output json_query
      debug:
        var: mgmtinf

如果结果匹配,上述方法将返回真/假。问题是我手动定义了查看“Vlan13”的接口。所以我需要一种方法来为该 TL 键使用迭代器,然后在循环中发生匹配的部分返回该迭代器的值。

【问题讨论】:

    标签: ansible jmespath


    【解决方案1】:

    您没有给出选择匹配键名的所有要求,但以下内容应该会让您步入正轨。

    重点:

    • 使用dict2tems filter 将顶级字典转换为{key: X, value: Y} 对象列表
    • 使用 jmespath 来:
      1. 过滤掉vlan对应的元素(我在我的例子中使用了“Vlan*”)
      2. 过滤掉每个 ipv4 元素的 ips 列表,以仅匹配您所需的 IP。

    注意:我的表达式中的 to_json | from_json 是克服 ansible 和 jmespath 之间通信中的 current bug 所必需的(字符串类型映射)。

    这是一个说明上述场景的示例任务。

        - name: Filter out specific ip elements on my vlans
          vars:
            vals: {
              "GigabitEthernet0/9": {
                "description": "server",
                "ipv4": [],
                "macaddress": "b8zz.xxxx.7709",
                "type": "Gigabit Ethernet"
              },
              "Vlan13": {
                "description": null,
                "ipv4": [
                {
                  "address": "10.20.30.19",
                  "subnet": "24"
                }
                ],
                "macaddress": "b8zz.xxx.yy41",
                "type": "EtherSVI"
              }
            }
            query: >-
              [?starts_with(key, 'Vlan')].value.ipv4[] | [?contains(address, '10.20.30.19')]
          debug:
            msg: >-
              {{ vals | dict2items | to_json | from_json | json_query(query) }}
    

    我用你当前的值得到的结果:

    TASK [Filter out specific ip elements on my vlans] *************************************************************************************************************************************************************************************************************************************************************
    ok: [localhost] => {
        "msg": [
            {
                "address": "10.20.30.19",
                "subnet": "24"
            }
        ]
    }
    
    

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 2011-02-23
      • 2020-01-07
      • 1970-01-01
      • 2016-08-03
      • 2013-09-06
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      相关资源
      最近更新 更多