2017-04-24 47 views
0

我知道模块的设置提供了每个接口的MAC地址的所有MAC地址ansible的列表,例如:获取使用

"ansible_eth0": {                                                              
     "active": true,                                                              
     "device": "eth0",                                                                                                    
     "ipv4": {                                                               
      "address": "192.168.35.174",                                                         
      "broadcast": "192.168.35.255",                                                         
      "netmask": "255.255.255.0",                                                          
      "network": "192.168.35.0"                                                          
     },                                                                 
     "ipv6": [                                                               
      {                                                                
       "address": "fe80::250:56ff:fe91:a6c2",                                                      
       "prefix": "64",                                                            
       "scope": "link"                                                            
      }                                                                
     ],                                                                 
     "macaddress": "00:50:56:91:a6:c2",                                                         
     "module": "vmxnet3",                                                            
     "mtu": 1500,                                                              
     "pciid": "0000:0b:00.0",                                                           
     "promisc": false,                                                             
     "speed": 10000,                                                              
     "type": "ether" 

假设服务器有10个接口,我要收集他们所有的Mac的,与分离分号。如果我不知道服务器有多少个接口并且我不知道他们的名字,我该怎么做?

回答

0

查看此answer的完整描述。

你可以试试这个:

ansible_interfaces | 
    map('regex_replace','^','ansible_') | 
    map('extract',hostvars[inventory_hostname]) | 
    selectattr('macaddress','defined') | 
    map(attribute='macaddress') | 
    list 

这种表达是没有测试,但这个想法应该是清楚的。

+0

当接口包含下划线时(也许即使有短划线)它会中断,任何想法为什么? '错误是:KeyError:u'ansible_docker_gwbridge'' – Petr

+0

这是完全不同的问题。看到这[问题](https://github.com/ansible/ansible/issues/23577)。你在什么版本上? –