diff --git a/README.md b/README.md
index e7bc87b..6f028c8 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.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.
diff --git a/changelogs/fragments/add_ipmath_filter.yaml b/changelogs/fragments/add_ipmath_filter.yaml
new file mode 100644
index 0000000..a58df7a
--- /dev/null
+++ b/changelogs/fragments/add_ipmath_filter.yaml
@@ -0,0 +1,3 @@
+---
+minor_changes:
+ - Add ipmath filter plugin.
diff --git a/docs/ansible.utils.ipmath_filter.rst b/docs/ansible.utils.ipmath_filter.rst
new file mode 100644
index 0000000..468ac6b
--- /dev/null
+++ b/docs/ansible.utils.ipmath_filter.rst
@@ -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
+
+
+
+ Parameter |
+ Choices/Defaults |
+ Configuration |
+ Comments |
+
+
+
+
+ amount
+
+
+ integer
+
+ |
+
+ |
+
+ |
+
+ integer for arithmetic. Example -1,2,3
+ |
+
+
+
+
+ value
+
+
+ string
+ / required
+
+ |
+
+ |
+
+ |
+
+ list of subnets or individual address or any other values input for ipaddr plugin
+ |
+
+
+
+
+
+
+
+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 `_, the following are the fields unique to this filter:
+
+.. raw:: html
+
+
+
+ Key |
+ Returned |
+ Description |
+
+
+
+
+ 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/ipmath.py b/plugins/filter/ipmath.py
new file mode 100644
index 0000000..3cf50d3
--- /dev/null
+++ b/plugins/filter/ipmath.py
@@ -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
+ )
diff --git a/tests/integration/targets/utils_ipaddr_filter/tasks/ipmath.yaml b/tests/integration/targets/utils_ipaddr_filter/tasks/ipmath.yaml
new file mode 100644
index 0000000..f976b32
--- /dev/null
+++ b/tests/integration/targets/utils_ipaddr_filter/tasks/ipmath.yaml
@@ -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' }}"
diff --git a/tests/unit/plugins/filter/test_ipmath.py b/tests/unit/plugins/filter/test_ipmath.py
new file mode 100644
index 0000000..a6248f6
--- /dev/null
+++ b/tests/unit/plugins/filter/test_ipmath.py
@@ -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))