Repository: ansible/ansible

Issue:
Secondary ip is overwriting interfaces[iface]['ipv4'] instead of being added to interfaces[iface]["ipv4_secondaries"]
On a centos 6.5 machine running keepalived i have an issue with virtual ips added by keepalived to the eth1 interface. The virtual ip added by keepalived is incorrectly assigned as main ip when facts are discovered. But the issue should be easy to replicate by adding ip addresses with ip addr add.

This is the ip config on the machine, 192.168.255.254/32 was added by keepalived.

```
[vagrant@lb02 ~]$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:07:39:2f brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
    inet6 fe80::a00:27ff:fe07:392f/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:75:c5:ac brd ff:ff:ff:ff:ff:ff
    inet 172.16.128.2/24 brd 172.16.128.255 scope global eth1
    inet 192.168.255.254/32 scope global eth1
    inet6 fe80::a00:27ff:fe75:c5ac/64 scope link 
       valid_lft forever preferred_lft forever
```

This is the ifconfig output on the same machine:

```
[vagrant@lb02 ~]$ ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:07:39:2F  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe07:392f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:173144 errors:0 dropped:0 overruns:0 frame:0
          TX packets:89948 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:133312285 (127.1 MiB)  TX bytes:7206859 (6.8 MiB)

eth1      Link encap:Ethernet  HWaddr 08:00:27:75:C5:AC  
          inet addr:172.16.128.2  Bcast:172.16.128.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe75:c5ac/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:11116 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12333 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:667324 (651.6 KiB)  TX bytes:672218 (656.4 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:280 (280.0 b)  TX bytes:280 (280.0 b)

```

This is the output from the setup module for eth1 and all_ipv4_addresses:

```
#ansible -i hosts -m setup lb02 -a 'filter=ansible_eth1'                    
lb02 | success >> {
    "ansible_facts": {
        "ansible_eth1": {
            "active": true, 
            "device": "eth1", 
            "ipv4": {
                "address": "192.168.255.254", 
                "netmask": "255.255.255.255", 
                "network": "192.168.255.254"
            }, 
            "ipv6": [
                {
                    "address": "fe80::a00:27ff:fe75:c5ac", 
                    "prefix": "64", 
                    "scope": "link"
                }
            ], 
            "macaddress": "08:00:27:75:c5:ac", 
            "module": "e1000", 
            "mtu": 1500, 
            "promisc": false, 
            "type": "ether"
        }
    }, 
    "changed": false
}

ansible -i hosts -m setup lb02 -a 'filter=ansible_all_ipv4_addresses'      
lb02 | success >> {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "10.0.2.15", 
            "172.16.128.2", 
            "192.168.255.254"
        ]
    }, 
    "changed": false
}

```



Discussion:

Resolving patch:
diff --git a/lib/ansible/module_utils/facts.py b/lib/ansible/module_utils/facts.py
--- a/lib/ansible/module_utils/facts.py
+++ b/lib/ansible/module_utils/facts.py
@@ -1558,7 +1558,7 @@ def parse_ip_output(output, secondary=False):
                         iface = words[-1]
                         if iface != device:
                             interfaces[iface] = {}
-                        if not secondary or "ipv4" not in interfaces[iface]:
+                        if not secondary and "ipv4" not in interfaces[iface]:
                             interfaces[iface]['ipv4'] = {'address': address,
                                                          'netmask': netmask,
                                                          'network': network}


Test patch: