From ea54e973592de8b4804a19fb5a8f0a75c73f8129 Mon Sep 17 00:00:00 2001 From: Ashwini Mhatre Date: Tue, 25 Jan 2022 03:13:18 +0530 Subject: [PATCH] Add hwaddr, macaddr, slaac filter plugin (#129) Add hwaddr, macaddr, slaac filter plugin SUMMARY Added hwaddr, macaddr, slaac filter plugin ISSUE TYPE New Module Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Nathaniel Case Reviewed-by: Ashwini Mhatre Reviewed-by: Sagar Paul Reviewed-by: None --- README.md | 3 + .../add_slaac_mac_hwaddr_filter.yaml | 3 + docs/ansible.utils.hwaddr_filter.rst | 166 ++++++++++++++++++ docs/ansible.utils.macaddr_filter.rst | 148 ++++++++++++++++ docs/ansible.utils.slaac_filter.rst | 140 +++++++++++++++ plugins/filter/hwaddr.py | 130 ++++++++++++++ plugins/filter/macaddr.py | 131 ++++++++++++++ plugins/filter/slaac.py | 147 ++++++++++++++++ plugins/plugin_utils/base/ipaddr_utils.py | 87 +++++++++ .../utils_ipaddr_filter/tasks/hwaddr.yaml | 17 ++ .../utils_ipaddr_filter/tasks/macaddr.yaml | 17 ++ .../utils_ipaddr_filter/tasks/slaac.yaml | 8 + tests/unit/plugins/filter/test_hwaddr.py | 32 ++++ tests/unit/plugins/filter/test_macaddr.py | 32 ++++ tests/unit/plugins/filter/test_slaac.py | 30 ++++ 15 files changed, 1091 insertions(+) create mode 100644 changelogs/fragments/add_slaac_mac_hwaddr_filter.yaml create mode 100644 docs/ansible.utils.hwaddr_filter.rst create mode 100644 docs/ansible.utils.macaddr_filter.rst create mode 100644 docs/ansible.utils.slaac_filter.rst create mode 100644 plugins/filter/hwaddr.py create mode 100644 plugins/filter/macaddr.py create mode 100644 plugins/filter/slaac.py create mode 100644 tests/integration/targets/utils_ipaddr_filter/tasks/hwaddr.yaml create mode 100644 tests/integration/targets/utils_ipaddr_filter/tasks/macaddr.yaml create mode 100644 tests/integration/targets/utils_ipaddr_filter/tasks/slaac.yaml create mode 100644 tests/unit/plugins/filter/test_hwaddr.py create mode 100644 tests/unit/plugins/filter/test_macaddr.py create mode 100644 tests/unit/plugins/filter/test_slaac.py diff --git a/README.md b/README.md index 8cff24c..7f3162f 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,10 @@ Name | Description [ansible.utils.cidr_merge](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.cidr_merge_filter.rst)|This filter can be used to merge subnets or individual addresses. [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.hwaddr](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.hwaddr_filter.rst)|HWaddr / MAC address filters [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.macaddr](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.macaddr_filter.rst)|macaddr / MAC address filters +[ansible.utils.slaac](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.slaac_filter.rst)|This filter returns the SLAAC address within a network for a given HW/MAC address. [ansible.utils.ipaddr](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipaddr_filter.rst)|This filter is designed to return the input value if a query is True, else False. [ansible.utils.ipwrap](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipwrap_filter.rst)|This filter is designed to Wrap IPv6 addresses in [ ] brackets. [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. diff --git a/changelogs/fragments/add_slaac_mac_hwaddr_filter.yaml b/changelogs/fragments/add_slaac_mac_hwaddr_filter.yaml new file mode 100644 index 0000000..6db98bc --- /dev/null +++ b/changelogs/fragments/add_slaac_mac_hwaddr_filter.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - Add slaac,hwaddr,mac filter plugin. diff --git a/docs/ansible.utils.hwaddr_filter.rst b/docs/ansible.utils.hwaddr_filter.rst new file mode 100644 index 0000000..d1fb4c0 --- /dev/null +++ b/docs/ansible.utils.hwaddr_filter.rst @@ -0,0 +1,166 @@ +.. _ansible.utils.hwaddr_filter: + + +******************** +ansible.utils.hwaddr +******************** + +**HWaddr / MAC address filters** + + +Version added: 2.5.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- This filter check if string is a HW/MAC address and filter it +- You can use the hwaddr() filter to check if a given string is a MAC address or convert it between various formats. + + + + +Parameters +---------- + +.. raw:: html + + + + + + + + + + + + + + + + + + + + + + + + + + +
ParameterChoices/DefaultsConfigurationComments
+
+ alias + +
+ string +
+
+ Default:
"hwaddr"
+
+ +
alias
+
+
+ query + +
+ string +
+
+ Default:
""
+
+ +
query string. Example. cisco,linux,unix etc
+
+
+ value + +
+ string + / required +
+
+ + +
HW/MAC address.
+
+
+ + + + +Examples +-------- + +.. code-block:: yaml + + #### examples + - name: Check if given string is a MAC address + debug: + msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.hwaddr }}" + + - name: Convert HW address to Cisco format + debug: + msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.hwaddr('cisco') }}" + + # TASK [Check if given string is a MAC address] *************************************************************** + # ok: [localhost] => { + # "msg": "1a:2b:3c:4d:5e:6f" + # } + # + # TASK [Convert HW address to Cisco format] ****************************************************************** + # ok: [localhost] => { + # "msg": "1a2b.3c4d.5e6f" + # } + + + +Return Values +------------- +Common return values are documented `here `_, the following are the fields unique to this filter: + +.. raw:: html + + + + + + + + + + + + +
KeyReturnedDescription
+
+ data + +
+ string +
+
+
mac/Hw address
+
+
+

+ + +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/docs/ansible.utils.macaddr_filter.rst b/docs/ansible.utils.macaddr_filter.rst new file mode 100644 index 0000000..4102be8 --- /dev/null +++ b/docs/ansible.utils.macaddr_filter.rst @@ -0,0 +1,148 @@ +.. _ansible.utils.macaddr_filter: + + +********************* +ansible.utils.macaddr +********************* + +**macaddr / MAC address filters** + + +Version added: 2.5.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- This filter check if string is a MAC address and filter it +- You can use the macaddr() filter to check if a given string is a MAC address or convert it between various formats. + + + + +Parameters +---------- + +.. raw:: html + + + + + + + + + + + + + + + + + + + + +
ParameterChoices/DefaultsConfigurationComments
+
+ query + +
+ string +
+
+ Default:
""
+
+ +
query string. Example. cisco,linux,unix etc
+
+
+ value + +
+ string + / required +
+
+ + +
HW/MAC address.
+
+
+ + + + +Examples +-------- + +.. code-block:: yaml + + #### examples + - name: Check if given string is a MAC address + debug: + msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.macaddr }}" + + - name: Convert MAC address to Cisco format + debug: + msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.macaddr('cisco') }}" + + # TASK [Check if given string is a MAC address] *************************************************************** + # ok: [localhost] => { + # "msg": "1a:2b:3c:4d:5e:6f" + # } + # + # TASK [Convert MAC address to Cisco format] ****************************************************************** + # ok: [localhost] => { + # "msg": "1a2b.3c4d.5e6f" + # } + + + +Return Values +------------- +Common return values are documented `here `_, the following are the fields unique to this filter: + +.. raw:: html + + + + + + + + + + + + +
KeyReturnedDescription
+
+ data + +
+ string +
+
+
mac/Hw address
+
+
+

+ + +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/docs/ansible.utils.slaac_filter.rst b/docs/ansible.utils.slaac_filter.rst new file mode 100644 index 0000000..5f16c45 --- /dev/null +++ b/docs/ansible.utils.slaac_filter.rst @@ -0,0 +1,140 @@ +.. _ansible.utils.slaac_filter: + + +******************* +ansible.utils.slaac +******************* + +**This filter returns the SLAAC address within a network for a given HW/MAC address.** + + +Version added: 2.5.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- This filter returns the SLAAC address within a network for a given HW/MAC address. +- The filter slaac() generates an IPv6 address for a given network and a MAC Address in Stateless Configuration. + + + + +Parameters +---------- + +.. raw:: html + + + + + + + + + + + + + + + + + + + + +
ParameterChoices/DefaultsConfigurationComments
+
+ query + +
+ string +
+
+ + +
nth host
+
+
+ value + +
+ string + / required +
+
+ + +
The network address or range to test against.
+
+
+ + + + +Examples +-------- + +.. code-block:: yaml + + #### examples + - name: The filter slaac() generates an IPv6 address for a given network and a MAC Address in Stateless Configuration. + debug: + msg: "{{ 'fdcf:1894:23b5:d38c:0000:0000:0000:0000' | slaac('c2:31:b3:83:bf:2b') }}" + + # TASK [The filter slaac() generates an IPv6 address for a given network and a MAC Address in Stateless Configuration.] *** + # task path: /Users/amhatre/ansible-collections/playbooks/test_slaac.yaml:7 + # Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils + # ok: [localhost] => { + # "msg": "fdcf:1894:23b5:d38c:c031:b3ff:fe83:bf2b" + # } + + + +Return Values +------------- +Common return values are documented `here `_, the following are the fields unique to this filter: + +.. raw:: html + + + + + + + + + + + + +
KeyReturnedDescription
+
+ data + +
+ string +
+
+
Returns nth host from 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/hwaddr.py b/plugins/filter/hwaddr.py new file mode 100644 index 0000000..e37f9cc --- /dev/null +++ b/plugins/filter/hwaddr.py @@ -0,0 +1,130 @@ +# -*- 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: hwaddr +""" +from __future__ import absolute_import, division, print_function +from functools import partial +from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddr_utils import ( + _need_netaddr, + hwaddr, +) +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: hwaddr + author: Ashwini Mhatre (@amhatre) + version_added: "2.5.0" + short_description: HWaddr / MAC address filters + description: + - This filter check if string is a HW/MAC address and filter it + - You can use the hwaddr() filter to check if a given string is a MAC address or convert it between various + formats. + options: + value: + description: HW/MAC address. + type: str + required: True + query: + description: query string. Example. cisco,linux,unix etc + type: str + default: "" + alias: + description: alias + type: str + default: hwaddr + notes: +""" + +EXAMPLES = r""" +#### examples +- name: Check if given string is a MAC address + debug: + msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.hwaddr }}" + +- name: Convert HW address to Cisco format + debug: + msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.hwaddr('cisco') }}" + +# TASK [Check if given string is a MAC address] *************************************************************** +# ok: [localhost] => { +# "msg": "1a:2b:3c:4d:5e:6f" +# } +# +# TASK [Convert HW address to Cisco format] ****************************************************************** +# ok: [localhost] => { +# "msg": "1a2b.3c4d.5e6f" +# } + + +""" + +RETURN = """ + data: + type: str + description: + - mac/Hw address + +""" + + +@pass_environment +def _hwaddr(*args, **kwargs): + """This filter check if string is a HW/MAC address and filter it """ + keys = ["value", "query", "alias"] + data = dict(zip(keys, args[1:])) + data.update(kwargs) + aav = AnsibleArgSpecValidator( + data=data, schema=DOCUMENTATION, name="hwaddr" + ) + valid, errors, updated_data = aav.validate() + if not valid: + raise AnsibleFilterError(errors) + return hwaddr(**updated_data) + + +class FilterModule(object): + """IP address and network manipulation filters + """ + + filter_map = { + # IP addresses and networks + "hwaddr": _hwaddr + } + + 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/plugins/filter/macaddr.py b/plugins/filter/macaddr.py new file mode 100644 index 0000000..a285415 --- /dev/null +++ b/plugins/filter/macaddr.py @@ -0,0 +1,131 @@ +# -*- 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: macaddr +""" +from __future__ import absolute_import, division, print_function +from functools import partial +from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddr_utils import ( + _need_netaddr, + hwaddr, +) +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: macaddr + author: Ashwini Mhatre (@amhatre) + version_added: "2.5.0" + short_description: macaddr / MAC address filters + description: + - This filter check if string is a MAC address and filter it + - You can use the macaddr() filter to check if a given string is a MAC address or convert it between various + formats. + options: + value: + description: HW/MAC address. + type: str + required: True + query: + description: query string. Example. cisco,linux,unix etc + type: str + default: "" + notes: +""" + +EXAMPLES = r""" +#### examples +- name: Check if given string is a MAC address + debug: + msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.macaddr }}" + +- name: Convert MAC address to Cisco format + debug: + msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.macaddr('cisco') }}" + +# TASK [Check if given string is a MAC address] *************************************************************** +# ok: [localhost] => { +# "msg": "1a:2b:3c:4d:5e:6f" +# } +# +# TASK [Convert MAC address to Cisco format] ****************************************************************** +# ok: [localhost] => { +# "msg": "1a2b.3c4d.5e6f" +# } + + + +""" + +RETURN = """ + data: + type: str + description: + - mac/Hw address + +""" + + +@pass_environment +def _macaddr(*args, **kwargs): + """This filter check if string is a HW/MAC address and filter it """ + keys = ["value", "query"] + data = dict(zip(keys, args[1:])) + data.update(kwargs) + aav = AnsibleArgSpecValidator( + data=data, schema=DOCUMENTATION, name="macaddr" + ) + valid, errors, updated_data = aav.validate() + if not valid: + raise AnsibleFilterError(errors) + return macaddr(**updated_data) + + +def macaddr(value, query=""): + return hwaddr(value, query, alias="macaddr") + + +class FilterModule(object): + """IP address and network manipulation filters + """ + + filter_map = { + # IP addresses and networks + "macaddr": _macaddr + } + + 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/plugins/filter/slaac.py b/plugins/filter/slaac.py new file mode 100644 index 0000000..0cdf0e6 --- /dev/null +++ b/plugins/filter/slaac.py @@ -0,0 +1,147 @@ +# -*- 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: slaac +""" +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, + hwaddr, +) +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: slaac + author: Ashwini Mhatre (@amhatre) + version_added: "2.5.0" + short_description: This filter returns the SLAAC address within a network for a given HW/MAC address. + description: + - This filter returns the SLAAC address within a network for a given HW/MAC address. + - The filter slaac() generates an IPv6 address for a given network and a MAC Address in Stateless Configuration. + options: + value: + description: The network address or range to test against. + type: str + required: True + query: + description: nth host + type: str + notes: +""" + +EXAMPLES = r""" +#### examples +- name: The filter slaac() generates an IPv6 address for a given network and a MAC Address in Stateless Configuration. + debug: + msg: "{{ 'fdcf:1894:23b5:d38c:0000:0000:0000:0000' | slaac('c2:31:b3:83:bf:2b') }}" + +# TASK [The filter slaac() generates an IPv6 address for a given network and a MAC Address in Stateless Configuration.] *** +# task path: /Users/amhatre/ansible-collections/playbooks/test_slaac.yaml:7 +# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils +# ok: [localhost] => { +# "msg": "fdcf:1894:23b5:d38c:c031:b3ff:fe83:bf2b" +# } + + +""" + +RETURN = """ + data: + type: str + description: + - Returns nth host from network + +""" + + +@pass_environment +def _slaac(*args, **kwargs): + """This filter returns whether an address or a network passed as argument is in a network.""" + keys = ["value", "query"] + data = dict(zip(keys, args[1:])) + data.update(kwargs) + aav = AnsibleArgSpecValidator( + data=data, schema=DOCUMENTATION, name="slaac" + ) + valid, errors, updated_data = aav.validate() + if not valid: + raise AnsibleFilterError(errors) + return slaac(**updated_data) + + +def slaac(value, query=""): + """ Get the SLAAC address within given network """ + try: + vtype = ipaddr(value, "type") + if vtype == "address": + v = ipaddr(value, "cidr") + elif vtype == "network": + v = ipaddr(value, "subnet") + + if ipaddr(value, "version") != 6: + return False + + value = netaddr.IPNetwork(v) + except Exception: + return False + + if not query: + return False + + try: + mac = hwaddr(query, alias="slaac") + + eui = netaddr.EUI(mac) + except Exception: + return False + + return str(eui.ipv6(value.network)) + + +class FilterModule(object): + """IP address and network manipulation filters + """ + + filter_map = { + # IP addresses and networks + "slaac": _slaac + } + + 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/plugins/plugin_utils/base/ipaddr_utils.py b/plugins/plugin_utils/base/ipaddr_utils.py index d527943..59b8e06 100644 --- a/plugins/plugin_utils/base/ipaddr_utils.py +++ b/plugins/plugin_utils/base/ipaddr_utils.py @@ -637,3 +637,90 @@ def _range_checker(ip_check, first, last): return True else: return False + + +# ---- HWaddr query helpers ---- +def _bare_query(v): + v.dialect = netaddr.mac_bare + return str(v) + + +def _bool_hwaddr_query(v): + if v: + return True + + +def _int_hwaddr_query(v): + return int(v) + + +def _cisco_query(v): + v.dialect = netaddr.mac_cisco + return str(v) + + +def _empty_hwaddr_query(v, value): + if v: + return value + + +def _linux_query(v): + v.dialect = mac_linux + return str(v) + + +def _postgresql_query(v): + v.dialect = netaddr.mac_pgsql + import epdb + + epdb.serve() + return str(v) + + +def _unix_query(v): + v.dialect = netaddr.mac_unix + return str(v) + + +def _win_query(v): + v.dialect = netaddr.mac_eui48 + return str(v) + + +# ---- HWaddr / MAC address filters ---- +def hwaddr(value, query="", alias="hwaddr"): + """ Check if string is a HW/MAC address and filter it """ + + query_func_extra_args = {"": ("value",)} + + query_func_map = { + "": _empty_hwaddr_query, + "bare": _bare_query, + "bool": _bool_hwaddr_query, + "int": _int_hwaddr_query, + "cisco": _cisco_query, + "eui48": _win_query, + "linux": _linux_query, + "pgsql": _postgresql_query, + "postgresql": _postgresql_query, + "psql": _postgresql_query, + "unix": _unix_query, + "win": _win_query, + } + + try: + v = netaddr.EUI(value) + except Exception: + v = None + if query and query != "bool": + raise AnsibleFilterError( + alias + ": not a hardware address: %s" % value + ) + + extras = [] + for arg in query_func_extra_args.get(query, tuple()): + extras.append(locals()[arg]) + try: + return query_func_map[query](v, *extras) + except KeyError: + raise AnsibleFilterError(alias + ": unknown filter type: %s" % query) diff --git a/tests/integration/targets/utils_ipaddr_filter/tasks/hwaddr.yaml b/tests/integration/targets/utils_ipaddr_filter/tasks/hwaddr.yaml new file mode 100644 index 0000000..094d5e9 --- /dev/null +++ b/tests/integration/targets/utils_ipaddr_filter/tasks/hwaddr.yaml @@ -0,0 +1,17 @@ +--- +- name: hwaddr filter test1 + ansible.builtin.set_fact: + result1: "{{ '1a:2b:3c:4d:5e:6f'|ansible.utils.hwaddr }}" + +- name: Assert result for hwaddr. + assert: + that: "{{ result1 == '1a:2b:3c:4d:5e:6f' }}" + + +- name: hwaddr filter test2 + ansible.builtin.set_fact: + result1: "{{ '1a:2b:3c:4d:5e:6f'|ansible.utils.hwaddr('cisco') }}" + +- name: Assert result for hwaddr. + assert: + that: "{{ result1 == '1a2b.3c4d.5e6f' }}" diff --git a/tests/integration/targets/utils_ipaddr_filter/tasks/macaddr.yaml b/tests/integration/targets/utils_ipaddr_filter/tasks/macaddr.yaml new file mode 100644 index 0000000..1622c1c --- /dev/null +++ b/tests/integration/targets/utils_ipaddr_filter/tasks/macaddr.yaml @@ -0,0 +1,17 @@ +--- +- name: macaddr filter test1 + ansible.builtin.set_fact: + result1: "{{ '1a:2b:3c:4d:5e:6f'|ansible.utils.macaddr }}" + +- name: Assert result for macaddr. + assert: + that: "{{ result1 == '1a:2b:3c:4d:5e:6f' }}" + + +- name: macaddr filter test2 + ansible.builtin.set_fact: + result1: "{{ '1a:2b:3c:4d:5e:6f'|ansible.utils.macaddr('cisco') }}" + +- name: Assert result for macaddr. + assert: + that: "{{ result1 == '1a2b.3c4d.5e6f' }}" diff --git a/tests/integration/targets/utils_ipaddr_filter/tasks/slaac.yaml b/tests/integration/targets/utils_ipaddr_filter/tasks/slaac.yaml new file mode 100644 index 0000000..e86399d --- /dev/null +++ b/tests/integration/targets/utils_ipaddr_filter/tasks/slaac.yaml @@ -0,0 +1,8 @@ +--- +- name: slaac filter test1 + ansible.builtin.set_fact: + result1: "{{ 'fdcf:1894:23b5:d38c:0000:0000:0000:0000'|ansible.utils.slaac('c2:31:b3:83:bf:2b') }}" + +- name: Assert result for slaac. + assert: + that: "{{ result1 == 'fdcf:1894:23b5:d38c:c031:b3ff:fe83:bf2b' }}" diff --git a/tests/unit/plugins/filter/test_hwaddr.py b/tests/unit/plugins/filter/test_hwaddr.py new file mode 100644 index 0000000..d7a6873 --- /dev/null +++ b/tests/unit/plugins/filter/test_hwaddr.py @@ -0,0 +1,32 @@ +# -*- 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 hwaddr filter plugin +""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import unittest +from ansible_collections.ansible.utils.plugins.filter.hwaddr import _hwaddr + + +class Test_hwaddr(unittest.TestCase): + def setUp(self): + pass + + def test_hwaddr_filter_1(self): + """hwaddr filter""" + args = ["", "1a:2b:3c:4d:5e:6f"] + result = _hwaddr(*args) + self.assertEqual(result, "1a:2b:3c:4d:5e:6f") + + def test_hwaddr_filter_2(self): + """hwaddr filter""" + args = ["", "1a:2b:3c:4d:5e:6f", "cisco"] + result = _hwaddr(*args) + self.assertEqual(result, "1a2b.3c4d.5e6f") diff --git a/tests/unit/plugins/filter/test_macaddr.py b/tests/unit/plugins/filter/test_macaddr.py new file mode 100644 index 0000000..2f60544 --- /dev/null +++ b/tests/unit/plugins/filter/test_macaddr.py @@ -0,0 +1,32 @@ +# -*- 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 macaddr filter plugin +""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import unittest +from ansible_collections.ansible.utils.plugins.filter.macaddr import _macaddr + + +class Test_macaddr(unittest.TestCase): + def setUp(self): + pass + + def test_macaddr_filter_1(self): + """macaddr filter""" + args = ["", "1a:2b:3c:4d:5e:6f"] + result = _macaddr(*args) + self.assertEqual(result, "1a:2b:3c:4d:5e:6f") + + def test_macaddr_filter_2(self): + """macaddr filter""" + args = ["", "1a:2b:3c:4d:5e:6f", "cisco"] + result = _macaddr(*args) + self.assertEqual(result, "1a2b.3c4d.5e6f") diff --git a/tests/unit/plugins/filter/test_slaac.py b/tests/unit/plugins/filter/test_slaac.py new file mode 100644 index 0000000..39cbf78 --- /dev/null +++ b/tests/unit/plugins/filter/test_slaac.py @@ -0,0 +1,30 @@ +# -*- 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 slaac filter plugin +""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import unittest +from ansible_collections.ansible.utils.plugins.filter.slaac import _slaac + + +class Test_slaac(unittest.TestCase): + def setUp(self): + pass + + def test_slaac_filter_1(self): + """slaac filter""" + args = [ + "", + "fdcf:1894:23b5:d38c:0000:0000:0000:0000", + "c2:31:b3:83:bf:2b", + ] + result = _slaac(*args) + self.assertEqual(result, "fdcf:1894:23b5:d38c:c031:b3ff:fe83:bf2b")