Error on NetBSD-CURRENT due to new ifconfig behavior

Upcoming versions of NetBSD will print the network prefix in CIDR
notation instead of as a hex subnet mask. This currently causes Ansible
to choke on ifconfig output there. See:
http://cvsweb.netbsd.org/bsdweb.cgi/src/sbin/ifconfig/af_inet.c.diff?r1=1.22&r2=1.23&only_with_tag=MAIN&f=h

This fixes it for me. I had never even looked at Ansible code before
today, let alone debugged it, so I'm sure there are better ways.

diff --git a/lib/ansible/module_utils/facts/network/generic_bsd.py b/lib/ansible/module_utils/facts/network/generic_bsd.py
index 212c842f43..30e7f5f726 100644
--- a/lib/ansible/module_utils/facts/network/generic_bsd.py
+++ b/lib/ansible/module_utils/facts/network/generic_bsd.py
@@ -198,13 +198,18 @@ class GenericBsdIfconfigNetwork(Network):
         # inet alias 127.1.1.1 netmask 0xff000000
         if words[1] == 'alias':
             del words[1]
- address = {'address': words[1]}
+ if re.search('/', words[1]):
+ address = {'address': words[1].split('/')[0]}
+ address['netmask'] = socket.inet_ntoa(
+ struct.pack('!I', ( 1 << 32 ) - (1 << ( 32 - int(words[1].split('/')[1])))))
+ else:
+ address = {'address': words[1]}
         # deal with hex netmask
         if re.match('([0-9a-f]){8}', words[3]) and len(words[3]) == 8:
             words[3] = '0x' + words[3]
         if words[3].startswith('0x'):
             address['netmask'] = socket.inet_ntoa(struct.pack('!L', int(words[3], base=16)))
- else:
+ elif not address['netmask']:
             # otherwise assume this is a dotted quad
             address['netmask'] = words[3]
         # calculate the network

Could you get an example output from NetBSD ‘ifconfig -a’ before and after the change?

If so, I’ll add test cases and update the code.

Any idea if that change will also be coming to FreeBSD/OpenBSD ?

Adrian Likins <alikins@redhat.com> writes:

Could you get an example output from NetBSD 'ifconfig -a' before and after
the change?

If so, I'll add test cases and update the code.

On NetBSD 7.1:
laika# ifconfig -a
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33624
        inet 127.0.0.1 netmask 0xff000000
        inet6 ::1 prefixlen 128
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
re0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        capabilities=3f80<TSO4,IP4CSUM_Rx,IP4CSUM_Tx,TCP4CSUM_Rx,TCP4CSUM_Tx>
        capabilities=3f80<UDP4CSUM_Rx,UDP4CSUM_Tx>
        enabled=0
        ec_capabilities=3<VLAN_MTU,VLAN_HWTAGGING>
        ec_enabled=0
        address: 52:54:00:63:55:af
        media: Ethernet autoselect (100baseTX full-duplex)
        status: active
        inet 192.168.122.205 netmask 0xffffff00 broadcast 192.168.122.255
        inet6 fe80::5054:ff:fe63:55af%re0 prefixlen 64 scopeid 0x2

And in the same machine, NetBSD from CVS built two days ago:

laika# ifconfig -a
lo0: flags=0x8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33624
        inet 127.0.0.1/8 flags 0x0
        inet6 ::1/128 flags 0x20<NODAD>
        inet6 fe80::1%lo0/64 flags 0x0 scopeid 0x1
re0: flags=0x8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        capabilities=3f80<TSO4,IP4CSUM_Rx,IP4CSUM_Tx,TCP4CSUM_Rx,TCP4CSUM_Tx>
        capabilities=3f80<UDP4CSUM_Rx,UDP4CSUM_Tx>
        enabled=0
        ec_capabilities=3<VLAN_MTU,VLAN_HWTAGGING>
        ec_enabled=0
        address: 52:54:00:63:55:af
        media: Ethernet autoselect (100baseTX full-duplex)
        status: active
        inet 192.168.122.205/24 broadcast 192.168.122.255 flags 0x0
        inet6 fe80::5054:ff:fe63:55af%re0/64 flags 0x0 scopeid 0x2

Any idea if that change will also be coming to FreeBSD/OpenBSD ?

I remember it being discussed on the FreeBSD-current list ages ago, but
I don't think anything came of it. I'll look into this more later today.

Any idea if that change will also be coming to FreeBSD/OpenBSD ?

I remember it being discussed on the FreeBSD-current list ages ago, but
I don't think anything came of it. I'll look into this more later today.

On FreeBSD, the default is still a hex netmask. A more recent change (
https://reviews.freebsd.org/rS301059 ) gave the "-f" flag to ifconfig,
which allows the format to be adjusted (-f inet:dotted -f inet6:cidr for
example). This was introduced with FreeBSD 11, the current stable
release. This change also made it into the latest TrueOS (formerly
PC-BSD), which frequently rebases against FreeBSD-current.

OpenBSD, DragonFlyBSD, and MidnightBSD only display hex netmasks, and
I'm not aware of any plans to change those.

Thanks for the sample output. Working on test cases now.

pr with proposed fixes and some basic unit tests at
https://github.com/ansible/ansible/pull/25442