Added ipv4 specefic test plugins (#69)

Added ipv4 specefic test plugins

Reviewed-by: https://github.com/apps/ansible-zuul
pull/75/head
Priyam Sahoo 2021-05-08 00:00:00 +05:30 committed by GitHub
parent 1c9866e8d8
commit e92cc6e78d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 1285 additions and 0 deletions

View File

@ -52,6 +52,10 @@ Name | Description
[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.ip](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ip_test.rst)|Test if something in an IP address or network
[ansible.utils.ip_address](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ip_address_test.rst)|Test if something in an IP address
[ansible.utils.ipv4](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipv4_test.rst)|Test if something is an IPv4 address or network
[ansible.utils.ipv4_address](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipv4_address_test.rst)|Test if something is an IPv4 address
[ansible.utils.ipv4_hostmask](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipv4_hostmask_test.rst)|Test if an address is a valid hostmask
[ansible.utils.ipv4_netmask](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipv4_netmask_test.rst)|Test if an address is a valid netmask
[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 ipv4, ipv4_address, ipv4_hostmask, ipv4_netmask test plugins

View File

@ -0,0 +1,150 @@
.. _ansible.utils.ipv4_address_test:
**************************
ansible.utils.ipv4_address
**************************
**Test if something is an IPv4 address**
Version added: 2.2.0
.. contents::
:local:
:depth: 1
Synopsis
--------
- This plugin checks if the provided value is a valid host IP address with IPv4 addressing scheme
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 the value against which the test is going to be performed</div>
<div>{&#x27;For example&#x27;: [&#x27;10.1.1.1&#x27;, &#x27;10.0.0.0/8&#x27;, &#x27;fe80::216:3eff:fee4:16f3&#x27;]}</div>
</td>
</tr>
</table>
<br/>
Examples
--------
.. code-block:: yaml
#### Simple examples
- name: Check if 10.1.1.1 is a valid IPv4 address
ansible.builtin.set_fact:
data: "{{ '10.1.1.1' is ansible.utils.ipv4_address }}"
# TASK [Check if 10.1.1.1 is a valid IPv4 address] *************************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 10.1.1.1/31 is not a valid IPv4 address
ansible.builtin.set_fact:
data: "{{ '10.1.1.1/31' is not ansible.utils.ipv4_address }}"
# TASK [Check if 10.1.1.1/31 is a valid IPv4 address] **********************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if fe80::216:3eff:fee4:16f3 is not a valid IPv4 address
ansible.builtin.set_fact:
data: "{{ 'fe80::216:3eff:fee4:16f3' is not ansible.utils.ipv4_address }}"
# TASK [Check if fe80::216:3eff:fee4:16f3 is not a valid IPv4 address] *********
# 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,138 @@
.. _ansible.utils.ipv4_hostmask_test:
***************************
ansible.utils.ipv4_hostmask
***************************
**Test if an address is a valid hostmask**
Version added: 2.2.0
.. contents::
:local:
:depth: 1
Synopsis
--------
- This plugin checks if the provided ip address is a IPv4 hostmask or not
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 the value against which the test is going to be performed</div>
<div>{&#x27;For example&#x27;: [&#x27;0.1.255.255&#x27;, &#x27;255.255.255.0&#x27;]}</div>
</td>
</tr>
</table>
<br/>
Examples
--------
.. code-block:: yaml
#### Simple examples
- name: Check if 0.0.0.255 is a hostmask
ansible.builtin.set_fact:
data: "{{ '0.0.0.255' is ansible.utils.ipv4_hostmask }}"
# TASK [Check if 0.0.0.255 is a hostmask] ***********************************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 255.255.255.0 is not a hostmask
ansible.builtin.set_fact:
data: "{{ '255.255.255.0' is not ansible.utils.ipv4_hostmask }}"
# TASK [Check if 255.255.255.0 is a hostmask] *********************************
# 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,150 @@
.. _ansible.utils.ipv4_netmask_test:
**************************
ansible.utils.ipv4_netmask
**************************
**Test if an address is a valid netmask**
Version added: 2.2.0
.. contents::
:local:
:depth: 1
Synopsis
--------
- This plugin checks if the provided ip address is a valid IPv4 netmask or not
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>mask</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 value against which the test is going to be performed</div>
<div>{&#x27;For example&#x27;: [&#x27;0.1.255.255&#x27;, &#x27;255.255.255.0&#x27;]}</div>
</td>
</tr>
</table>
<br/>
Examples
--------
.. code-block:: yaml
#### Simple examples
- name: Check if 255.255.255.0 is a netmask
ansible.builtin.set_fact:
data: "{{ '255.255.255.0' is ansible.utils.ipv4_netmask }}"
# TASK [Check if 255.255.255.0 is a netmask] *******************************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 255.255.255.128 is a netmask
ansible.builtin.set_fact:
data: "{{ '255.255.255.128' is ansible.utils.ipv4_netmask }}"
# TASK [Check if 255.255.255.128 is a netmask] *****************************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 255.255.255.127 is not a netmask
ansible.builtin.set_fact:
data: "{{ '255.255.255.127' is not ansible.utils.ipv4_netmask }}"
# TASK [Check if 255.255.255.127 is not a netmask] *************************************
# 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,150 @@
.. _ansible.utils.ipv4_test:
******************
ansible.utils.ipv4
******************
**Test if something is an IPv4 address or network**
Version added: 2.2.0
.. contents::
:local:
:depth: 1
Synopsis
--------
- This plugin checks if the provided value is a valid host or network IP address with IPv4 addressing scheme
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 the value against which the test is going to be performed</div>
<div>{&#x27;For example&#x27;: [&#x27;10.1.1.1&#x27;, &#x27;10.0.0.0/8&#x27;, &#x27;fe80::216:3eff:fee4:16f3&#x27;]}</div>
</td>
</tr>
</table>
<br/>
Examples
--------
.. code-block:: yaml
#### Simple examples
- name: Check if 10.0.0.0/8 is a valid IPv4 address
ansible.builtin.set_fact:
data: "{{ '10.0.0.0/8' is ansible.utils.ipv4 }}"
# TASK [Check if 10.0.0.0/8 is a valid IPv4 address] ***************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 192.168.1.250 is a valid IPv4 address
ansible.builtin.set_fact:
data: "{{ '192.168.1.250' is ansible.utils.ipv4 }}"
# TASK [Check if 192.168.1.250 is a valid IPv4 address] ********************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if fe80::216:3eff:fee4:16f3 is not a valid IPv4 address
ansible.builtin.set_fact:
data: "{{ 'fe80::216:3eff:fee4:16f3' is not ansible.utils.ipv4 }}"
# TASK [Check if fe80::216:3eff:fee4:16f3 is not a valid IPv4 address] *********
# 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.

108
plugins/test/ipv4.py Normal file
View File

@ -0,0 +1,108 @@
# -*- 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: ipv4
"""
from __future__ import absolute_import, division, print_function
from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddress_utils import (
ip_network,
_need_ipaddress,
_validate_args,
)
__metaclass__ = type
DOCUMENTATION = """
name: ipv4
author: Priyam Sahoo (@priyamsahoo)
version_added: "2.2.0"
short_description: Test if something is an IPv4 address or network
description:
- This plugin checks if the provided value is a valid host or network IP address with IPv4 addressing scheme
options:
ip:
description:
- A string that represents the value against which the test is going to be performed
- For example:
- "10.1.1.1"
- "10.0.0.0/8"
- "fe80::216:3eff:fee4:16f3"
type: str
required: True
notes:
"""
EXAMPLES = r"""
#### Simple examples
- name: Check if 10.0.0.0/8 is a valid IPv4 address
ansible.builtin.set_fact:
data: "{{ '10.0.0.0/8' is ansible.utils.ipv4 }}"
# TASK [Check if 10.0.0.0/8 is a valid IPv4 address] ***************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 192.168.1.250 is a valid IPv4 address
ansible.builtin.set_fact:
data: "{{ '192.168.1.250' is ansible.utils.ipv4 }}"
# TASK [Check if 192.168.1.250 is a valid IPv4 address] ********************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if fe80::216:3eff:fee4:16f3 is not a valid IPv4 address
ansible.builtin.set_fact:
data: "{{ 'fe80::216:3eff:fee4:16f3' is not ansible.utils.ipv4 }}"
# TASK [Check if fe80::216:3eff:fee4:16f3 is not a valid IPv4 address] *********
# 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 _ipv4(ip):
"""Test if something in an IPv4 address or network"""
params = {"ip": ip}
_validate_args("ipv4", DOCUMENTATION, params)
try:
return ip_network(ip).version == 4
except Exception:
return False
class TestModule(object):
""" network jinja test"""
test_map = {"ipv4": _ipv4}
def tests(self):
return self.test_map

View File

@ -0,0 +1,108 @@
# -*- 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: ipv4_address
"""
from __future__ import absolute_import, division, print_function
from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddress_utils import (
ip_address,
_need_ipaddress,
_validate_args,
)
__metaclass__ = type
DOCUMENTATION = """
name: ipv4_address
author: Priyam Sahoo (@priyamsahoo)
version_added: "2.2.0"
short_description: Test if something is an IPv4 address
description:
- This plugin checks if the provided value is a valid host IP address with IPv4 addressing scheme
options:
ip:
description:
- A string that represents the value against which the test is going to be performed
- For example:
- "10.1.1.1"
- "10.0.0.0/8"
- "fe80::216:3eff:fee4:16f3"
type: str
required: True
notes:
"""
EXAMPLES = r"""
#### Simple examples
- name: Check if 10.1.1.1 is a valid IPv4 address
ansible.builtin.set_fact:
data: "{{ '10.1.1.1' is ansible.utils.ipv4_address }}"
# TASK [Check if 10.1.1.1 is a valid IPv4 address] *************************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 10.1.1.1/31 is not a valid IPv4 address
ansible.builtin.set_fact:
data: "{{ '10.1.1.1/31' is not ansible.utils.ipv4_address }}"
# TASK [Check if 10.1.1.1/31 is a valid IPv4 address] **********************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if fe80::216:3eff:fee4:16f3 is not a valid IPv4 address
ansible.builtin.set_fact:
data: "{{ 'fe80::216:3eff:fee4:16f3' is not ansible.utils.ipv4_address }}"
# TASK [Check if fe80::216:3eff:fee4:16f3 is not a valid IPv4 address] *********
# 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 _ipv4_address(ip):
"""Test if something in an IPv4 address"""
params = {"ip": ip}
_validate_args("ipv4_address", DOCUMENTATION, params)
try:
return ip_address(ip).version == 4
except Exception:
return False
class TestModule(object):
""" network jinja test"""
test_map = {"ipv4_address": _ipv4_address}
def tests(self):
return self.test_map

View File

@ -0,0 +1,96 @@
# -*- 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: ipv4_hostmask
"""
from __future__ import absolute_import, division, print_function
from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddress_utils import (
ip_network,
_need_ipaddress,
_validate_args,
)
__metaclass__ = type
DOCUMENTATION = """
name: ipv4_hostmask
author: Priyam Sahoo (@priyamsahoo)
version_added: "2.2.0"
short_description: Test if an address is a valid hostmask
description:
- This plugin checks if the provided ip address is a IPv4 hostmask or not
options:
ip:
description:
- A string that represents the value against which the test is going to be performed
- For example:
- "0.1.255.255"
- "255.255.255.0"
type: str
required: True
notes:
"""
EXAMPLES = r"""
#### Simple examples
- name: Check if 0.0.0.255 is a hostmask
ansible.builtin.set_fact:
data: "{{ '0.0.0.255' is ansible.utils.ipv4_hostmask }}"
# TASK [Check if 0.0.0.255 is a hostmask] ***********************************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 255.255.255.0 is not a hostmask
ansible.builtin.set_fact:
data: "{{ '255.255.255.0' is not ansible.utils.ipv4_hostmask }}"
# TASK [Check if 255.255.255.0 is a hostmask] *********************************
# 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 _ipv4_hostmask(ip):
"""Test if an address is a hostmask"""
params = {"ip": ip}
_validate_args("ipv4_hostmask", DOCUMENTATION, params)
try:
ipaddr = ip_network("10.0.0.0/{ip}".format(ip=ip))
return str(ipaddr.hostmask) == ip
except Exception:
return False
class TestModule(object):
""" network jinja test"""
test_map = {"ipv4_hostmask": _ipv4_hostmask}
def tests(self):
return self.test_map

View File

@ -0,0 +1,108 @@
# -*- 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: ipv4_netmask
"""
from __future__ import absolute_import, division, print_function
from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddress_utils import (
ip_network,
_need_ipaddress,
_validate_args,
)
__metaclass__ = type
DOCUMENTATION = """
name: ipv4_netmask
author: Priyam Sahoo (@priyamsahoo)
version_added: "2.2.0"
short_description: Test if an address is a valid netmask
description:
- This plugin checks if the provided ip address is a valid IPv4 netmask or not
options:
mask:
description:
- A string that represents the value against which the test is going to be performed
- For example:
- "0.1.255.255"
- "255.255.255.0"
type: str
required: True
notes:
"""
EXAMPLES = r"""
#### Simple examples
- name: Check if 255.255.255.0 is a netmask
ansible.builtin.set_fact:
data: "{{ '255.255.255.0' is ansible.utils.ipv4_netmask }}"
# TASK [Check if 255.255.255.0 is a netmask] *******************************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 255.255.255.128 is a netmask
ansible.builtin.set_fact:
data: "{{ '255.255.255.128' is ansible.utils.ipv4_netmask }}"
# TASK [Check if 255.255.255.128 is a netmask] *****************************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": true
# },
# "changed": false
# }
- name: Check if 255.255.255.127 is not a netmask
ansible.builtin.set_fact:
data: "{{ '255.255.255.127' is not ansible.utils.ipv4_netmask }}"
# TASK [Check if 255.255.255.127 is not a netmask] *************************************
# 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 _ipv4_netmask(mask):
""" Test for a valid IPv4 netmask"""
params = {"mask": mask}
_validate_args("ipv4_netmask", DOCUMENTATION, params)
try:
network = ip_network("10.0.0.0/{mask}".format(mask=mask))
return str(network.netmask) == mask
except Exception:
return False
class TestModule(object):
""" network jinja test"""
test_map = {"ipv4_netmask": _ipv4_netmask}
def tests(self):
return self.test_map

View File

@ -0,0 +1,24 @@
---
- name: "ipv4: Check if 10.0.0.0/8 is a valid IPV4 address"
assert:
that: "{{ '10.0.0.0/8' is ansible.utils.ipv4 }}"
- name: "ipv4: Check if 192.168.1.250 is a valid IPV4 address"
assert:
that: "{{ '192.168.1.250' is ansible.utils.ipv4 }}"
- name: "ipv4: Test invalidness"
ansible.builtin.set_fact:
criteria_check1: "{{ 'fe80::216:3eff:fee4:16f3' is ansible.utils.ipv4 }}"
- name: "ipv4: Assert invalidness"
assert:
that: "{{ criteria_check1 == false }}"
- name: "ipv4: Test invalidness"
ansible.builtin.set_fact:
criteria_check2: "{{ 'string' is ansible.utils.ipv4 }}"
- name: "ipv4: Assert invalidness"
assert:
that: "{{ criteria_check2 == false }}"

View File

@ -0,0 +1,28 @@
---
- name: "ipv4_address: Check if 192.168.1.250 is a valid IPV4 address"
assert:
that: "{{ '192.168.1.250' is ansible.utils.ipv4_address }}"
- name: "ipv4_address: Test invalidness"
ansible.builtin.set_fact:
criteria_check1: "{{ '10.0.0.0/8' is ansible.utils.ipv4_address }}"
- name: "ipv4_address: Assert invalidness"
assert:
that: "{{ criteria_check1 == false }}"
- name: "ipv4_address: Test invalidness"
ansible.builtin.set_fact:
criteria_check2: "{{ 'fe80::216:3eff:fee4:16f3' is ansible.utils.ipv4_address }}"
- name: "ipv4_address: Assert invalidness"
assert:
that: "{{ criteria_check2 == false }}"
- name: "ipv4_address: Test invalidness"
ansible.builtin.set_fact:
criteria_check3: "{{ 'string' is ansible.utils.ipv4_address }}"
- name: "ipv4_address: Assert invalidness"
assert:
that: "{{ criteria_check3 == false }}"

View File

@ -0,0 +1,20 @@
---
- name: "ipv4_hostmask: Check if 0.0.0.255 is a hostmask"
assert:
that: "{{ '0.0.0.255' is ansible.utils.ipv4_hostmask }}"
- name: "ipv4_hostmask: Test invalidness"
ansible.builtin.set_fact:
criteria_check1: "{{ '255.255.255.0' is ansible.utils.ipv4_hostmask }}"
- name: "ipv4_hostmask: Assert invalidness"
assert:
that: "{{ criteria_check1 == false }}"
- name: "ipv4_hostmask: Test invalidness"
ansible.builtin.set_fact:
criteria_check2: "{{ '10.1.1.1' is ansible.utils.ipv4_hostmask }}"
- name: "ipv4_hostmask: Assert invalidness"
assert:
that: "{{ criteria_check2 == false }}"

View File

@ -0,0 +1,20 @@
---
- name: "ipv4_netmask: Check if 255.255.255.0 is a netmask"
assert:
that: "{{ '255.255.255.0' is ansible.utils.ipv4_netmask }}"
- name: "ipv4_netmask: Test invalidness"
ansible.builtin.set_fact:
criteria_check1: "{{ '0.0.0.255' is ansible.utils.ipv4_netmask }}"
- name: "ipv4_netmask: Assert invalidness"
assert:
that: "{{ criteria_check1 == false }}"
- name: "ipv4_netmask: Test invalidness"
ansible.builtin.set_fact:
criteria_check2: "{{ '10.1.1.1' is ansible.utils.ipv4_netmask }}"
- name: "ipv4_netmask: Assert invalidness"
assert:
that: "{{ criteria_check2 == false }}"

View File

@ -0,0 +1,43 @@
# -*- 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 netaddr test plugin: ipv4
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import unittest
from ansible_collections.ansible.utils.plugins.test.ipv4 import _ipv4
class TestIpV4(unittest.TestCase):
def setUp(self):
pass
def test_invalid_data(self):
"""Check passing invalid argspec"""
# missing argument
with self.assertRaises(TypeError) as error:
_ipv4()
self.assertIn("argument", str(error.exception))
def test_valid_data(self):
"""Check passing valid data as per criteria"""
result = _ipv4(ip="10.1.1.1")
self.assertEqual(result, True)
result = _ipv4(ip="10.0.0.0/8")
self.assertEqual(result, True)
result = _ipv4(ip="2001:db8:a::123")
self.assertEqual(result, False)
result = _ipv4(ip="string")
self.assertEqual(result, False)

View File

@ -0,0 +1,45 @@
# -*- 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 netaddr test plugin: ipv4_address
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import unittest
from ansible_collections.ansible.utils.plugins.test.ipv4_address import (
_ipv4_address,
)
class TestIpV4Address(unittest.TestCase):
def setUp(self):
pass
def test_invalid_data(self):
"""Check passing invalid argspec"""
# missing argument
with self.assertRaises(TypeError) as error:
_ipv4_address()
self.assertIn("argument", str(error.exception))
def test_valid_data(self):
"""Check passing valid data as per criteria"""
result = _ipv4_address(ip="10.1.1.1")
self.assertEqual(result, True)
result = _ipv4_address(ip="10.0.0.0/8")
self.assertEqual(result, False)
result = _ipv4_address(ip="2001:db8:a::123")
self.assertEqual(result, False)
result = _ipv4_address(ip="string")
self.assertEqual(result, False)

View File

@ -0,0 +1,45 @@
# -*- 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 netaddr test plugin: ipv4_hostmask
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import unittest
from ansible_collections.ansible.utils.plugins.test.ipv4_hostmask import (
_ipv4_hostmask,
)
class TestIpV4Hostmask(unittest.TestCase):
def setUp(self):
pass
def test_invalid_data(self):
"""Check passing invalid argspec"""
# missing argument
with self.assertRaises(TypeError) as error:
_ipv4_hostmask()
self.assertIn("argument", str(error.exception))
def test_valid_data(self):
"""Check passing valid data as per criteria"""
result = _ipv4_hostmask(ip="0.1.255.255")
self.assertEqual(result, True)
result = _ipv4_hostmask(ip="255.255.255.0")
self.assertEqual(result, False)
result = _ipv4_hostmask(ip="10.1.1.1")
self.assertEqual(result, False)
result = _ipv4_hostmask(ip="string")
self.assertEqual(result, False)

View File

@ -0,0 +1,45 @@
# -*- 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 netaddr test plugin: ipv4_netmask
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import unittest
from ansible_collections.ansible.utils.plugins.test.ipv4_netmask import (
_ipv4_netmask,
)
class TestIpV4Netmask(unittest.TestCase):
def setUp(self):
pass
def test_invalid_data(self):
"""Check passing invalid argspec"""
# missing argument
with self.assertRaises(TypeError) as error:
_ipv4_netmask()
self.assertIn("argument", str(error.exception))
def test_valid_data(self):
"""Check passing valid data as per criteria"""
result = _ipv4_netmask(mask="255.255.255.0")
self.assertEqual(result, True)
result = _ipv4_netmask(mask="0.1.255.255")
self.assertEqual(result, False)
result = _ipv4_netmask(mask="10.1.1.1")
self.assertEqual(result, False)
result = _ipv4_netmask(mask="string")
self.assertEqual(result, False)