mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 05:10:22 -07:00
Implement the reduce_on_network method to filter a list of IP addresses on a given range. (#34929) (#34930)
This commit is contained in:
parent
a1639abd71
commit
30ed29ed46
1 changed files with 34 additions and 0 deletions
|
@ -900,6 +900,39 @@ def network_in_network(value, test):
|
|||
return False
|
||||
|
||||
|
||||
def reduce_on_network(value, network):
|
||||
'''
|
||||
Reduces a list of addresses to only the addresses that match a given network.
|
||||
|
||||
:param: value: The list of addresses to filter on.
|
||||
:param: network: The network to validate against.
|
||||
|
||||
:return: The reduced list of addresses.
|
||||
'''
|
||||
# normalize network variable into an ipaddr
|
||||
n = _address_normalizer(network)
|
||||
|
||||
# get first and last addresses as integers to compare value and test; or cathes value when case is /32
|
||||
n_first = ipaddr(ipaddr(n, 'network') or ipaddr(n, 'address'), 'int')
|
||||
n_last = ipaddr(ipaddr(n, 'broadcast') or ipaddr(n, 'address'), 'int')
|
||||
|
||||
# create an empty list to fill and return
|
||||
r = []
|
||||
|
||||
for address in value:
|
||||
# normalize address variables into an ipaddr
|
||||
a = _address_normalizer(address)
|
||||
|
||||
# get first and last addresses as integers to compare value and test; or cathes value when case is /32
|
||||
a_first = ipaddr(ipaddr(a, 'network') or ipaddr(a, 'address'), 'int')
|
||||
a_last = ipaddr(ipaddr(a, 'broadcast') or ipaddr(a, 'address'), 'int')
|
||||
|
||||
if _range_checker(a_first, n_first, n_last) and _range_checker(a_last, n_first, n_last):
|
||||
r.append(address)
|
||||
|
||||
return r
|
||||
|
||||
|
||||
# Returns the SLAAC address within a network for a given HW/MAC address.
|
||||
# Usage:
|
||||
#
|
||||
|
@ -1002,6 +1035,7 @@ class FilterModule(object):
|
|||
'next_nth_usable': next_nth_usable,
|
||||
'network_in_network': network_in_network,
|
||||
'network_in_usable': network_in_usable,
|
||||
'reduce_on_network': reduce_on_network,
|
||||
'nthhost': nthhost,
|
||||
'previous_nth_usable': previous_nth_usable,
|
||||
'slaac': slaac,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue