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

时间:2017-07-15 11:35:48

标签: python-2.7 cisco ansible-facts

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

 - 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"
        },

但我正在努力解决字典取消引用字典只能在"状态"是" up"?

我使用以下版本运行:

ansible 2.3.1.0

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

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

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