Add network_in_network filter plugin (#125)
Add network_in_network filter plugin SUMMARY ISSUE TYPE New Module Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Nathaniel Case <this.is@nathanielca.se> Reviewed-by: Nilashish Chakraborty <nilashishchakraborty8@gmail.com> Reviewed-by: None <None>pull/126/head^2
parent
1d697455b6
commit
234b0a51cf
|
@ -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.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.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.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.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.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.
|
[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.
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
---
|
||||||
|
minor_changes:
|
||||||
|
- Add network_in_network filter plugin.
|
|
@ -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
|
||||||
|
|
||||||
|
<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>test</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>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<div>The address or network to validate if it is within the range of 'value'.</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>The network address or range to test against.</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<br/>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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 <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">boolean</span>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td></td>
|
||||||
|
<td>
|
||||||
|
<div>Returns whether an address or a network passed as argument is in a network.</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.
|
|
@ -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
|
||||||
|
)
|
|
@ -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 }}"
|
|
@ -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)
|
Loading…
Reference in New Issue