diff --git a/README.md b/README.md
index cf0e0aa..113081e 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.network_in_network](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.network_in_network_filter.rst)|This filter returns whether an address or a network passed as argument is in a network.
[ansible.utils.next_nth_usable](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.next_nth_usable_filter.rst)|This filter returns the next nth usable ip within a network described by value.
[ansible.utils.ipsubnet](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipsubnet_filter.rst)|This filter can be used to manipulate network subnets in several ways.
[ansible.utils.ipv6](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipv6_filter.rst)|To filter only Ipv6 addresses Ipv6 filter is used.
diff --git a/changelogs/fragments/add_network_in_network_filter.yaml b/changelogs/fragments/add_network_in_network_filter.yaml
new file mode 100644
index 0000000..775ac12
--- /dev/null
+++ b/changelogs/fragments/add_network_in_network_filter.yaml
@@ -0,0 +1,3 @@
+---
+minor_changes:
+ - Add network_in_network filter plugin.
diff --git a/docs/ansible.utils.network_in_network_filter.rst b/docs/ansible.utils.network_in_network_filter.rst
new file mode 100644
index 0000000..f7f72a0
--- /dev/null
+++ b/docs/ansible.utils.network_in_network_filter.rst
@@ -0,0 +1,161 @@
+.. _ansible.utils.network_in_network_filter:
+
+
+********************************
+ansible.utils.network_in_network
+********************************
+
+**This filter returns whether an address or a network passed as argument is in a network.**
+
+
+Version added: 2.5.0
+
+.. contents::
+ :local:
+ :depth: 1
+
+
+Synopsis
+--------
+- This filter returns whether an address or a network passed as argument is in a network.
+
+
+
+
+Parameters
+----------
+
+.. raw:: html
+
+
+
+ Parameter |
+ Choices/Defaults |
+ Configuration |
+ Comments |
+
+
+
+
+ test
+
+
+ string
+
+ |
+
+ |
+
+ |
+
+ The address or network to validate if it is within the range of 'value'.
+ |
+
+
+
+
+ value
+
+
+ string
+ / required
+
+ |
+
+ |
+
+ |
+
+ The network address or range to test against.
+ |
+
+
+
+
+
+
+
+Examples
+--------
+
+.. code-block:: yaml
+
+ #### examples
+ - name: Check ip address 1 is part of another network
+ debug:
+ msg: "{{ '192.168.0.0/24' | ansible.utils.network_in_network( '192.168.0.1' ) }}"
+
+ - name: Check ip address 2 is part of another network
+ debug:
+ msg: "{{ '192.168.0.0/24' | ansible.utils.network_in_network( '10.0.0.1' ) }}"
+
+ - name: Check in a network is part of another network.
+ debug:
+ msg: "{{ '192.168.0.0/16' | ansible.utils.network_in_network( '192.168.0.0/24' ) }}"
+
+ # TASK [Check ip address 1 is part of another network] ********************************************************
+ # task path: /Users/amhatre/ansible-collections/playbooks/test_network_in_network.yaml:7
+ # Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
+ # ok: [localhost] => {
+ # "msg": true
+ # }
+ #
+ # TASK [Check ip address 2 is part of another network] ********************************************************
+ # task path: /Users/amhatre/ansible-collections/playbooks/test_network_in_network.yaml:11
+ # Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
+ # ok: [localhost] => {
+ # "msg": false
+ # }
+ #
+ # TASK [Check in a network is part of another network.] *******************************************************
+ # task path: /Users/amhatre/ansible-collections/playbooks/test_network_in_network.yaml:15
+ # Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
+ # ok: [localhost] => {
+ # "msg": true
+ # }
+
+
+
+Return Values
+-------------
+Common return values are documented `here `_, the following are the fields unique to this filter:
+
+.. raw:: html
+
+
+
+ Key |
+ Returned |
+ Description |
+
+
+
+
+ data
+
+
+ boolean
+
+ |
+ |
+
+ Returns whether an address or a network passed as argument is in a 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/network_in_network.py b/plugins/filter/network_in_network.py
new file mode 100644
index 0000000..b071162
--- /dev/null
+++ b/plugins/filter/network_in_network.py
@@ -0,0 +1,165 @@
+# -*- 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: network_in_network
+"""
+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,
+ _address_normalizer,
+ _range_checker,
+)
+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: network_in_network
+ author: Ashwini Mhatre (@amhatre)
+ version_added: "2.5.0"
+ short_description: This filter returns whether an address or a network passed as argument is in a network.
+ description:
+ - This filter returns whether an address or a network passed as argument is in a network.
+ options:
+ value:
+ description: The network address or range to test against.
+ type: str
+ required: True
+ test:
+ description: The address or network to validate if it is within the range of 'value'.
+ type: str
+ notes:
+"""
+
+EXAMPLES = r"""
+#### examples
+- name: Check ip address 1 is part of another network
+ debug:
+ msg: "{{ '192.168.0.0/24' | ansible.utils.network_in_network( '192.168.0.1' ) }}"
+
+- name: Check ip address 2 is part of another network
+ debug:
+ msg: "{{ '192.168.0.0/24' | ansible.utils.network_in_network( '10.0.0.1' ) }}"
+
+- name: Check in a network is part of another network.
+ debug:
+ msg: "{{ '192.168.0.0/16' | ansible.utils.network_in_network( '192.168.0.0/24' ) }}"
+
+# TASK [Check ip address 1 is part of another network] ********************************************************
+# task path: /Users/amhatre/ansible-collections/playbooks/test_network_in_network.yaml:7
+# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
+# ok: [localhost] => {
+# "msg": true
+# }
+#
+# TASK [Check ip address 2 is part of another network] ********************************************************
+# task path: /Users/amhatre/ansible-collections/playbooks/test_network_in_network.yaml:11
+# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
+# ok: [localhost] => {
+# "msg": false
+# }
+#
+# TASK [Check in a network is part of another network.] *******************************************************
+# task path: /Users/amhatre/ansible-collections/playbooks/test_network_in_network.yaml:15
+# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
+# ok: [localhost] => {
+# "msg": true
+# }
+
+
+"""
+
+RETURN = """
+ data:
+ type: bool
+ description:
+ - Returns whether an address or a network passed as argument is in a network.
+
+"""
+
+
+@pass_environment
+def _network_in_network(*args, **kwargs):
+ """This filter returns whether an address or a network passed as argument is in a network."""
+ keys = ["value", "test"]
+ data = dict(zip(keys, args[1:]))
+ data.update(kwargs)
+ aav = AnsibleArgSpecValidator(
+ data=data, schema=DOCUMENTATION, name="network_in_network"
+ )
+ valid, errors, updated_data = aav.validate()
+ if not valid:
+ raise AnsibleFilterError(errors)
+ return network_in_network(**updated_data)
+
+
+def network_in_network(value, test):
+ """
+ Checks whether the 'test' address or addresses are in 'value', including broadcast and network
+ :param: value: The network address or range to test against.
+ :param test: The address or network to validate if it is within the range of 'value'.
+ :return: bool
+ """
+ # normalize value and test variables into an ipaddr
+ v = _address_normalizer(value)
+ w = _address_normalizer(test)
+
+ # get first and last addresses as integers to compare value and test; or cathes value when case is /32
+ v_first = ipaddr(ipaddr(v, "network") or ipaddr(v, "address"), "int")
+ v_last = ipaddr(ipaddr(v, "broadcast") or ipaddr(v, "address"), "int")
+ w_first = ipaddr(ipaddr(w, "network") or ipaddr(w, "address"), "int")
+ w_last = ipaddr(ipaddr(w, "broadcast") or ipaddr(w, "address"), "int")
+
+ if _range_checker(w_first, v_first, v_last) and _range_checker(
+ w_last, v_first, v_last
+ ):
+ return True
+ else:
+ return False
+
+
+class FilterModule(object):
+ """IP address and network manipulation filters
+ """
+
+ filter_map = {
+ # IP addresses and networks
+ "network_in_network": _network_in_network
+ }
+
+ 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/tests/integration/targets/utils_ipaddr_filter/tasks/network_in_network.yaml b/tests/integration/targets/utils_ipaddr_filter/tasks/network_in_network.yaml
new file mode 100644
index 0000000..15301f6
--- /dev/null
+++ b/tests/integration/targets/utils_ipaddr_filter/tasks/network_in_network.yaml
@@ -0,0 +1,25 @@
+---
+- name: network_in_network filter test1
+ ansible.builtin.set_fact:
+ result1: "{{ '192.168.0.0/24'|ansible.utils.network_in_network('192.168.0.1') }}"
+
+- name: Assert result for network_in_network.
+ assert:
+ that: "{{ result1 == True }}"
+
+- name: network_in_network filter test2
+ ansible.builtin.set_fact:
+ result1: "{{ '192.168.0.0/24'|ansible.utils.network_in_network('10.0.0.1') }}"
+
+- name: Assert result for network_in_network
+ assert:
+ that: "{{ result1 == False }}"
+
+
+- name: network_in_network filter test3
+ ansible.builtin.set_fact:
+ result1: "{{ '192.168.0.0/16'|ansible.utils.network_in_network('192.168.0.0/24') }}"
+
+- name: Assert result for network_in_network
+ assert:
+ that: "{{ result1 == True }}"
diff --git a/tests/unit/plugins/filter/test_network_in_network.py b/tests/unit/plugins/filter/test_network_in_network.py
new file mode 100644
index 0000000..7f4919d
--- /dev/null
+++ b/tests/unit/plugins/filter/test_network_in_network.py
@@ -0,0 +1,40 @@
+# -*- 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 network_in_network filter plugin
+"""
+
+from __future__ import absolute_import, division, print_function
+
+__metaclass__ = type
+
+import unittest
+from ansible_collections.ansible.utils.plugins.filter.network_in_network import (
+ _network_in_network,
+)
+
+
+class Test_network_in_network(unittest.TestCase):
+ def setUp(self):
+ pass
+
+ def test_network_in_network_filter_1(self):
+ """network_in_network filter"""
+ args = ["", "192.168.0.0/24", "192.168.0.1"]
+ result = _network_in_network(*args)
+ self.assertEqual(result, True)
+
+ def test_network_in_network_filter_2(self):
+ """network_in_network filter"""
+ args = ["", "192.168.0.0/24", "10.0.0.1"]
+ result = _network_in_network(*args)
+ self.assertEqual(result, False)
+
+ def test_network_in_network_filter_3(self):
+ """network_in_network filter"""
+ args = ["", "192.168.0.0/16", "192.168.0.0/24"]
+ result = _network_in_network(*args)
+ self.assertEqual(result, True)