小编moo*_*oky的帖子

使用 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
Run Code Online (Sandbox Code Playgroud)

我想得到:

[
  "10.112.171.81/28",
  "10.112.171.65/28",
  "10.112.171.97/28"
]
Run Code Online (Sandbox Code Playgroud)

它可以在 JMESPath 网站上运行,但我的剧本失败了:

ansible-playbook play-testJMESPath.yml [WARNING]: provided hosts list
is empty, only localhost is available. Note that the implicit
localhost …
Run Code Online (Sandbox Code Playgroud)

ansible jmespath json-query

5
推荐指数
1
解决办法
1817
查看次数

标签 统计

ansible ×1

jmespath ×1

json-query ×1