Add Ipmath filter plugin (#117)

Add Ipmath filter plugin

SUMMARY


ISSUE TYPE


New Module Pull Request

COMPONENT NAME

ADDITIONAL INFORMATION

Reviewed-by: Sagar Paul <sagpaul@redhat.com>
Reviewed-by: Ashwini Mhatre <mashu97@gmail.com>
Reviewed-by: None <None>
pull/119/head^2
Ashwini Mhatre 2022-01-20 15:19:15 +05:30 committed by GitHub
parent 22234f2963
commit 458547c2b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 507 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.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.
[ansible.utils.param_list_compare](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.param_list_compare_filter.rst)|Generate the final param list combining/comparing base and provided parameters.

View File

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

View File

@ -0,0 +1,194 @@
.. _ansible.utils.ipmath_filter:
********************
ansible.utils.ipmath
********************
**This filter is designed to do simple IP math/arithmetic.**
Version added: 2.5.0
.. contents::
:local:
:depth: 1
Synopsis
--------
- This filter is designed to do simple IP math/arithmetic.
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>amount</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">integer</span>
</div>
</td>
<td>
</td>
<td>
</td>
<td>
<div>integer for arithmetic. Example -1,2,3</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>value</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>list of subnets or individual address or any other values input for ipaddr plugin</div>
</td>
</tr>
</table>
<br/>
Examples
--------
.. code-block:: yaml
#### examples
# Ipmath filter plugin with different arthmetic.
# Get the next fifth address based on an IP address
- debug:
msg: "{{ '192.168.1.5' | ansible.netcommon.ipmath(5) }}"
# Get the tenth previous address based on an IP address
- debug:
msg: "{{ '192.168.1.5' | ansible.netcommon.ipmath(-10) }}"
# Get the next fifth address using CIDR notation
- debug:
msg: "{{ '192.168.1.1/24' | ansible.netcommon.ipmath(5) }}"
# Get the previous fifth address using CIDR notation
- debug:
msg: "{{ '192.168.1.6/24' | ansible.netcommon.ipmath(-5) }}"
# Get the previous tenth address using cidr notation
# It returns a address of the previous network range
- debug:
msg: "{{ '192.168.2.6/24' | ansible.netcommon.ipmath(-10) }}"
# Get the next tenth address in IPv6
- debug:
msg: "{{ '2001::1' | ansible.netcommon.ipmath(10) }}"
# Get the previous tenth address in IPv6
- debug:
msg: "{{ '2001::5' | ansible.netcommon.ipmath(-10) }}"
# TASK [debug] **********************************************************************************************************
# ok: [localhost] => {
# "msg": "192.168.1.10"
# }
#
# TASK [debug] **********************************************************************************************************
# ok: [localhost] => {
# "msg": "192.168.0.251"
# }
#
# TASK [debug] **********************************************************************************************************
# ok: [localhost] => {
# "msg": "192.168.1.6"
# }
#
# TASK [debug] **********************************************************************************************************
# ok: [localhost] => {
# "msg": "192.168.1.1"
# }
#
# TASK [debug] **********************************************************************************************************
# ok: [localhost] => {
# "msg": "192.168.1.252"
# }
#
# TASK [debug] **********************************************************************************************************
# ok: [localhost] => {
# "msg": "2001::b"
# }
#
# TASK [debug] **********************************************************************************************************
# ok: [localhost] => {
# "msg": "2000:ffff:ffff:ffff:ffff:ffff:ffff:fffb"
# }
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">list</span>
/ <span style="color: purple">elements=string</span>
</div>
</td>
<td></td>
<td>
<div>Returns list with values valid for a particular query.</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.

188
plugins/filter/ipmath.py Normal file
View File

@ -0,0 +1,188 @@
# -*- 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: cidr_merge
"""
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: ipmath
author: Ashwini Mhatre (@amhatre)
version_added: "2.5.0"
short_description: This filter is designed to do simple IP math/arithmetic.
description:
- This filter is designed to do simple IP math/arithmetic.
options:
value:
description:
- list of subnets or individual address or any other values input for ipaddr plugin
type: str
required: True
amount:
type: int
description: integer for arithmetic. Example -1,2,3
"""
EXAMPLES = r"""
#### examples
# Ipmath filter plugin with different arthmetic.
# Get the next fifth address based on an IP address
- debug:
msg: "{{ '192.168.1.5' | ansible.netcommon.ipmath(5) }}"
# Get the tenth previous address based on an IP address
- debug:
msg: "{{ '192.168.1.5' | ansible.netcommon.ipmath(-10) }}"
# Get the next fifth address using CIDR notation
- debug:
msg: "{{ '192.168.1.1/24' | ansible.netcommon.ipmath(5) }}"
# Get the previous fifth address using CIDR notation
- debug:
msg: "{{ '192.168.1.6/24' | ansible.netcommon.ipmath(-5) }}"
# Get the previous tenth address using cidr notation
# It returns a address of the previous network range
- debug:
msg: "{{ '192.168.2.6/24' | ansible.netcommon.ipmath(-10) }}"
# Get the next tenth address in IPv6
- debug:
msg: "{{ '2001::1' | ansible.netcommon.ipmath(10) }}"
# Get the previous tenth address in IPv6
- debug:
msg: "{{ '2001::5' | ansible.netcommon.ipmath(-10) }}"
# TASK [debug] **********************************************************************************************************
# ok: [localhost] => {
# "msg": "192.168.1.10"
# }
#
# TASK [debug] **********************************************************************************************************
# ok: [localhost] => {
# "msg": "192.168.0.251"
# }
#
# TASK [debug] **********************************************************************************************************
# ok: [localhost] => {
# "msg": "192.168.1.6"
# }
#
# TASK [debug] **********************************************************************************************************
# ok: [localhost] => {
# "msg": "192.168.1.1"
# }
#
# TASK [debug] **********************************************************************************************************
# ok: [localhost] => {
# "msg": "192.168.1.252"
# }
#
# TASK [debug] **********************************************************************************************************
# ok: [localhost] => {
# "msg": "2001::b"
# }
#
# TASK [debug] **********************************************************************************************************
# ok: [localhost] => {
# "msg": "2000:ffff:ffff:ffff:ffff:ffff:ffff:fffb"
# }
"""
RETURN = """
data:
type: list
elements: str
description:
- Returns list with values valid for a particular query.
"""
@pass_environment
def _ipmath(*args, **kwargs):
"""Convert the given data from json to xml."""
keys = ["value", "amount"]
data = dict(zip(keys, args[1:]))
data.update(kwargs)
aav = AnsibleArgSpecValidator(
data=data, schema=DOCUMENTATION, name="ipmath"
)
valid, errors, updated_data = aav.validate()
if not valid:
raise AnsibleFilterError(errors)
return ipmath(**updated_data)
def ipmath(value, amount):
try:
if "/" in value:
ip = netaddr.IPNetwork(value).ip
else:
ip = netaddr.IPAddress(value)
except (netaddr.AddrFormatError, ValueError):
msg = "You must pass a valid IP address; {0} is invalid".format(value)
raise AnsibleFilterError(msg)
if not isinstance(amount, int):
msg = (
"You must pass an integer for arithmetic; "
"{0} is not a valid integer"
).format(amount)
raise AnsibleFilterError(msg)
return str(ip + amount)
class FilterModule(object):
"""IP address and network manipulation filters
"""
filter_map = {
# This filter is designed to do simple IP math/arithmetic
"ipmath": _ipmath
}
def filters(self):
""" ipmath 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,49 @@
---
- name: Get the next fifth address based on an IP address with Ipmath filter
ansible.builtin.set_fact:
result1: "{{ '192.168.1.5'|ansible.utils.ipmath('5') }}"
- name: Assert result for ipmath.
assert:
that: "{{ result1 == '192.168.1.10' }}"
- name: Get the tenth previous address based on an IP address with Ipmath filter
ansible.builtin.set_fact:
result1: "{{ '192.168.1.5'|ansible.utils.ipmath('-10') }}"
- name: Assert result for ipmath.
assert:
that: "{{ result1 == '192.168.0.251' }}"
- name: Get the next fifth address using CIDR notation with Ipmath filter
ansible.builtin.set_fact:
result1: "{{ '192.168.1.1/24'|ansible.utils.ipmath('5') }}"
- name: Assert result for ipmath.
assert:
that: "{{ result1 == '192.168.1.6' }}"
- name: Get the previous fifth address using CIDR notation with Ipmath filter
ansible.builtin.set_fact:
result1: "{{ '192.168.1.6/24'|ansible.utils.ipmath('-5') }}"
- name: Assert result for ipmath.
assert:
that: "{{ result1 == '192.168.1.1' }}"
- name: Get the next tenth address in IPv6 with Ipmath filter
ansible.builtin.set_fact:
result1: "{{ '2001::1'|ansible.utils.ipmath('10') }}"
- name: Assert result for ipmath.
assert:
that: "{{ result1 == '2001::b' }}"
- name: Get the previous tenth address in IPv6 with Ipmath filter
ansible.builtin.set_fact:
result1: "{{ '2001::5'|ansible.utils.ipmath('-10') }}"
- name: Assert result for ipmath.
assert:
that: "{{ result1 == '2000:ffff:ffff:ffff:ffff:ffff:ffff:fffb' }}"

View File

@ -0,0 +1,72 @@
# -*- 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 ipmath filter plugin
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import unittest
from ansible.errors import AnsibleFilterError
from ansible_collections.ansible.utils.plugins.filter.ipmath import _ipmath
class TestIpAddr(unittest.TestCase):
def setUp(self):
pass
def test_find_next_fifth_address(self):
"""Get the next fifth address based on an IP address"""
args = ["", "192.168.1.5", 5]
result = _ipmath(*args)
self.assertEqual(result, "192.168.1.10")
def test_find_previous_fifth_address(self):
"""Get the previous fifth address """
args = ["", "192.168.1.5", -10]
result = _ipmath(*args)
self.assertEqual(result, "192.168.0.251")
def test_find_next_fifth_address_cidr(self):
"""Get the next fifth address CIDR notation"""
args = ["", "192.168.1.1/24", 5]
result = _ipmath(*args)
self.assertEqual(result, "192.168.1.6")
def test_find_previous_fifth_address_cidr(self):
"""Get the previous fifth address CIDR notation"""
args = ["", "192.168.1.6/24", -5]
result = _ipmath(*args)
self.assertEqual(result, "192.168.1.1")
def test_find_next_fifth_address_ipv6(self):
"""Get the next fifth address in ipv6"""
args = ["", "2001::1", 10]
result = _ipmath(*args)
self.assertEqual(result, "2001::b")
def test_find_previous_fifth_address_ipv6(self):
"""Get the previous fifth address in ipv6"""
args = ["", "2001::5", -10]
result = _ipmath(*args)
self.assertEqual(result, "2000:ffff:ffff:ffff:ffff:ffff:ffff:fffb")
def test_invalid_data(self):
"""Check passing invalid data"""
args = ["", "2001::1.999.0", 10]
kwargs = {}
with self.assertRaises(AnsibleFilterError) as error:
_ipmath(*args, **kwargs)
self.assertIn("You must pass a valid IP address", str(error.exception))