Ansible:使用 JMESPath 过滤包含字符串的元素

Ansible : filter elements containing string with JMESPath

我想获取已定义接口类型的地址列表。
我找到了一些信息

这是我的剧本:

- name: Test JMESPath
  hosts: localhost
  gather_facts: no

  vars:
    interfaces:
    - name: em0
      address: 10.127.37.89/29
    - name: bge0
      address: 10.112.171.81/28
    - name: bge1
      address: 10.112.171.65/28
    - name: bge2
      address: 10.112.171.97/28
  tasks:
    - name: JMESPath query
      set_fact:
        result: "{{ interfaces | json_query(query) }}"
      vars:
        query: "[?name.contains(@, 'bge')].address"

    - debug:
        var: result

我想得到:

[
  "10.112.171.81/28",
  "10.112.171.65/28",
  "10.112.171.97/28"
]

它适用于 JMESPath 网站,但我的剧本失败了:

ansible-playbook play-testJMESPath.yml [WARNING]: provided hosts list
is empty, only localhost is available. Note that the implicit
localhost does not match 'all'

PLAY [Test JMESPath]
**************************************************************************************************************************************************************************************************

TASK [JMESPath query]
************************************************************************************************************************************************************************************************* fatal: [localhost]: FAILED! => {"msg": "JMESPathError in json_query
filter plugin:\nIn function contains(), invalid type for value:
external, expected one of: ['array', 'string'], received:
\"unknown\""}

PLAY RECAP
************************************************************************************************************************************************************************************************************ localhost                  : ok=0    changed=0    unreachable=0   
failed=1    skipped=0    rescued=0    ignored=0

谁能解释一下为什么?

Jinja filters can use Python regex tests. This makes them a stronger tool compared to JMESPath contains function 仅当字符串包含提供的 $search 参数时 " returns 为真".

下面的任务

    - set_fact:
        result: "{{ interfaces|
                    selectattr('name', 'search', 'bge')|
                    map(attribute='address')|
                    list }}"
    - debug:
        var: result

给予

    "result": [
        "10.112.171.81/28",
        "10.112.171.65/28",
        "10.112.171.97/28"
    ]

对于您遇到的 JMESPath 问题,请在此处进行解释:

The problem is related to the fact that Ansible uses own types for strings: AnsibleUnicode and AnsibleUnsafeText. And as long as jmespath library has very strict type-checking, it fails to accept this types as string literals.

来源:https://github.com/ansible/ansible/issues/27299#issuecomment-331068246


trick 使它工作,如在同一期中解释的那样,是使用 to_json | from_json 过滤器对,以强制返回正确的类型。

所以,剧本:

- hosts: localhost
  gather_facts: no

  tasks:
    - debug:
        msg: "{{ interfaces | to_json | from_json | json_query(query) }}"
      vars:
        query: "[?name.contains(@, 'bge')].address"
        interfaces:
          - name: em0
            address: 10.127.37.89/29
          - name: bge0
            address: 10.112.171.81/28
          - name: bge1
            address: 10.112.171.65/28
          - name: bge2
            address: 10.112.171.97/28

给出预期:

TASK [debug] *****************************************************************************************************
ok: [localhost] => {
    "msg": [
        "10.112.171.81/28",
        "10.112.171.65/28",
        "10.112.171.97/28"
    ]
}

PLAY RECAP *******************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0