From c04c648b7ae858029463700d0f40af1c65d5ef3a Mon Sep 17 00:00:00 2001 From: Ashwini Mhatre Date: Thu, 20 Jan 2022 18:40:31 +0530 Subject: [PATCH] Add IPV6 filter plugin (#121) Add IPV6 filter plugin SUMMARY ISSUE TYPE New Module Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Nathaniel Case Reviewed-by: None --- README.md | 1 + .../fragments/add_ipv6_filter_plugin.yaml | 3 + docs/ansible.utils.ipv6_filter.rst | 200 ++++++++++++++++++ plugins/filter/ipv6.py | 179 ++++++++++++++++ .../utils_ipaddr_filter/tasks/ipv6.yaml | 35 +++ .../utils_ipaddr_filter/vars/main.yaml | 14 ++ tests/unit/plugins/filter/test_ipv6.py | 60 ++++++ 7 files changed, 492 insertions(+) create mode 100644 changelogs/fragments/add_ipv6_filter_plugin.yaml create mode 100644 docs/ansible.utils.ipv6_filter.rst create mode 100644 plugins/filter/ipv6.py create mode 100644 tests/integration/targets/utils_ipaddr_filter/tasks/ipv6.yaml create mode 100644 tests/unit/plugins/filter/test_ipv6.py diff --git a/README.md b/README.md index 3280ae1..49ce865 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.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. [ansible.utils.ipmath](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipmath_filter.rst)|This filter is designed to do simple IP math/arithmetic. [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. diff --git a/changelogs/fragments/add_ipv6_filter_plugin.yaml b/changelogs/fragments/add_ipv6_filter_plugin.yaml new file mode 100644 index 0000000..6cb0cca --- /dev/null +++ b/changelogs/fragments/add_ipv6_filter_plugin.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - Add ipv6 filter plugin. diff --git a/docs/ansible.utils.ipv6_filter.rst b/docs/ansible.utils.ipv6_filter.rst new file mode 100644 index 0000000..eda4835 --- /dev/null +++ b/docs/ansible.utils.ipv6_filter.rst @@ -0,0 +1,200 @@ +.. _ansible.utils.ipv6_filter: + + +****************** +ansible.utils.ipv6 +****************** + +**To filter only Ipv6 addresses Ipv6 filter is used.** + + +Version added: 2.5.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- Sometimes you need only IPv6 addresses. To filter only Ipv6 addresses Ipv6 filter is used. + + + + +Parameters +---------- + +.. raw:: html + + + + + + + + + + + + + + + + + + + + +
ParameterChoices/DefaultsConfigurationComments
+
+ query + +
+ string +
+
+ Default:
""
+
+ +
You can provide a single argument to each ipv6() filter.
+
Example. query type 'ipv4' to convert ipv6 into ipv4
+
+
+ value + +
+ list + / elements=string + / required +
+
+ + +
list of subnets or individual address or any other values input for ipv6 plugin
+
+
+ + + + +Examples +-------- + +.. code-block:: yaml + + #### examples + # Ipv6 filter plugin with different queries. + - name: Set value as input list + ansible.builtin.set_fact: + value: + - 192.24.2.1 + - ::ffff:192.168.32.0/120 + - '' + - ::ffff:192.24.2.1/128 + - 192.168.32.0/24 + - fe80::100/10 + - True + - name: IPv6 filter to filter Ipv6 Address + debug: + msg: "{{ value|ansible.utils.ipv6 }}" + + - name: convert IPv6 addresses into IPv4 addresses. + debug: + msg: "{{ value|ansible.utils.ipv6('ipv4') }}" + + - name: filter only IPv6 addresses. + debug: + msg: "{{ value|ansible.utils.ipv6('address') }}" + + + # PLAY [Ipv6 filter plugin with different queries.] ****************************************************************** + # TASK [Set value as input list] *************************************************************************************** + # ok: [localhost] => { + # "ansible_facts": { + # "value": [ + # "192.24.2.1", + # "::ffff:192.168.32.0/120", + # "", + # "::ffff:192.24.2.1/128", + # "192.168.32.0/24", + # "fe80::100/10", + # true + # ] + # }, + # "changed": false + # } + # + # TASK [IPv6 filter to filter Ipv6 Address] **************************************************************************** + # ok: [localhost] => { + # "msg": [ + # "::ffff:192.168.32.0/120", + # "::ffff:192.24.2.1/128", + # "fe80::100/10" + # ] + # } + # + # TASK [convert IPv6 addresses into IPv4 addresses.] ******************************************************************* + # ok: [localhost] => { + # "msg": [ + # "192.168.32.0/24", + # "192.24.2.1/32" + # ] + # } + # + # TASK [filter only IPv6 addresses] ******************************************************************* + # ok: [localhost] => { + # "msg": [ + # "::ffff:192.168.32.0", + # "::ffff:192.24.2.1", + # "fe80::100" + # ] + # } + # + + + +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/ipv6.py b/plugins/filter/ipv6.py new file mode 100644 index 0000000..b7eb206 --- /dev/null +++ b/plugins/filter/ipv6.py @@ -0,0 +1,179 @@ +# -*- 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: ipv6 +""" +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, +) +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: ipv6 + author: Ashwini Mhatre (@amhatre) + version_added: "2.5.0" + short_description: To filter only Ipv6 addresses Ipv6 filter is used. + description: + - Sometimes you need only IPv6 addresses. To filter only Ipv6 addresses Ipv6 filter is used. + options: + value: + description: + - list of subnets or individual address or any other values input for ipv6 plugin + type: list + elements: str + required: True + query: + description: + - You can provide a single argument to each ipv6() filter. + - Example. query type 'ipv4' to convert ipv6 into ipv4 + type: str + default: '' + notes: +""" + +EXAMPLES = r""" +#### examples +# Ipv6 filter plugin with different queries. +- name: Set value as input list + ansible.builtin.set_fact: + value: + - 192.24.2.1 + - ::ffff:192.168.32.0/120 + - '' + - ::ffff:192.24.2.1/128 + - 192.168.32.0/24 + - fe80::100/10 + - True +- name: IPv6 filter to filter Ipv6 Address + debug: + msg: "{{ value|ansible.utils.ipv6 }}" + +- name: convert IPv6 addresses into IPv4 addresses. + debug: + msg: "{{ value|ansible.utils.ipv6('ipv4') }}" + +- name: filter only IPv6 addresses. + debug: + msg: "{{ value|ansible.utils.ipv6('address') }}" + + +# PLAY [Ipv6 filter plugin with different queries.] ****************************************************************** +# TASK [Set value as input list] *************************************************************************************** +# ok: [localhost] => { +# "ansible_facts": { +# "value": [ +# "192.24.2.1", +# "::ffff:192.168.32.0/120", +# "", +# "::ffff:192.24.2.1/128", +# "192.168.32.0/24", +# "fe80::100/10", +# true +# ] +# }, +# "changed": false +# } +# +# TASK [IPv6 filter to filter Ipv6 Address] **************************************************************************** +# ok: [localhost] => { +# "msg": [ +# "::ffff:192.168.32.0/120", +# "::ffff:192.24.2.1/128", +# "fe80::100/10" +# ] +# } +# +# TASK [convert IPv6 addresses into IPv4 addresses.] ******************************************************************* +# ok: [localhost] => { +# "msg": [ +# "192.168.32.0/24", +# "192.24.2.1/32" +# ] +# } +# +# TASK [filter only IPv6 addresses] ******************************************************************* +# ok: [localhost] => { +# "msg": [ +# "::ffff:192.168.32.0", +# "::ffff:192.24.2.1", +# "fe80::100" +# ] +# } +# + +""" + +RETURN = """ + data: + type: list + elements: str + description: + - Returns list with values valid for a particular query. +""" + + +@pass_environment +def _ipv6(*args, **kwargs): + """This filter is designed to return the input value if a query is True, and False if a query is False""" + keys = ["value", "query"] + data = dict(zip(keys, args[1:])) + data.update(kwargs) + aav = AnsibleArgSpecValidator(data=data, schema=DOCUMENTATION, name="ipv6") + valid, errors, updated_data = aav.validate() + if not valid: + raise AnsibleFilterError(errors) + return ipv6(**updated_data) + + +def ipv6(value, query=""): + return ipaddr(value, query, version=6, alias="ipv6") + + +class FilterModule(object): + """IP address and network manipulation filters + """ + + filter_map = { + # IP addresses and networks + "ipv6": _ipv6 + } + + def filters(self): + """ ipv6 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/ipv6.yaml b/tests/integration/targets/utils_ipaddr_filter/tasks/ipv6.yaml new file mode 100644 index 0000000..ca8a1f9 --- /dev/null +++ b/tests/integration/targets/utils_ipaddr_filter/tasks/ipv6.yaml @@ -0,0 +1,35 @@ +--- +- name: set ipaddress list + ansible.builtin.set_fact: + value: + - 192.24.2.1 + - ::ffff:192.168.32.0/120 + - '' + - ::ffff:192.24.2.1/128 + - 192.168.32.0/24 + - fe80::100/10 + - True + +- name: ipv6 filter + ansible.builtin.set_fact: + result1: "{{ value|ansible.utils.ipv6 }}" + +- name: Assert result for ipv6. + assert: + that: "{{ result1 == ipv6_result1 }}" + +- name: convert ipv6 to ipv4 filter + ansible.builtin.set_fact: + result2: "{{ value|ansible.utils.ipv6('ipv4') }}" + +- name: Assert result for ipv4. + assert: + that: "{{ result2 == ipv6_result2 }}" + +- name: Ipv6 filter with address query + ansible.builtin.set_fact: + result3: "{{ value|ansible.utils.ipv6('address') }}" + +- name: Assert result for ipv6 filter with address query. + assert: + that: "{{ result3 == ipv6_result3 }}" diff --git a/tests/integration/targets/utils_ipaddr_filter/vars/main.yaml b/tests/integration/targets/utils_ipaddr_filter/vars/main.yaml index 555abd3..f68af6b 100644 --- a/tests/integration/targets/utils_ipaddr_filter/vars/main.yaml +++ b/tests/integration/targets/utils_ipaddr_filter/vars/main.yaml @@ -10,6 +10,20 @@ result2_val: result3_val: - "192.24.2.1" +ipv6_result1: + - "::ffff:192.168.32.0/120" + - "::ffff:192.24.2.1/128" + - "fe80::100/10" + +ipv6_result2: + - "192.168.32.0/24" + - "192.24.2.1/32" + +ipv6_result3: + - "::ffff:192.168.32.0" + - "::ffff:192.24.2.1" + - "fe80::100" + cidr_result1: - 192.168.0.0/16 diff --git a/tests/unit/plugins/filter/test_ipv6.py b/tests/unit/plugins/filter/test_ipv6.py new file mode 100644 index 0000000..03a0a2f --- /dev/null +++ b/tests/unit/plugins/filter/test_ipv6.py @@ -0,0 +1,60 @@ +# -*- 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.ipv6 import _ipv6 + + +VALID_DATA = [ + "192.24.2.1", + "::ffff:192.168.32.0/120", + "", + "::ffff:192.24.2.1/128", + "192.168.32.0/24", + "fe80::100/10", + True, +] + + +VALID_OUTPUT = [ + "::ffff:192.168.32.0/120", + "::ffff:192.24.2.1/128", + "fe80::100/10", +] + +VALID_OUTPUT1 = ["192.168.32.0/24", "192.24.2.1/32"] + +VALID_OUTPUT2 = ["::ffff:192.168.32.0", "::ffff:192.24.2.1", "fe80::100"] + + +class TestIp6(unittest.TestCase): + def setUp(self): + pass + + def test_ipv6_filter_empty_query(self): + """Check ipv6 filter empty query""" + args = ["", VALID_DATA, ""] + result = _ipv6(*args) + self.assertEqual(result, VALID_OUTPUT) + + def test_ipv6_ipv4_conversion(self): + """Check ipv6 to ipv4 conversion""" + args = ["", VALID_DATA, "ipv4"] + result = _ipv6(*args) + self.assertEqual(result, VALID_OUTPUT1) + + def test_ipv6_filter_address_query(self): + """Check ipv6 filter address query""" + args = ["", VALID_DATA, "address"] + result = _ipv6(*args) + self.assertEqual(result, VALID_OUTPUT2)