使用ansible获取所有Mac地址的列表

时间:2017-04-24 09:43:09

标签: ansible

我知道模块设置为每个接口提供Mac地址,例如:

    "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,用分号分隔。如果我不知道服务器有多少接口而且我不知道他们的名字,我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

请查看此answer以获取完整说明。

你可以试试这个:

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

此表达式未经过测试,但该想法应该明确。