2

I know that module setup provides Mac addresses per interface, for example:

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

Suppose the server has 10 interfaces and I want to gather all their mac's, separated with semicolon. How would I do that if I don't know how many interfaces server has and I don't know their names?

Petr
  • 13,747
  • 20
  • 89
  • 144

1 Answers1

1

Take a look at this answer for complete description.

You may try this:

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

This expression is not tested, but the idea should be clear.

Community
  • 1
  • 1
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • It breaks when interface contains underscore (and maybe even if there is a dash), any idea why? `The error was: KeyError: u'ansible_docker_gwbridge'` – Petr Nov 28 '17 at 14:55
  • This is different issue altogether. See this [issue](https://github.com/ansible/ansible/issues/23577). What version are you on? – Konstantin Suvorov Nov 28 '17 at 15:16