Added 'in_any_network', 'in_network', 'in_one_network' test plugins (#66)

Added 'in_any_network', 'in_network', 'in_one_network' test plugins

Reviewed-by: https://github.com/apps/ansible-zuul
pull/68/head
Priyam Sahoo 2021-05-03 19:52:35 +05:30 committed by GitHub
parent c32e3960ff
commit c09bbacdbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 1194 additions and 0 deletions

View File

@ -47,6 +47,9 @@ Name | Description
### Test plugins
Name | Description
--- | ---
[ansible.utils.in_any_network](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.in_any_network_test.rst)|Test if Test if an IP or network falls in any network
[ansible.utils.in_network](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.in_network_test.rst)|Test if IP address falls in the network
[ansible.utils.in_one_network](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.in_one_network_test.rst)|Test if IP address belongs in any one of the networks in the list
[ansible.utils.validate](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.validate_test.rst)|Validate data with provided criteria
<!--end collection content-->

View File

@ -0,0 +1,3 @@
---
minor_changes:
- Add in_any_network, in_network, in_one_network test plugins

View File

@ -0,0 +1,170 @@
.. _ansible.utils.in_any_network_test:
****************************
ansible.utils.in_any_network
****************************
**Test if Test if an IP or network falls in any network**
Version added: 2.2.0
.. contents::
:local:
:depth: 1
Synopsis
--------
- This plugin checks if the provided IP or network address belongs to the provided list network addresses
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>ip</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>A string that represents an IP address of a host or network</div>
<div>{&#x27;For example&#x27;: &#x27;10.1.1.1&#x27;}</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>networks</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">list</span>
/ <span style="color: red">required</span>
</div>
</td>
<td>
</td>
<td>
</td>
<td>
<div>A list of string and each string represents a network address in CIDR form</div>
<div>{&#x27;For example&#x27;: [&#x27;10.0.0.0/8&#x27;, &#x27;192.168.1.0/24&#x27;]}</div>
</td>
</tr>
</table>
<br/>
Examples
--------
.. code-block:: yaml
#### Simple examples
- name: Set network list
ansible.builtin.set_fact:
networks:
- "10.0.0.0/8"
- "192.168.1.0/24"
- name: Check if 10.1.1.1 is in the provided network list
ansible.builtin.set_fact:
data: "{{ '10.1.1.1' is ansible.utils.in_any_network networks }}"
# TASK [Check if 10.1.1.1 is in the provided network list] **************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Set network list
ansible.builtin.set_fact:
networks:
- "10.0.0.0/8"
- "192.168.1.0/24"
- "172.16.0.0/16"
- name: Check if 8.8.8.8 is not in the provided network list
ansible.builtin.set_fact:
data: "{{ '8.8.8.8' is not ansible.utils.in_any_network networks }}"
# TASK [Check if 8.8.8.8 is not in the provided network list] ************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
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 test:
.. 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">-</span>
</div>
</td>
<td></td>
<td>
<div>If jinja test satisfies plugin expression <code>true</code></div>
<div>If jinja test does not satisfy plugin expression <code>false</code></div>
<br/>
</td>
</tr>
</table>
<br/><br/>
Status
------
Authors
~~~~~~~
- Priyam Sahoo (@priyamsahoo)
.. 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.

View File

@ -0,0 +1,183 @@
.. _ansible.utils.in_network_test:
************************
ansible.utils.in_network
************************
**Test if IP address falls in the network**
Version added: 2.2.0
.. contents::
:local:
:depth: 1
Synopsis
--------
- This plugin checks if the provided IP address belongs to the provided 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>ip</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>A string that represents an IP address</div>
<div>{&#x27;For example&#x27;: &#x27;10.1.1.1&#x27;}</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>network</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>A string that represents the network address in CIDR form</div>
<div>{&#x27;For example&#x27;: &#x27;10.0.0.0/8&#x27;}</div>
</td>
</tr>
</table>
<br/>
Examples
--------
.. code-block:: yaml
#### Simple examples
- name: Check if 10.1.1.1 is in 10.0.0.0/8
ansible.builtin.set_fact:
data: "{{ '10.1.1.1' is ansible.utils.in_network '10.0.0.0/8' }}"
# TASK [Check if 10.1.1.1 is in 10.0.0.0/8] ***********************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 10.1.1.1 is not in 192.168.1.0/24
ansible.builtin.set_fact:
data: "{{ '10.1.1.1' is not ansible.utils.in_network '192.168.1.0/24' }}"
# TASK [Check if 10.1.1.1 is not in 192.168.1.0/24] ****************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 2001:db8:a::123 is in 2001:db8:a::/64
ansible.builtin.set_fact:
data: "{{ '2001:db8:a::123' is ansible.utils.in_network '2001:db8:a::/64' }}"
# TASK [Check if 2001:db8:a::123 is in 2001:db8:a::/64] ****************************
# task path: /home/prsahoo/playbooks/collections/localhost_test/utils_in_network.yml:16
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 2001:db8:a::123 is not in 10.0.0.0/8
ansible.builtin.set_fact:
data: "{{ '2001:db8:a::123' is not ansible.utils.in_network '10.0.0.0/8' }}"
# TASK [Check if 2001:db8:a::123 is not in 10.0.0.0/8] *********************************
# task path: /home/prsahoo/playbooks/collections/localhost_test/utils_in_network.yml:20
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
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 test:
.. 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">-</span>
</div>
</td>
<td></td>
<td>
<div>If jinja test satisfies plugin expression <code>true</code></div>
<div>If jinja test does not satisfy plugin expression <code>false</code></div>
<br/>
</td>
</tr>
</table>
<br/><br/>
Status
------
Authors
~~~~~~~
- Priyam Sahoo (@priyamsahoo)
.. 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.

View File

@ -0,0 +1,168 @@
.. _ansible.utils.in_one_network_test:
****************************
ansible.utils.in_one_network
****************************
**Test if IP address belongs in any one of the networks in the list**
Version added: 2.2.0
.. contents::
:local:
:depth: 1
Synopsis
--------
- This plugin checks if the provided IP address belongs to the provided list network addresses
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>ip</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>A string that represents an IP address</div>
<div>{&#x27;For example&#x27;: &#x27;10.1.1.1&#x27;}</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>networks</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">list</span>
/ <span style="color: red">required</span>
</div>
</td>
<td>
</td>
<td>
</td>
<td>
<div>A list of string and each string represents a network address in CIDR form</div>
<div>{&#x27;For example&#x27;: [&#x27;10.0.0.0/8&#x27;, &#x27;192.168.1.0/24&#x27;]}</div>
</td>
</tr>
</table>
<br/>
Examples
--------
.. code-block:: yaml
#### Simple examples
- name: Set network list
ansible.builtin.set_fact:
networks:
- "10.0.0.0/8"
- "192.168.1.0/24"
- name: Check if 10.1.1.1 is in the provided network list
ansible.builtin.set_fact:
data: "{{ '10.1.1.1' is ansible.utils.in_one_network networks }}"
# TASK [Check if 10.1.1.1 is in the provided network list] **********************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
- name: Set network list
ansible.builtin.set_fact:
networks:
- "10.0.0.0/8"
- "10.1.1.0/24"
- name: Check if 10.1.1.1 is not in the provided network list
ansible.builtin.set_fact:
data: "{{ '10.1.1.1' is not ansible.utils.in_one_network networks }}"
# TASK [Check if 10.1.1.1 is in not the provided network list] ************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
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 test:
.. 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">-</span>
</div>
</td>
<td></td>
<td>
<div>If jinja test satisfies plugin expression <code>true</code></div>
<div>If jinja test does not satisfy plugin expression <code>false</code></div>
<br/>
</td>
</tr>
</table>
<br/><br/>
Status
------
Authors
~~~~~~~
- Priyam Sahoo (@priyamsahoo)
.. 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.

View File

@ -0,0 +1,86 @@
# -*- 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)
"""
The utils file for all netaddr tests
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
from ansible.errors import AnsibleError
from ansible.module_utils.basic import missing_required_lib
from ansible.module_utils.six import ensure_text
from functools import wraps
from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import (
check_argspec,
)
try:
import ipaddress
HAS_IPADDRESS = True
except ImportError:
HAS_IPADDRESS = False
def ip_network(ip):
""" PY2 compat shim, PY2 requires unicode
"""
if not HAS_IPADDRESS:
raise AnsibleError(missing_required_lib("ipaddress"))
return ipaddress.ip_network(ensure_text(ip))
def ip_address(ip):
""" PY2 compat shim, PY2 requires unicode
"""
if not HAS_IPADDRESS:
raise AnsibleError(missing_required_lib("ipaddress"))
return ipaddress.ip_address(ensure_text(ip))
def _need_ipaddress(func):
@wraps(func)
def wrapper(*args, **kwargs):
if not HAS_IPADDRESS:
raise AnsibleError(missing_required_lib("ipaddress"))
return func(*args, **kwargs)
return wrapper
def _is_subnet_of(network_a, network_b):
try:
if network_a._version != network_b._version:
return False
return (
network_b.network_address <= network_a.network_address
and network_b.broadcast_address >= network_a.broadcast_address
)
except Exception:
return False
def _validate_args(plugin, doc, params):
""" argspec validator utility function
"""
valid, argspec_result, updated_params = check_argspec(
doc, plugin + " test", **params
)
if not valid:
raise AnsibleError(
"{argspec_result} with errors: {argspec_errors}".format(
argspec_result=argspec_result.get("msg"),
argspec_errors=argspec_result.get("errors"),
)
)

View File

@ -0,0 +1,112 @@
# -*- 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)
"""
Test plugin file for netaddr tests: in_any_network
"""
from __future__ import absolute_import, division, print_function
from ansible_collections.ansible.utils.plugins.test.in_network import (
_in_network,
)
from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddress_utils import (
_validate_args,
)
__metaclass__ = type
DOCUMENTATION = """
name: in_any_network
author: Priyam Sahoo (@priyamsahoo)
version_added: "2.2.0"
short_description: Test if Test if an IP or network falls in any network
description:
- This plugin checks if the provided IP or network address belongs to the provided list network addresses
options:
ip:
description:
- A string that represents an IP address of a host or network
- For example: "10.1.1.1"
type: str
required: True
networks:
description:
- A list of string and each string represents a network address in CIDR form
- For example: ['10.0.0.0/8', '192.168.1.0/24']
type: list
required: True
notes:
"""
EXAMPLES = r"""
#### Simple examples
- name: Set network list
ansible.builtin.set_fact:
networks:
- "10.0.0.0/8"
- "192.168.1.0/24"
- name: Check if 10.1.1.1 is in the provided network list
ansible.builtin.set_fact:
data: "{{ '10.1.1.1' is ansible.utils.in_any_network networks }}"
# TASK [Check if 10.1.1.1 is in the provided network list] **************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Set network list
ansible.builtin.set_fact:
networks:
- "10.0.0.0/8"
- "192.168.1.0/24"
- "172.16.0.0/16"
- name: Check if 8.8.8.8 is not in the provided network list
ansible.builtin.set_fact:
data: "{{ '8.8.8.8' is not ansible.utils.in_any_network networks }}"
# TASK [Check if 8.8.8.8 is not in the provided network list] ************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
"""
RETURN = """
data:
description:
- If jinja test satisfies plugin expression C(true)
- If jinja test does not satisfy plugin expression C(false)
"""
def _in_any_network(ip, networks):
"""Test if an IP or network is in any network"""
params = {"ip": ip, "networks": networks}
_validate_args("in_any_network", DOCUMENTATION, params)
bools = [_in_network(ip, network) for network in networks]
if True in bools:
return True
return False
class TestModule(object):
""" network jinja test"""
test_map = {"in_any_network": _in_any_network}
def tests(self):
return self.test_map

128
plugins/test/in_network.py Normal file
View File

@ -0,0 +1,128 @@
# -*- 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)
"""
Test plugin file for netaddr tests: in_network
"""
from __future__ import absolute_import, division, print_function
from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddress_utils import (
ip_network,
_is_subnet_of,
_need_ipaddress,
)
from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddress_utils import (
_validate_args,
)
__metaclass__ = type
DOCUMENTATION = """
name: in_network
author: Priyam Sahoo (@priyamsahoo)
version_added: "2.2.0"
short_description: Test if IP address falls in the network
description:
- This plugin checks if the provided IP address belongs to the provided network
options:
ip:
description:
- A string that represents an IP address
- For example: "10.1.1.1"
type: str
required: True
network:
description:
- A string that represents the network address in CIDR form
- For example: "10.0.0.0/8"
type: str
required: True
notes:
"""
EXAMPLES = r"""
#### Simple examples
- name: Check if 10.1.1.1 is in 10.0.0.0/8
ansible.builtin.set_fact:
data: "{{ '10.1.1.1' is ansible.utils.in_network '10.0.0.0/8' }}"
# TASK [Check if 10.1.1.1 is in 10.0.0.0/8] ***********************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 10.1.1.1 is not in 192.168.1.0/24
ansible.builtin.set_fact:
data: "{{ '10.1.1.1' is not ansible.utils.in_network '192.168.1.0/24' }}"
# TASK [Check if 10.1.1.1 is not in 192.168.1.0/24] ****************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 2001:db8:a::123 is in 2001:db8:a::/64
ansible.builtin.set_fact:
data: "{{ '2001:db8:a::123' is ansible.utils.in_network '2001:db8:a::/64' }}"
# TASK [Check if 2001:db8:a::123 is in 2001:db8:a::/64] ****************************
# task path: /home/prsahoo/playbooks/collections/localhost_test/utils_in_network.yml:16
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 2001:db8:a::123 is not in 10.0.0.0/8
ansible.builtin.set_fact:
data: "{{ '2001:db8:a::123' is not ansible.utils.in_network '10.0.0.0/8' }}"
# TASK [Check if 2001:db8:a::123 is not in 10.0.0.0/8] *********************************
# task path: /home/prsahoo/playbooks/collections/localhost_test/utils_in_network.yml:20
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
"""
RETURN = """
data:
description:
- If jinja test satisfies plugin expression C(true)
- If jinja test does not satisfy plugin expression C(false)
"""
@_need_ipaddress
def _in_network(ip, network):
"""Test if an address or network is in a network"""
params = {"ip": ip, "network": network}
_validate_args("in_network", DOCUMENTATION, params)
try:
return _is_subnet_of(ip_network(ip), ip_network(network))
except Exception:
return False
class TestModule(object):
""" network jinja test"""
test_map = {"in_network": _in_network}
def tests(self):
return self.test_map

View File

@ -0,0 +1,111 @@
# -*- 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)
"""
Test plugin file for netaddr tests: in_one_network
"""
from __future__ import absolute_import, division, print_function
from ansible_collections.ansible.utils.plugins.test.in_network import (
_in_network,
)
from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddress_utils import (
_validate_args,
)
__metaclass__ = type
DOCUMENTATION = """
name: in_one_network
author: Priyam Sahoo (@priyamsahoo)
version_added: "2.2.0"
short_description: Test if IP address belongs in any one of the networks in the list
description:
- This plugin checks if the provided IP address belongs to the provided list network addresses
options:
ip:
description:
- A string that represents an IP address
- For example: "10.1.1.1"
type: str
required: True
networks:
description:
- A list of string and each string represents a network address in CIDR form
- For example: ['10.0.0.0/8', '192.168.1.0/24']
type: list
required: True
notes:
"""
EXAMPLES = r"""
#### Simple examples
- name: Set network list
ansible.builtin.set_fact:
networks:
- "10.0.0.0/8"
- "192.168.1.0/24"
- name: Check if 10.1.1.1 is in the provided network list
ansible.builtin.set_fact:
data: "{{ '10.1.1.1' is ansible.utils.in_one_network networks }}"
# TASK [Check if 10.1.1.1 is in the provided network list] **********************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
- name: Set network list
ansible.builtin.set_fact:
networks:
- "10.0.0.0/8"
- "10.1.1.0/24"
- name: Check if 10.1.1.1 is not in the provided network list
ansible.builtin.set_fact:
data: "{{ '10.1.1.1' is not ansible.utils.in_one_network networks }}"
# TASK [Check if 10.1.1.1 is in not the provided network list] ************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
"""
RETURN = """
data:
description:
- If jinja test satisfies plugin expression C(true)
- If jinja test does not satisfy plugin expression C(false)
"""
def _in_one_network(ip, networks):
"""Test if an IP or network is in one network"""
params = {"ip": ip, "networks": networks}
_validate_args("in_one_network", DOCUMENTATION, params)
bools = [_in_network(ip, network) for network in networks]
if bools.count(True) == 1:
return True
return False
class TestModule(object):
""" network jinja test"""
test_map = {"in_one_network": _in_one_network}
def tests(self):
return self.test_map

View File

@ -1,6 +1,7 @@
black==19.3b0 ; python_version > '3.5'
coverage==4.5.4
flake8
ipaddress ; python_version < '3.0'
mock ; python_version < '3.5'
pytest-xdist
yamllint

View File

@ -0,0 +1,17 @@
---
- name: "in_any_network: :Check if 10.1.1.1 is in the provided network list"
assert:
that:
- "{{ '10.1.1.1' is ansible.utils.in_any_network networks }}"
- name: "in_any_network: Check if 8.8.8.8 is not in the provided network list"
assert:
that: "{{ '8.8.8.8' is not ansible.utils.in_any_network networks }}"
- name: "in_any_network: Test invalidness"
ansible.builtin.set_fact:
criteria_check: "{{ '192.168.4.56' is ansible.utils.in_any_network networks }}"
- name: "in_any_network: Assert invalidness"
assert:
that: "{{ criteria_check == false }}"

View File

@ -0,0 +1,33 @@
---
- name: "in_network: Check if 10.1.1.1 is in 10.0.0.0/8"
assert:
that: "{{ '10.1.1.1' is ansible.utils.in_network '10.0.0.0/8' }}"
- name: "in_network: Check if 10.1.1.1 is not in 192.168.1.0/24"
assert:
that: "{{ '10.1.1.1' is not ansible.utils.in_network '192.168.1.0/24' }}"
- name: "in_network: Test invalidness"
ansible.builtin.set_fact:
criteria_check1: "{{ '8.8.8.8' is ansible.utils.in_network '10.0.0.0/8' }}"
- name: "in_network: Assert invalidness"
assert:
that: "{{ criteria_check1 == false }}"
- name: "in_network: Check if 2001:db8:a::123 is in 2001:db8:a::/64"
assert:
that: "{{ '2001:db8:a::123' is ansible.utils.in_network '2001:db8:a::/64' }}"
- name: "in_network: Check if 2001:db8:a::123 is not in 10.0.0.0/8"
assert:
that: "{{ '2001:db8:a::123' is not ansible.utils.in_network '10.0.0.0/8' }}"
- name: "in_network: Test invalidness"
ansible.builtin.set_fact:
criteria_check2: "{{ '2001:db8:a::123' is not ansible.utils.in_network '2001:db8:a::/64' }}"
register: result2
- name: "in_network: Assert invalidness"
assert:
that: "{{ criteria_check2 == false }}"

View File

@ -0,0 +1,16 @@
---
- name: "in_one_network: Check if 10.1.1.1 is in the provided network list"
assert:
that: "{{ '10.1.1.1' is ansible.utils.in_one_network networks }}"
- name: "in_one_network: Check if 192.168.3.5 is not in the provided network list"
assert:
that: "{{ '192.168.3.5' is not ansible.utils.in_one_network networks }}"
- name: "in_one_network: Test invalidness"
ansible.builtin.set_fact:
criteria_check: "{{ '172.168.2.16' is ansible.utils.in_one_network networks }}"
- name: "in_one_network: Assert invalidness"
assert:
that: "{{ criteria_check == false }}"

View File

@ -0,0 +1,13 @@
---
- name: Recursively find all test files
find:
file_type: file
paths: "{{ role_path }}/tasks/include"
recurse: true
use_regex: true
patterns:
- '^(?!_).+$'
register: found
- include: "{{ item.path }}"
loop: "{{ found.files }}"

View File

@ -0,0 +1,5 @@
---
networks:
- "10.0.0.0/8"
- "192.168.1.0/24"
- "172.16.0.0/16"

View File

@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Red Hat
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
"""
Unit test file for netaddr test plugin: in_any_network
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import unittest
from ansible.errors import AnsibleError
from ansible_collections.ansible.utils.plugins.test.in_any_network import (
_in_any_network,
)
class TestInAnyNetwork(unittest.TestCase):
def setUp(self):
pass
def test_invalid_data(self):
"""Check passing invalid argspec"""
# invalid argument
with self.assertRaises(AnsibleError) as error:
_in_any_network(
ip="10.1.1.1",
networks={
"name": "networks",
"value": ["10.0.0.0/8", "192.168.1.0/24"],
},
)
self.assertIn("unable to convert to list", str(error.exception))
def test_valid_data(self):
"""Check passing valid data as per criteria"""
result = _in_any_network(
ip="10.1.1.1", networks=["10.0.0.0/8", "192.168.1.0/24"]
)
self.assertEqual(result, True)
result = _in_any_network(
ip="8.8.8.8", networks=["10.0.0.0/8", "192.168.1.0/24"]
)
self.assertEqual(result, False)

View File

@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Red Hat
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
"""
Unit test file for netaddr test plugin: in_network
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import unittest
from ansible_collections.ansible.utils.plugins.test.in_network import (
_in_network,
)
class TestInNetwork(unittest.TestCase):
def setUp(self):
pass
def test_invalid_data(self):
"""Check passing invalid argspec"""
# invalid argument
with self.assertRaises(TypeError) as error:
_in_network(ip="10.1.1.1")
self.assertIn("argument", str(error.exception))
def test_valid_data(self):
"""Check passing valid data as per criteria"""
result = _in_network(ip="10.1.1.1", network="10.0.0.0/8")
self.assertEqual(result, True)
result = _in_network(ip="8.8.8.8", network="192.168.1.0/24")
self.assertEqual(result, False)
result = _in_network(ip="2001:db8:a::123", network="2001:db8:a::/64")
self.assertEqual(result, True)
result = _in_network(ip="2001:db8:a::123", network="10.0.0.0/8")
self.assertEqual(result, False)

View File

@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Red Hat
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
"""
Unit test file for netaddr test plugin: in_one_network
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import unittest
from ansible.errors import AnsibleError
from ansible_collections.ansible.utils.plugins.test.in_one_network import (
_in_one_network,
)
class TestInOneNetwork(unittest.TestCase):
def setUp(self):
pass
def test_invalid_data(self):
"""Check passing invalid argspec"""
# invalid argument
with self.assertRaises(AnsibleError) as error:
_in_one_network(
ip="10.1.1.1",
networks={
"name": "networks",
"value": ["10.0.0.0/8", "192.168.1.0/24"],
},
)
self.assertIn("unable to convert to list", str(error.exception))
def test_valid_data(self):
"""Check passing valid data as per criteria"""
result = _in_one_network(
ip="10.1.1.1", networks=["10.0.0.0/8", "192.168.1.0/24"]
)
self.assertEqual(result, True)
result = _in_one_network(
ip="8.8.8.8", networks=["10.0.0.0/8", "10.1.1.0/24"]
)
self.assertEqual(result, False)