2017-07-15 48 views
-1

我试图找到一个交换机上的所有接口起来,通过看一些输出的nxos_facts结果:搜索Ansible调试消息字符串

​​3210

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

"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 

任何帮助深表感谢。

回答

1

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

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

谢谢阿米特,这是一个重复的问题,我终于到了底部。 map属性命令是我正在寻找的,并且在[本文]中得到了很好的回答(https://stackoverflow.com/questions/31895602/ansible-filter-a-list-by-its-attributes/31896249#31896249 ) – user1039417