Add plugin Reduce_on_network (#127)
Add plugin Reduce_on_network SUMMARY ISSUE TYPE New Module Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Nathaniel Case <this.is@nathanielca.se> Reviewed-by: Nilashish Chakraborty <nilashishchakraborty8@gmail.com> Reviewed-by: None <None>pull/126/head^2
parent
234b0a51cf
commit
315200166f
|
@ -34,6 +34,7 @@ Name | Description
|
|||
[ansible.utils.nthhost](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.nthhost_filter.rst)|This filter returns the nth host within a network described by value.
|
||||
[ansible.utils.ipv4](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipv4_filter.rst)|To filter only Ipv4 addresses Ipv4 filter is used.
|
||||
[ansible.utils.param_list_compare](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.param_list_compare_filter.rst)|Generate the final param list combining/comparing base and provided parameters.
|
||||
[ansible.utils.reduce_on_network](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.reduce_on_network_filter.rst)|This filter reduces a list of addresses to only the addresses that match a given network.
|
||||
[ansible.utils.to_paths](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.to_paths_filter.rst)|Flatten a complex object into a dictionary of paths and values
|
||||
[ansible.utils.to_xml](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.to_xml_filter.rst)|Convert given JSON string to XML
|
||||
[ansible.utils.usable_range](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.usable_range_filter.rst)|Expand the usable IP addresses
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
minor_changes:
|
||||
- Add reduce_on_network filter plugin.
|
|
@ -0,0 +1,143 @@
|
|||
.. _ansible.utils.reduce_on_network_filter:
|
||||
|
||||
|
||||
*******************************
|
||||
ansible.utils.reduce_on_network
|
||||
*******************************
|
||||
|
||||
**This filter reduces a list of addresses to only the addresses that match a given network.**
|
||||
|
||||
|
||||
Version added: 2.5.0
|
||||
|
||||
.. contents::
|
||||
:local:
|
||||
:depth: 1
|
||||
|
||||
|
||||
Synopsis
|
||||
--------
|
||||
- This filter reduces a list of addresses to only the addresses that match a given network.
|
||||
- To check whether multiple addresses belong to a network, use the reduce_on_network filter.
|
||||
|
||||
|
||||
|
||||
|
||||
Parameters
|
||||
----------
|
||||
|
||||
.. raw:: html
|
||||
|
||||
<table border=0 cellpadding=0 class="documentation-table">
|
||||
<tr>
|
||||
<th colspan="1">Parameter</th>
|
||||
<th>Choices/<font color="blue">Defaults</font></th>
|
||||
<th>Configuration</th>
|
||||
<th width="100%">Comments</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="1">
|
||||
<div class="ansibleOptionAnchor" id="parameter-"></div>
|
||||
<b>network</b>
|
||||
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
|
||||
<div style="font-size: small">
|
||||
<span style="color: purple">string</span>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
<div>The network to validate against.</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="1">
|
||||
<div class="ansibleOptionAnchor" id="parameter-"></div>
|
||||
<b>value</b>
|
||||
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
|
||||
<div style="font-size: small">
|
||||
<span style="color: purple">list</span>
|
||||
/ <span style="color: purple">elements=string</span>
|
||||
/ <span style="color: red">required</span>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
<div>the list of addresses to filter on.</div>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<br/>
|
||||
|
||||
|
||||
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
#### examples
|
||||
- name: To check whether multiple addresses belong to a network, use the reduce_on_network filter.
|
||||
debug:
|
||||
msg: "{{ ['192.168.0.34', '10.3.0.3', '192.168.2.34'] | ansible.utils.reduce_on_network( '192.168.0.0/24' ) }}"
|
||||
|
||||
# TASK [To check whether multiple addresses belong to a network, use the reduce_on_network filter.] ***********
|
||||
# task path: /Users/amhatre/ansible-collections/playbooks/test_reduce_on_network.yaml:7
|
||||
# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
|
||||
# ok: [localhost] => {
|
||||
# "msg": [
|
||||
# "192.168.0.34"
|
||||
# ]
|
||||
# }
|
||||
|
||||
|
||||
|
||||
Return Values
|
||||
-------------
|
||||
Common return values are documented `here <https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#common-return-values>`_, the following are the fields unique to this filter:
|
||||
|
||||
.. raw:: html
|
||||
|
||||
<table border=0 cellpadding=0 class="documentation-table">
|
||||
<tr>
|
||||
<th colspan="1">Key</th>
|
||||
<th>Returned</th>
|
||||
<th width="100%">Description</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="1">
|
||||
<div class="ansibleOptionAnchor" id="return-"></div>
|
||||
<b>data</b>
|
||||
<a class="ansibleOptionLink" href="#return-" title="Permalink to this return value"></a>
|
||||
<div style="font-size: small">
|
||||
<span style="color: purple">boolean</span>
|
||||
</div>
|
||||
</td>
|
||||
<td></td>
|
||||
<td>
|
||||
<div>Returns whether an address or a network passed as argument is in a network.</div>
|
||||
<br/>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<br/><br/>
|
||||
|
||||
|
||||
Status
|
||||
------
|
||||
|
||||
|
||||
Authors
|
||||
~~~~~~~
|
||||
|
||||
- Ashwini Mhatre (@amhatre)
|
||||
|
||||
|
||||
.. hint::
|
||||
Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.
|
|
@ -0,0 +1,155 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2021 Red Hat
|
||||
# GNU General Public License v3.0+
|
||||
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
"""
|
||||
filter plugin file for ipaddr filters: reduce_on_network
|
||||
"""
|
||||
from __future__ import absolute_import, division, print_function
|
||||
from functools import partial
|
||||
from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddr_utils import (
|
||||
ipaddr,
|
||||
_need_netaddr,
|
||||
_address_normalizer,
|
||||
_range_checker,
|
||||
)
|
||||
from ansible.errors import AnsibleFilterError
|
||||
from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import (
|
||||
AnsibleArgSpecValidator,
|
||||
)
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
try:
|
||||
from jinja2.filters import pass_environment
|
||||
except ImportError:
|
||||
from jinja2.filters import environmentfilter as pass_environment
|
||||
|
||||
try:
|
||||
import netaddr
|
||||
|
||||
HAS_NETADDR = True
|
||||
except ImportError:
|
||||
# in this case, we'll make the filters return error messages (see bottom)
|
||||
HAS_NETADDR = False
|
||||
else:
|
||||
|
||||
class mac_linux(netaddr.mac_unix):
|
||||
pass
|
||||
|
||||
mac_linux.word_fmt = "%.2x"
|
||||
|
||||
DOCUMENTATION = """
|
||||
name: reduce_on_network
|
||||
author: Ashwini Mhatre (@amhatre)
|
||||
version_added: "2.5.0"
|
||||
short_description: This filter reduces a list of addresses to only the addresses that match a given network.
|
||||
description:
|
||||
- This filter reduces a list of addresses to only the addresses that match a given network.
|
||||
- To check whether multiple addresses belong to a network, use the reduce_on_network filter.
|
||||
options:
|
||||
value:
|
||||
description: the list of addresses to filter on.
|
||||
type: list
|
||||
elements: str
|
||||
required: True
|
||||
network:
|
||||
description: The network to validate against.
|
||||
type: str
|
||||
notes:
|
||||
"""
|
||||
|
||||
EXAMPLES = r"""
|
||||
#### examples
|
||||
- name: To check whether multiple addresses belong to a network, use the reduce_on_network filter.
|
||||
debug:
|
||||
msg: "{{ ['192.168.0.34', '10.3.0.3', '192.168.2.34'] | ansible.utils.reduce_on_network( '192.168.0.0/24' ) }}"
|
||||
|
||||
# TASK [To check whether multiple addresses belong to a network, use the reduce_on_network filter.] ***********
|
||||
# task path: /Users/amhatre/ansible-collections/playbooks/test_reduce_on_network.yaml:7
|
||||
# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
|
||||
# ok: [localhost] => {
|
||||
# "msg": [
|
||||
# "192.168.0.34"
|
||||
# ]
|
||||
# }
|
||||
|
||||
|
||||
"""
|
||||
|
||||
RETURN = """
|
||||
data:
|
||||
type: bool
|
||||
description:
|
||||
- Returns whether an address or a network passed as argument is in a network.
|
||||
|
||||
"""
|
||||
|
||||
|
||||
@pass_environment
|
||||
def _reduce_on_network(*args, **kwargs):
|
||||
"""This filter returns whether an address or a network passed as argument is in a network."""
|
||||
keys = ["value", "network"]
|
||||
data = dict(zip(keys, args[1:]))
|
||||
data.update(kwargs)
|
||||
aav = AnsibleArgSpecValidator(
|
||||
data=data, schema=DOCUMENTATION, name="reduce_on_network"
|
||||
)
|
||||
valid, errors, updated_data = aav.validate()
|
||||
if not valid:
|
||||
raise AnsibleFilterError(errors)
|
||||
return reduce_on_network(**updated_data)
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
class FilterModule(object):
|
||||
"""IP address and network manipulation filters
|
||||
"""
|
||||
|
||||
filter_map = {
|
||||
# IP addresses and networks
|
||||
"reduce_on_network": _reduce_on_network
|
||||
}
|
||||
|
||||
def filters(self):
|
||||
""" ipaddr filter """
|
||||
if HAS_NETADDR:
|
||||
return self.filter_map
|
||||
else:
|
||||
return dict(
|
||||
(f, partial(_need_netaddr, f)) for f in self.filter_map
|
||||
)
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
- name: reduce_on_network filter test1
|
||||
ansible.builtin.set_fact:
|
||||
result1: "{{ ['192.168.0.34', '10.3.0.3', '192.168.2.34'] |ansible.utils.reduce_on_network('192.168.0.0/24') }}"
|
||||
|
||||
- name: Assert result for reduce_on_network.
|
||||
assert:
|
||||
that: "{{ result1 == ['192.168.0.34'] }}"
|
|
@ -0,0 +1,42 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2021 Red Hat
|
||||
# GNU General Public License v3.0+
|
||||
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
"""
|
||||
Unit test file for reduce_on_network filter plugin
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import unittest
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible_collections.ansible.utils.plugins.filter.reduce_on_network import (
|
||||
_reduce_on_network,
|
||||
)
|
||||
|
||||
|
||||
class Test_reduce_on_network(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_invalid_data(self):
|
||||
"""Check passing invalid argspec"""
|
||||
|
||||
# missing required arguments
|
||||
args = [""]
|
||||
kwargs = {}
|
||||
with self.assertRaises(AnsibleError) as error:
|
||||
_reduce_on_network(*args, **kwargs)
|
||||
self.assertIn(
|
||||
"missing required arguments: value", str(error.exception)
|
||||
)
|
||||
|
||||
def test_reduce_on_network_filter_1(self):
|
||||
"""reduce_on_network filter"""
|
||||
list1 = ["192.168.0.34", "10.3.0.3", "192.168.2.34"]
|
||||
args = ["", list1, "192.168.0.0/24"]
|
||||
result = _reduce_on_network(*args)
|
||||
self.assertEqual(result, ["192.168.0.34"])
|
Loading…
Reference in New Issue