diff --git a/README.md b/README.md index dcea147..5b9271b 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Name | Description [ansible.utils.from_xml](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.from_xml_filter.rst)|Convert given XML string to native python dictionary. [ansible.utils.get_path](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.get_path_filter.rst)|Retrieve the value in a variable using a path [ansible.utils.index_of](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.index_of_filter.rst)|Find the indices of items in a list matching some criteria +[ansible.utils.network_in_usable](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.network_in_usable_filter.rst)|The network_in_usable filter returns whether an address passed as an argument is usable in a network. [ansible.utils.network_in_network](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.network_in_network_filter.rst)|This filter returns whether an address or a network passed as argument is in a network. [ansible.utils.next_nth_usable](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.next_nth_usable_filter.rst)|This filter returns the next nth usable ip within a network described by value. [ansible.utils.ipsubnet](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipsubnet_filter.rst)|This filter can be used to manipulate network subnets in several ways. diff --git a/changelogs/fragments/add_network_in_usable_filter.yaml b/changelogs/fragments/add_network_in_usable_filter.yaml new file mode 100644 index 0000000..5ca270d --- /dev/null +++ b/changelogs/fragments/add_network_in_usable_filter.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - Add network_in_usable filter plugin. diff --git a/docs/ansible.utils.network_in_usable_filter.rst b/docs/ansible.utils.network_in_usable_filter.rst new file mode 100644 index 0000000..9844f07 --- /dev/null +++ b/docs/ansible.utils.network_in_usable_filter.rst @@ -0,0 +1,162 @@ +.. _ansible.utils.network_in_usable_filter: + + +******************************* +ansible.utils.network_in_usable +******************************* + +**The network_in_usable filter returns whether an address passed as an argument is usable in a network.** + + +Version added: 2.5.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- The network_in_usable filter returns whether an address passed as an argument is usable in a network Usable addresses are addresses that can be assigned to a host. +- The network ID and the broadcast address are not usable addresses. + + + + +Parameters +---------- + +.. raw:: html + + + + + + + + + + + + + + + + + + + + +
ParameterChoices/DefaultsConfigurationComments
+
+ test + +
+ string +
+
+ + +
The address or network is usable or not.
+
+
+ value + +
+ string + / required +
+
+ + +
The network address or range to test against.
+
+
+ + + + +Examples +-------- + +.. code-block:: yaml + + #### examples + - name: Check ip address is usable in a network + debug: + msg: "{{ '192.168.0.0/24' | ansible.utils.network_in_usable( '192.168.0.1' ) }}" + + - name: Check broadcast address is usable in a network + debug: + msg: "{{ '192.168.0.0/24' | ansible.utils.network_in_usable( '192.168.0.255' ) }}" + + - name: Check in a network is part of another network. + debug: + msg: "{{ '192.168.0.0/16' | ansible.utils.network_in_usable( '192.168.0.255' ) }}" + + # TASK [Check ip address is usable in a network] ************************************************************** + # task path: /Users/amhatre/ansible-collections/playbooks/test_network_in_usable.yaml:7 + # Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils + # ok: [localhost] => { + # "msg": true + # } + # + # TASK [Check broadcast address is usable in a network] ******************************************************* + # task path: /Users/amhatre/ansible-collections/playbooks/test_network_in_usable.yaml:11 + # Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils + # ok: [localhost] => { + # "msg": false + # } + # + # TASK [Check in a network is part of another network.] ******************************************************* + # task path: /Users/amhatre/ansible-collections/playbooks/test_network_in_usable.yaml:15 + # Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils + # ok: [localhost] => { + # "msg": true + # } + + + +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/network_in_usable.py b/plugins/filter/network_in_usable.py new file mode 100644 index 0000000..8271648 --- /dev/null +++ b/plugins/filter/network_in_usable.py @@ -0,0 +1,167 @@ +# -*- 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: network_in_usable +""" +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: network_in_usable + author: Ashwini Mhatre (@amhatre) + version_added: "2.5.0" + short_description: The network_in_usable filter returns whether an address passed as an argument is usable in a network. + description: + - The network_in_usable filter returns whether an address passed as an argument is usable in a network + Usable addresses are addresses that can be assigned to a host. + - The network ID and the broadcast address are not usable addresses. + options: + value: + description: The network address or range to test against. + type: str + required: True + test: + description: The address or network is usable or not. + type: str + notes: +""" + +EXAMPLES = r""" +#### examples +- name: Check ip address is usable in a network + debug: + msg: "{{ '192.168.0.0/24' | ansible.utils.network_in_usable( '192.168.0.1' ) }}" + +- name: Check broadcast address is usable in a network + debug: + msg: "{{ '192.168.0.0/24' | ansible.utils.network_in_usable( '192.168.0.255' ) }}" + +- name: Check in a network is part of another network. + debug: + msg: "{{ '192.168.0.0/16' | ansible.utils.network_in_usable( '192.168.0.255' ) }}" + +# TASK [Check ip address is usable in a network] ************************************************************** +# task path: /Users/amhatre/ansible-collections/playbooks/test_network_in_usable.yaml:7 +# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils +# ok: [localhost] => { +# "msg": true +# } +# +# TASK [Check broadcast address is usable in a network] ******************************************************* +# task path: /Users/amhatre/ansible-collections/playbooks/test_network_in_usable.yaml:11 +# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils +# ok: [localhost] => { +# "msg": false +# } +# +# TASK [Check in a network is part of another network.] ******************************************************* +# task path: /Users/amhatre/ansible-collections/playbooks/test_network_in_usable.yaml:15 +# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils +# ok: [localhost] => { +# "msg": true +# } + + +""" + +RETURN = """ + data: + type: bool + description: + - Returns whether an address or a network passed as argument is in a network. + +""" + + +@pass_environment +def _network_in_usable(*args, **kwargs): + """This filter returns whether an address or a network passed as argument is in a network.""" + keys = ["value", "test"] + data = dict(zip(keys, args[1:])) + data.update(kwargs) + aav = AnsibleArgSpecValidator( + data=data, schema=DOCUMENTATION, name="network_in_usable" + ) + valid, errors, updated_data = aav.validate() + if not valid: + raise AnsibleFilterError(errors) + return network_in_usable(**updated_data) + + +def network_in_usable(value, test): + """ + Checks whether 'test' is a usable address or addresses in 'value' + :param: value: The string representation of an address or network to test against. + :param test: The string representation of an address or network to validate if it is within the range of 'value'. + :return: bool + """ + # normalize value and test variables into an ipaddr + v = _address_normalizer(value) + w = _address_normalizer(test) + + # get first and last addresses as integers to compare value and test; or cathes value when case is /32 + v_first = ipaddr(ipaddr(v, "first_usable") or ipaddr(v, "address"), "int") + v_last = ipaddr(ipaddr(v, "last_usable") or ipaddr(v, "address"), "int") + w_first = ipaddr(ipaddr(w, "network") or ipaddr(w, "address"), "int") + w_last = ipaddr(ipaddr(w, "broadcast") or ipaddr(w, "address"), "int") + + if _range_checker(w_first, v_first, v_last) and _range_checker( + w_last, v_first, v_last + ): + return True + else: + return False + + +class FilterModule(object): + """IP address and network manipulation filters + """ + + filter_map = { + # IP addresses and networks + "network_in_usable": _network_in_usable + } + + 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/network_in_usable.yaml b/tests/integration/targets/utils_ipaddr_filter/tasks/network_in_usable.yaml new file mode 100644 index 0000000..46015bc --- /dev/null +++ b/tests/integration/targets/utils_ipaddr_filter/tasks/network_in_usable.yaml @@ -0,0 +1,25 @@ +--- +- name: network_in_usable filter test1 + ansible.builtin.set_fact: + result1: "{{ '192.168.0.0/24'|ansible.utils.network_in_usable('192.168.0.1') }}" + +- name: Assert result for network_in_usable. + assert: + that: "{{ result1 == True }}" + +- name: network_in_usable filter test2 + ansible.builtin.set_fact: + result1: "{{ '192.168.0.0/24'|ansible.utils.network_in_usable('192.168.0.255') }}" + +- name: Assert result for network_in_usable + assert: + that: "{{ result1 == False }}" + + +- name: network_in_usable filter test3 + ansible.builtin.set_fact: + result1: "{{ '192.168.0.0/16'|ansible.utils.network_in_usable('192.168.0.255') }}" + +- name: Assert result for network_in_usable + assert: + that: "{{ result1 == True }}" diff --git a/tests/unit/plugins/filter/test_network_in_usable.py b/tests/unit/plugins/filter/test_network_in_usable.py new file mode 100644 index 0000000..246bf78 --- /dev/null +++ b/tests/unit/plugins/filter/test_network_in_usable.py @@ -0,0 +1,40 @@ +# -*- 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 network_in_usable filter plugin +""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import unittest +from ansible_collections.ansible.utils.plugins.filter.network_in_usable import ( + _network_in_usable, +) + + +class Test_Network_In_Usable(unittest.TestCase): + def setUp(self): + pass + + def test_network_in_usable_filter_1(self): + """network_in_usable filter""" + args = ["", "192.168.0.0/24", "192.168.0.1"] + result = _network_in_usable(*args) + self.assertEqual(result, True) + + def test_network_in_usable_filter_2(self): + """network_in_usable filter""" + args = ["", "192.168.0.0/24", "192.168.0.255"] + result = _network_in_usable(*args) + self.assertEqual(result, False) + + def test_network_in_usable_filter_3(self): + """network_in_usable filter""" + args = ["", "192.168.0.0/16", "192.168.0.255"] + result = _network_in_usable(*args) + self.assertEqual(result, True)