diff --git a/README.md b/README.md index 113081e..cc0ec95 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/changelogs/fragments/add_reduce_on_network_filter.yaml b/changelogs/fragments/add_reduce_on_network_filter.yaml new file mode 100644 index 0000000..ed2643b --- /dev/null +++ b/changelogs/fragments/add_reduce_on_network_filter.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - Add reduce_on_network filter plugin. diff --git a/docs/ansible.utils.reduce_on_network_filter.rst b/docs/ansible.utils.reduce_on_network_filter.rst new file mode 100644 index 0000000..82beb13 --- /dev/null +++ b/docs/ansible.utils.reduce_on_network_filter.rst @@ -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 + + + + + + + + + + + + + + + + + + + + +
ParameterChoices/DefaultsConfigurationComments
+
+ network + +
+ string +
+
+ + +
The network to validate against.
+
+
+ value + +
+ list + / elements=string + / required +
+
+ + +
the list of addresses to filter on.
+
+
+ + + + +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 `_, the following are the fields unique to this filter: + +.. raw:: html + + + + + + + + + + + + +
KeyReturnedDescription
+
+ data + +
+ boolean +
+
+
Returns whether an address or a network passed as argument is in a network.
+
+
+

+ + +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. diff --git a/plugins/filter/reduce_on_network.py b/plugins/filter/reduce_on_network.py new file mode 100644 index 0000000..6cbdb9e --- /dev/null +++ b/plugins/filter/reduce_on_network.py @@ -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 + ) diff --git a/tests/integration/targets/utils_ipaddr_filter/tasks/reduce_on_network.yaml b/tests/integration/targets/utils_ipaddr_filter/tasks/reduce_on_network.yaml new file mode 100644 index 0000000..db41dd1 --- /dev/null +++ b/tests/integration/targets/utils_ipaddr_filter/tasks/reduce_on_network.yaml @@ -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'] }}" diff --git a/tests/unit/plugins/filter/test_reduce_on_network.py b/tests/unit/plugins/filter/test_reduce_on_network.py new file mode 100644 index 0000000..d7cfe15 --- /dev/null +++ b/tests/unit/plugins/filter/test_reduce_on_network.py @@ -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"])