ipaddr filter: Calculating default gateway

276 views
Skip to first unread message

Udondan

unread,
May 20, 2016, 7:13:40 AM5/20/16
to Ansible Development
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

Michel blanc

unread,
May 20, 2016, 8:05:10 AM5/20/16
to ansibl...@googlegroups.com


Le 20/05/2016 à 13:13, 'Udondan' via Ansible Development a écrit :
> 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
> <https://github.com/ansible/ansible/blob/7af47a3886d5a2ebdc49f79564d84a336e10ffcb/lib/ansible/plugins/filter/ipaddr.py#L86>:
>
> |
> def_gateway_query(v):
> ifv.size >1:
> ifv.ip !=v.network:
> returnstr(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):
> ifv.size >1:
> returnstr(netaddr.IPNetwork(str(v.ip)+'/'+str(v.prefixlen))[1])
> |

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
--
Michel Blanc
{ :github => "@leucos", :twitter => "@b9m", :gpg => "0X24B35C22" }
Reply all
Reply to author
Forward
0 new messages