From 1d697455b6cd5ad2cfd4f553a7eaa050b88ee7cb Mon Sep 17 00:00:00 2001 From: Ashwini Mhatre Date: Thu, 20 Jan 2022 23:00:14 +0530 Subject: [PATCH] Add next nth usable filter plugin (#123) Add next nth usable filter plugin SUMMARY ISSUE TYPE New Module Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Rohit Thakur Reviewed-by: None Reviewed-by: Nathaniel Case --- README.md | 1 + .../fragments/add_next_nth_usable_filter.yaml | 3 + docs/ansible.utils.next_nth_usable_filter.rst | 154 +++++++++++++++++ plugins/filter/next_nth_usable.py | 156 ++++++++++++++++++ .../tasks/next_nth_usable.yaml | 16 ++ .../plugins/filter/test_next_nth_usable.py | 34 ++++ 6 files changed, 364 insertions(+) create mode 100644 changelogs/fragments/add_next_nth_usable_filter.yaml create mode 100644 docs/ansible.utils.next_nth_usable_filter.rst create mode 100644 plugins/filter/next_nth_usable.py create mode 100644 tests/integration/targets/utils_ipaddr_filter/tasks/next_nth_usable.yaml create mode 100644 tests/unit/plugins/filter/test_next_nth_usable.py diff --git a/README.md b/README.md index 915e9d3..cf0e0aa 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.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. [ansible.utils.ipv6](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipv6_filter.rst)|To filter only Ipv6 addresses Ipv6 filter is used. [ansible.utils.ip4_hex](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ip4_hex_filter.rst)|This filter is designed to convert IPv4 address to Hexadecimal notation with optional delimiter. diff --git a/changelogs/fragments/add_next_nth_usable_filter.yaml b/changelogs/fragments/add_next_nth_usable_filter.yaml new file mode 100644 index 0000000..dc08391 --- /dev/null +++ b/changelogs/fragments/add_next_nth_usable_filter.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - Add next_nth_usable filter plugin. diff --git a/docs/ansible.utils.next_nth_usable_filter.rst b/docs/ansible.utils.next_nth_usable_filter.rst new file mode 100644 index 0000000..7d28be1 --- /dev/null +++ b/docs/ansible.utils.next_nth_usable_filter.rst @@ -0,0 +1,154 @@ +.. _ansible.utils.next_nth_usable_filter: + + +***************************** +ansible.utils.next_nth_usable +***************************** + +**This filter returns the next nth usable ip within a network described by value.** + + +Version added: 2.5.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- This filter returns the next nth usable ip within a network described by value. +- Use next_nth_usable to find the next nth usable IP address in relation to another within a range + + + + +Parameters +---------- + +.. raw:: html + + + + + + + + + + + + + + + + + + + + +
ParameterChoices/DefaultsConfigurationComments
+
+ offset + +
+ integer +
+
+ + +
index value
+
next nth usable IP address
+
+
+ value + +
+ string + / required +
+
+ + +
subnets or individual address input for next_nth_usable plugin
+
+
+ + + + +Examples +-------- + +.. code-block:: yaml + + #### examples + # Ipv4 filter plugin with different queries. + - name: next_nth_usable returns the second usable IP address for the given IP range + debug: + msg: "{{ '192.168.122.1/24' | ansible.utils.next_nth_usable(2) }}" + + - name: If there is no usable address, it returns an empty string. + debug: + msg: "{{ '192.168.122.254/24' | ansible.utils.next_nth_usable(2) }}" + + # TASK [next_nth_usable returns the second usable IP address for the given IP range] ************************** + # task path: /Users/amhatre/ansible-collections/playbooks/test_next_nth_usable.yaml:9 + # Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils + # ok: [localhost] => { + # "msg": "192.168.122.3" + # } + # + # TASK [If there is no usable address, it returns an empty string.] ******************************************* + # task path: /Users/amhatre/ansible-collections/playbooks/test_next_nth_usable.yaml:14 + # Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils + # ok: [localhost] => { + # "msg": "" + # } + + + +Return Values +------------- +Common return values are documented `here `_, the following are the fields unique to this filter: + +.. raw:: html + + + + + + + + + + + + +
KeyReturnedDescription
+
+ data + +
+ list + / elements=string +
+
+
Returns list with values valid for a particular query.
+
+
+

+ + +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/next_nth_usable.py b/plugins/filter/next_nth_usable.py new file mode 100644 index 0000000..baaa58d --- /dev/null +++ b/plugins/filter/next_nth_usable.py @@ -0,0 +1,156 @@ +# -*- 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: ipv4 +""" +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, + _first_last, +) +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: next_nth_usable + author: Ashwini Mhatre (@amhatre) + version_added: "2.5.0" + short_description: This filter returns the next nth usable ip within a network described by value. + description: + - This filter returns the next nth usable ip within a network described by value. + - Use next_nth_usable to find the next nth usable IP address in relation to another within a range + options: + value: + description: + - subnets or individual address input for next_nth_usable plugin + type: str + required: True + offset: + description: + - index value + - next nth usable IP address + type: int + notes: +""" + +EXAMPLES = r""" +#### examples +# Ipv4 filter plugin with different queries. +- name: next_nth_usable returns the second usable IP address for the given IP range + debug: + msg: "{{ '192.168.122.1/24' | ansible.utils.next_nth_usable(2) }}" + +- name: If there is no usable address, it returns an empty string. + debug: + msg: "{{ '192.168.122.254/24' | ansible.utils.next_nth_usable(2) }}" + +# TASK [next_nth_usable returns the second usable IP address for the given IP range] ************************** +# task path: /Users/amhatre/ansible-collections/playbooks/test_next_nth_usable.yaml:9 +# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils +# ok: [localhost] => { +# "msg": "192.168.122.3" +# } +# +# TASK [If there is no usable address, it returns an empty string.] ******************************************* +# task path: /Users/amhatre/ansible-collections/playbooks/test_next_nth_usable.yaml:14 +# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils +# ok: [localhost] => { +# "msg": "" +# } + +""" + +RETURN = """ + data: + type: list + elements: str + description: + - Returns list with values valid for a particular query. +""" + + +@pass_environment +def _next_nth_usable(*args, **kwargs): + """This filter returns the next nth usable ip within a network described by value.""" + keys = ["value", "offset"] + data = dict(zip(keys, args[1:])) + data.update(kwargs) + aav = AnsibleArgSpecValidator( + data=data, schema=DOCUMENTATION, name="next_nth_usable" + ) + valid, errors, updated_data = aav.validate() + if not valid: + raise AnsibleFilterError(errors) + return next_nth_usable(**updated_data) + + +def next_nth_usable(value, offset): + """ + Returns the next nth usable ip within a network described by value. + """ + try: + vtype = ipaddr(value, "type") + if vtype == "address": + v = ipaddr(value, "cidr") + elif vtype == "network": + v = ipaddr(value, "subnet") + + v = netaddr.IPNetwork(v) + except Exception: + return False + + if type(offset) != int: + raise AnsibleFilterError("Must pass in an integer") + if v.size > 1: + first_usable, last_usable = _first_last(v) + nth_ip = int(netaddr.IPAddress(int(v.ip) + offset)) + if first_usable <= nth_ip <= last_usable: + return str(netaddr.IPAddress(int(v.ip) + offset)) + + +class FilterModule(object): + """IP address and network manipulation filters + """ + + filter_map = { + # IP addresses and networks + "next_nth_usable": _next_nth_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/next_nth_usable.yaml b/tests/integration/targets/utils_ipaddr_filter/tasks/next_nth_usable.yaml new file mode 100644 index 0000000..ad15b70 --- /dev/null +++ b/tests/integration/targets/utils_ipaddr_filter/tasks/next_nth_usable.yaml @@ -0,0 +1,16 @@ +--- +- name: next_nth_usable filter + ansible.builtin.set_fact: + result1: "{{ '192.168.122.1/24'|ansible.utils.next_nth_usable(2) }}" + +- name: Assert result for next_nth_usable. + assert: + that: "{{ result1 == '192.168.122.3' }}" + +- name: next_nth_usable filter + ansible.builtin.set_fact: + result1: "{{ '192.168.122.254/24'|ansible.utils.next_nth_usable(2) }}" + +- name: Assert result for ipv4. + assert: + that: "{{ result1 == '' }}" diff --git a/tests/unit/plugins/filter/test_next_nth_usable.py b/tests/unit/plugins/filter/test_next_nth_usable.py new file mode 100644 index 0000000..e701bc2 --- /dev/null +++ b/tests/unit/plugins/filter/test_next_nth_usable.py @@ -0,0 +1,34 @@ +# -*- 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 ipwrap filter plugin +""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import unittest +from ansible_collections.ansible.utils.plugins.filter.next_nth_usable import ( + _next_nth_usable, +) + + +class Test_Next_Nth_Usable(unittest.TestCase): + def setUp(self): + pass + + def test_next_nth_usable_filter(self): + """next_nth_usable filter""" + args = ["", "192.168.122.1/24", 2] + result = _next_nth_usable(*args) + self.assertEqual(result, "192.168.122.3") + + def test_next_nth_usable_with_empty_return_string(self): + """Check ipv4 to ipv6 conversion""" + args = ["", "192.168.122.254/24", 2] + result = _next_nth_usable(*args) + self.assertEqual(result, None)