ipaddr filter: Calculating default gateway

I wanted to calculate the default gateway from a CIDR. The ipaddr filter has an undocumented gateway/router option but it results in unexpected values.

Here is the responsible method:

def _gateway_query(v): if v.size > 1: if v.ip != v.network: return str(v.ip) + '/' + str(v.prefixlen)

How is this a gateway? It basically returns the given CIDR.

Since this is not documented it’s hard to guess what was the intention behind this.

I’d like to send a pull request to change this but first wanted ask what you think about it. My proposal would be to change the method to:

def _gateway_query(v): if v.size > 1: return str(netaddr.IPNetwork(str(v.ip) + '/' + str(v.prefixlen))[1])

I know the gateway could be anything and does not necessarily have to be the first IP of the network range. But still this more or less the default. What do you think?

Greetings,

Daniel

Hi Daniel,

I am not sure replacing something wrong by something equally wrong is
the proper way to do it.

If you can not use ansible_default_ipv4.gateway because you are working
on something that is not covered by gather_facts, then you really should
provide the gateway explicitly.

M