Add ipv4_hex filter plugin (#119)

Add ipv4_hex filter plugin

SUMMARY


ISSUE TYPE


New Module Pull Request

COMPONENT NAME

ADDITIONAL INFORMATION

Reviewed-by: Priyam Sahoo <None>
Reviewed-by: None <None>
pull/121/head^2
Ashwini Mhatre 2022-01-20 17:18:12 +05:30 committed by GitHub
parent 458547c2b6
commit 79b28faaaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 352 additions and 0 deletions

View File

@ -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.

View File

@ -0,0 +1,3 @@
---
minor_changes:
- Add ip4_hex filter plugin.

View File

@ -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
<table border=0 cellpadding=0 class="documentation-table">
<tr>
<th colspan="1">Parameter</th>
<th>Choices/<font color="blue">Defaults</font></th>
<th>Configuration</th>
<th width="100%">Comments</th>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>arg</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">string</span>
/ <span style="color: red">required</span>
</div>
</td>
<td>
</td>
<td>
</td>
<td>
<div>IPv4 address.</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>delimiter</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">string</span>
</div>
</td>
<td>
<b>Default:</b><br/><div style="color: blue">""</div>
</td>
<td>
</td>
<td>
<div>You can provide a single argument to each ip4_hex() filter as delimiter.</div>
</td>
</tr>
</table>
<br/>
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 <https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#common-return-values>`_, the following are the fields unique to this filter:
.. raw:: html
<table border=0 cellpadding=0 class="documentation-table">
<tr>
<th colspan="1">Key</th>
<th>Returned</th>
<th width="100%">Description</th>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="return-"></div>
<b>data</b>
<a class="ansibleOptionLink" href="#return-" title="Permalink to this return value"></a>
<div style="font-size: small">
<span style="color: purple">string</span>
</div>
</td>
<td></td>
<td>
<div>Returns IPv4 address to Hexadecimal notation.</div>
<br/>
</td>
</tr>
</table>
<br/><br/>
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.

135
plugins/filter/ip4_hex.py Normal file
View File

@ -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
)

View File

@ -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' }}"

View File

@ -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))