-1

我试图通过查看 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"
        },

但是我正在努力使用语法来取消引用字典以仅在“状态”为“向上”时打印?

我正在使用以下版本运行:

ansible 2.3.1.0

任何帮助深表感谢。

4

1 回答 1

1

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

- name: mytask
  debug:
    msg: "{{ item }}"
  when: "item.value.state == 'up'"
  with_dict: "{{ nxfacts_nxapi.ansible_facts.ansible_net_interfaces }}"
于 2017-07-17T14:28:46.520 回答