在 Ansible 调试消息中搜索字符串

Searching Ansible debug messages for a string

我试图通过查看 nxos_facts 的某些输出结果来查找交换机上的所有上行接口:

 - name: get nxos facts via nxapi
  nxos_facts:
    provider: "{{ provider['nxapi'] }}"
    gather_subset:
      - "interfaces"

  register: nxfacts_nxapi

- debug: 
    msg: "{{ nxfacts_nxapi.ansible_facts.ansible_net_interfaces | to_nice_json}} "

并且我可以成功打印出调试以显示字典的结构:

"ansible_net_interfaces": {
        "Ethernet1/1": {
            "bandwidth": 1000000,
            "duplex": "full",
            "ipv4": {
                "address": "10.0.1.2",
                "masklen": 24
            },
            "macaddress": "0800.276d.ee15",
            "mtu": "1500",
            "speed": "1000 Mb/s",
            "state": "up",
            "type": "100/1000/10000 Ethernet"
        },
        "Ethernet1/10": {
            "bandwidth": 10000000,
            "duplex": "auto",
            "macaddress": "0800.276c.eecc",
            "mode": "access",
            "mtu": "1500",
            "speed": "auto-speed",
            "state": "down",
            "type": "100/1000/10000 Ethernet"
        },

但是我正在努力使用语法来取消引用字典以仅在 "state" 为 "up" 时打印?

我是运行以下版本:

ansible 2.3.1.0

非常感谢任何帮助。

您可以遍历接口字​​典并仅打印条件为 true 的那些元素。示例:

- name: mytask
  debug:
    msg: "{{ item }}"
  when: "item.value.state == 'up'"
  with_dict: "{{ nxfacts_nxapi.ansible_facts.ansible_net_interfaces }}"