diff --git a/README.md b/README.md index 6f028c8..3280ae1 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.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. [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. diff --git a/changelogs/fragments/add_ip4_hex_filter.yaml b/changelogs/fragments/add_ip4_hex_filter.yaml new file mode 100644 index 0000000..8211810 --- /dev/null +++ b/changelogs/fragments/add_ip4_hex_filter.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - Add ip4_hex filter plugin. diff --git a/docs/ansible.utils.ip4_hex_filter.rst b/docs/ansible.utils.ip4_hex_filter.rst new file mode 100644 index 0000000..8ba4094 --- /dev/null +++ b/docs/ansible.utils.ip4_hex_filter.rst @@ -0,0 +1,151 @@ +.. _ansible.utils.ip4_hex_filter: + + +********************* +ansible.utils.ip4_hex +********************* + +**This filter is designed to convert IPv4 address to Hexadecimal notation with optional delimiter.** + + +Version added: 2.5.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- This filter convert IPv4 address to Hexadecimal notation with optional delimiter + + + + +Parameters +---------- + +.. raw:: html + + + + + + + + + + + + + + + + + + + + +
ParameterChoices/DefaultsConfigurationComments
+
+ arg + +
+ string + / required +
+
+ + +
IPv4 address.
+
+
+ delimiter + +
+ string +
+
+ Default:
""
+
+ +
You can provide a single argument to each ip4_hex() filter as delimiter.
+
+
+ + + + +Examples +-------- + +.. code-block:: yaml + + #### examples + # ip4_hex convert IPv4 address to Hexadecimal notation with optional delimiter + - debug: + msg: "{{ '192.168.1.5' | ansible.utils.ip4_hex }}" + + # ip4_hex with delimiter + - debug: + msg: "{{ '192.168.1.5' | ansible.utils.ip4_hex(':') }}" + + # TASK [debug] ************************************************************************************************ + # task path: /Users/amhatre/ansible-collections/playbooks/test_ip4_hex.yaml:7 + # Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils + # ok: [localhost] => { + # "msg": "c0a80105" + # } + # + # TASK [debug] ************************************************************************************************ + # task path: /Users/amhatre/ansible-collections/playbooks/test_ip4_hex.yaml:11 + # Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils + # ok: [localhost] => { + # "msg": "c0:a8:01:05" + # } + + + +Return Values +------------- +Common return values are documented `here `_, the following are the fields unique to this filter: + +.. raw:: html + + + + + + + + + + + + +
KeyReturnedDescription
+
+ data + +
+ string +
+
+
Returns IPv4 address to Hexadecimal notation.
+
+
+

+ + +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/ip4_hex.py b/plugins/filter/ip4_hex.py new file mode 100644 index 0000000..97a5316 --- /dev/null +++ b/plugins/filter/ip4_hex.py @@ -0,0 +1,135 @@ +# -*- 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: ip4_hex +""" +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, +) +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: ip4_hex + author: Ashwini Mhatre (@amhatre) + version_added: "2.5.0" + short_description: This filter is designed to convert IPv4 address to Hexadecimal notation with optional delimiter. + description: + - This filter convert IPv4 address to Hexadecimal notation with optional delimiter + options: + arg: + description: IPv4 address. + type: str + required: True + delimiter: + description: + - You can provide a single argument to each ip4_hex() filter as delimiter. + type: str + default: '' + notes: +""" + +EXAMPLES = r""" +#### examples +# ip4_hex convert IPv4 address to Hexadecimal notation with optional delimiter +- debug: + msg: "{{ '192.168.1.5' | ansible.utils.ip4_hex }}" + +# ip4_hex with delimiter +- debug: + msg: "{{ '192.168.1.5' | ansible.utils.ip4_hex(':') }}" + +# TASK [debug] ************************************************************************************************ +# task path: /Users/amhatre/ansible-collections/playbooks/test_ip4_hex.yaml:7 +# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils +# ok: [localhost] => { +# "msg": "c0a80105" +# } +# +# TASK [debug] ************************************************************************************************ +# task path: /Users/amhatre/ansible-collections/playbooks/test_ip4_hex.yaml:11 +# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils +# ok: [localhost] => { +# "msg": "c0:a8:01:05" +# } + + +""" + +RETURN = """ + data: + type: str + description: + - Returns IPv4 address to Hexadecimal notation. +""" + + +@pass_environment +def _ip4_hex(*args, **kwargs): + """This filter is designed to Convert an IPv4 address to Hexadecimal notation""" + keys = ["arg", "delimiter"] + data = dict(zip(keys, args[1:])) + data.update(kwargs) + aav = AnsibleArgSpecValidator( + data=data, schema=DOCUMENTATION, name="ip4_hex" + ) + valid, errors, updated_data = aav.validate() + if not valid: + raise AnsibleFilterError(errors) + return ip4_hex(**updated_data) + + +def ip4_hex(arg, delimiter=""): + """ Convert an IPv4 address to Hexadecimal notation """ + numbers = list(map(int, arg.split("."))) + return "{0:02x}{sep}{1:02x}{sep}{2:02x}{sep}{3:02x}".format( + *numbers, sep=delimiter + ) + + +class FilterModule(object): + """IP address and network manipulation filters + """ + + filter_map = { + # IP addresses and networks + "ip4_hex": _ip4_hex + } + + def filters(self): + """ ip4_hex 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/ip4_hex.yaml b/tests/integration/targets/utils_ipaddr_filter/tasks/ip4_hex.yaml new file mode 100644 index 0000000..0fd613b --- /dev/null +++ b/tests/integration/targets/utils_ipaddr_filter/tasks/ip4_hex.yaml @@ -0,0 +1,16 @@ +--- +- name: ip4_hex without delimiter + ansible.builtin.set_fact: + result1: "{{ '192.168.1.5'|ansible.utils.ip4_hex }}" + +- name: Assert result for ip4_hex. + assert: + that: "{{ result1 == 'c0a80105' }}" + +- name: ip4_hex with delimiter + ansible.builtin.set_fact: + result1: "{{ '192.168.1.5'|ansible.utils.ip4_hex(':') }}" + +- name: Assert result for ip4_hex. + assert: + that: "{{ result1 == 'c0:a8:01:05' }}" diff --git a/tests/unit/plugins/filter/test_ip4_hex.py b/tests/unit/plugins/filter/test_ip4_hex.py new file mode 100644 index 0000000..8d0e1e9 --- /dev/null +++ b/tests/unit/plugins/filter/test_ip4_hex.py @@ -0,0 +1,46 @@ +# -*- 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.ip4_hex import _ip4_hex + +from ansible.errors import AnsibleFilterError + + +class TestIpWrap(unittest.TestCase): + def setUp(self): + pass + + def test_valid_data_list(self): + """Check passing valid argspec(list)""" + args = ["", "192.168.1.5", ""] + result = _ip4_hex(*args) + print(result) + self.assertEqual(result, "c0a80105") + + def test_valid_data_string(self): + """Check passing valid argspec(string)""" + + args = ["", "192.168.1.5", ":"] + result = _ip4_hex(*args) + self.assertEqual(result, "c0:a8:01:05") + + def test_args(self): + """Check passing invalid argspec""" + + # missing required arguments + args = [] + kwargs = {} + with self.assertRaises(AnsibleFilterError) as error: + _ip4_hex(*args, **kwargs) + self.assertIn("missing required arguments: arg", str(error.exception))