diff --git a/README.md b/README.md index 0fc2d12..14acc2e 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,11 @@ Name | Description [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.ipv6](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipv6_test.rst)|Test if something is an IPv6 address or network +[ansible.utils.ipv6_address](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipv6_address_test.rst)|Test if something is an IPv6 address +[ansible.utils.ipv6_ipv4_mapped](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipv6_ipv4_mapped_test.rst)|Test if something appears to be a mapped IPv6 to IPv4 mapped address +[ansible.utils.ipv6_sixtofour](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipv6_sixtofour_test.rst)|Test if something appears to be a 6to4 address +[ansible.utils.ipv6_teredo](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipv6_teredo_test.rst)|Test if something appears to be an IPv6 teredo address [ansible.utils.validate](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.validate_test.rst)|Validate data with provided criteria diff --git a/changelogs/fragments/add_netaddr_test_plugins_4.yml b/changelogs/fragments/add_netaddr_test_plugins_4.yml new file mode 100644 index 0000000..9b3e2cc --- /dev/null +++ b/changelogs/fragments/add_netaddr_test_plugins_4.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - Add ipv6, ipv6_address, ipv6_ipv4_mapped, ipv6_sixtofour, ipv6_teredo test plugins diff --git a/docs/ansible.utils.ipv6_address_test.rst b/docs/ansible.utils.ipv6_address_test.rst new file mode 100644 index 0000000..9915bf7 --- /dev/null +++ b/docs/ansible.utils.ipv6_address_test.rst @@ -0,0 +1,150 @@ +.. _ansible.utils.ipv6_address_test: + + +************************** +ansible.utils.ipv6_address +************************** + +**Test if something is an IPv6 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 IPv6 addressing scheme + + + + +Parameters +---------- + +.. raw:: html + + + + + + + + + + + + + + +
ParameterChoices/DefaultsConfigurationComments
+
+ ip + +
+ string + / required +
+
+ + +
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']}
+
+
+ + + + +Examples +-------- + +.. code-block:: yaml + + #### Simple examples + + - name: Check if fe80::216:3eff:fee4:16f3 is a valid IPv6 address + ansible.builtin.set_fact: + data: "{{ 'fe80::216:3eff:fee4:16f3' is ansible.utils.ipv6_address }}" + + # TASK [Check if fe80::216:3eff:fee4:16f3 is a valid IPv6 address] ********************* + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + - name: Check if 2001:db8:a::123/64 is not a valid IPv6 address + ansible.builtin.set_fact: + data: "{{ '2001:db8:a::123/64' is not ansible.utils.ipv6_address }}" + + # TASK [Check if 2001:db8:a::123/64 is not a valid IPv6 address] *********************** + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + - name: Check if 192.169.1.250 is not a valid IPv6 address + ansible.builtin.set_fact: + data: "{{ '192.169.1.250' is not ansible.utils.ipv6_address }}" + + # TASK [Check if 192.169.1.250 is not a valid IPv6 address] **************************** + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + + +Return Values +------------- +Common return values are documented `here `_, the following are the fields unique to this test: + +.. raw:: html + + + + + + + + + + + + +
KeyReturnedDescription
+
+ data + +
+ - +
+
+
If jinja test satisfies plugin expression true
+
If jinja test does not satisfy plugin expression false
+
+
+

+ + +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. diff --git a/docs/ansible.utils.ipv6_ipv4_mapped_test.rst b/docs/ansible.utils.ipv6_ipv4_mapped_test.rst new file mode 100644 index 0000000..da6e49f --- /dev/null +++ b/docs/ansible.utils.ipv6_ipv4_mapped_test.rst @@ -0,0 +1,150 @@ +.. _ansible.utils.ipv6_ipv4_mapped_test: + + +****************************** +ansible.utils.ipv6_ipv4_mapped +****************************** + +**Test if something appears to be a mapped IPv6 to IPv4 mapped address** + + +Version added: 2.2.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- This plugin checks if the provided value is a valid IPv4-mapped IPv6 address + + + + +Parameters +---------- + +.. raw:: html + + + + + + + + + + + + + + +
ParameterChoices/DefaultsConfigurationComments
+
+ ip + +
+ string + / required +
+
+ + +
A string that represents the value against which the test is going to be performed
+
{'For example': ['::FFFF:10.1.1.1', '::AAAA:10.1.1.1', 'helloworld']}
+
+
+ + + + +Examples +-------- + +.. code-block:: yaml + + #### Simple examples + + - name: Check if ::FFFF:10.1.1.1 is a valid IPv4-mapped IPv6 address + ansible.builtin.set_fact: + data: "{{ '::FFFF:10.1.1.1' is ansible.utils.ipv6_ipv4_mapped }}" + + # TASK [Check if ::FFFF:10.1.1.1 is a valid IPv4-mapped IPv6 address] ************* + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + - name: Check if ::AAAA:10.1.1.1 is not a valid IPv4-mapped IPv6 address + ansible.builtin.set_fact: + data: "{{ '::AAAA:10.1.1.1' is not ansible.utils.ipv6_ipv4_mapped }}" + + # TASK [Check if ::AAAA:10.1.1.1 is not a valid IPv4-mapped IPv6 address] ****************** + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + - name: Check if helloworld is not a valid IPv4-mapped IPv6 address + ansible.builtin.set_fact: + data: "{{ 'helloworld' is not ansible.utils.ipv6_ipv4_mapped }}" + + # TASK [Check if helloworld is not a valid IPv4-mapped IPv6 address] *********************** + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + + +Return Values +------------- +Common return values are documented `here `_, the following are the fields unique to this test: + +.. raw:: html + + + + + + + + + + + + +
KeyReturnedDescription
+
+ data + +
+ - +
+
+
If jinja test satisfies plugin expression true
+
If jinja test does not satisfy plugin expression false
+
+
+

+ + +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. diff --git a/docs/ansible.utils.ipv6_sixtofour_test.rst b/docs/ansible.utils.ipv6_sixtofour_test.rst new file mode 100644 index 0000000..f6e6e99 --- /dev/null +++ b/docs/ansible.utils.ipv6_sixtofour_test.rst @@ -0,0 +1,150 @@ +.. _ansible.utils.ipv6_sixtofour_test: + + +**************************** +ansible.utils.ipv6_sixtofour +**************************** + +**Test if something appears to be a 6to4 address** + + +Version added: 2.2.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- This plugin checks if the provided value is a valid 6to4 address + + + + +Parameters +---------- + +.. raw:: html + + + + + + + + + + + + + + +
ParameterChoices/DefaultsConfigurationComments
+
+ ip + +
+ string + / required +
+
+ + +
A string that represents the value against which the test is going to be performed
+
{'For example': ['2002:c0a8:6301:1::1', '::AAAA:10.1.1.1', 'hello_world']}
+
+
+ + + + +Examples +-------- + +.. code-block:: yaml + + #### Simple examples + + - name: Check if 2002:c0a8:6301:1::1 is a valid 6to4 address + ansible.builtin.set_fact: + data: "{{ '2002:c0a8:6301:1::1' is ansible.utils.ipv6_sixtofour }}" + + # TASK [Check if 2002:c0a8:6301:1::1 is a valid 6to4 address] **************************** + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + - name: Check if 2001:c0a8:6301:1::1 is not a valid 6to4 address + ansible.builtin.set_fact: + data: "{{ '2001:c0a8:6301:1::1' is not ansible.utils.ipv6_sixtofour }}" + + # TASK [Check if 2001:c0a8:6301:1::1 is not a valid 6to4 address] ************************ + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + - name: Check if helloworld is not a valid 6to4 address + ansible.builtin.set_fact: + data: "{{ 'helloworld' is not ansible.utils.ipv6_sixtofour }}" + + # TASK [Check if helloworld is not a valid 6to4 address] ********************************* + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + + +Return Values +------------- +Common return values are documented `here `_, the following are the fields unique to this test: + +.. raw:: html + + + + + + + + + + + + +
KeyReturnedDescription
+
+ data + +
+ - +
+
+
If jinja test satisfies plugin expression true
+
If jinja test does not satisfy plugin expression false
+
+
+

+ + +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. diff --git a/docs/ansible.utils.ipv6_teredo_test.rst b/docs/ansible.utils.ipv6_teredo_test.rst new file mode 100644 index 0000000..9aae874 --- /dev/null +++ b/docs/ansible.utils.ipv6_teredo_test.rst @@ -0,0 +1,150 @@ +.. _ansible.utils.ipv6_teredo_test: + + +************************* +ansible.utils.ipv6_teredo +************************* + +**Test if something appears to be an IPv6 teredo address** + + +Version added: 2.2.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- This plugin checks if the provided value is a valid IPv6 teredo address + + + + +Parameters +---------- + +.. raw:: html + + + + + + + + + + + + + + +
ParameterChoices/DefaultsConfigurationComments
+
+ ip + +
+ string + / required +
+
+ + +
A string that represents the value against which the test is going to be performed
+
{'For example': ['2001::c0a8:6301:1', '2002::c0a8:6301:1', 'hello_world']}
+
+
+ + + + +Examples +-------- + +.. code-block:: yaml + + #### Simple examples + + - name: Check if 2001::c0a8:6301:1 is a valid IPv6 teredo address + ansible.builtin.set_fact: + data: "{{ '2001::c0a8:6301:1' is ansible.utils.ipv6_teredo }}" + + # TASK [Check if 2001::c0a8:6301:1 is a valid IPv6 teredo address] ******************** + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + - name: Check if 2002::c0a8:6301:1 is not a valid IPv6 teredo address + ansible.builtin.set_fact: + data: "{{ '2002::c0a8:6301:1' is not ansible.utils.ipv6_teredo }}" + + # TASK [Check if 2002::c0a8:6301:1 is not a valid IPv6 teredo address] **************** + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + - name: Check if hello_world is not a valid IPv6 teredo address + ansible.builtin.set_fact: + data: "{{ 'hello_world' is not ansible.utils.ipv6_teredo }}" + + # TASK [Check if hello_world is not a valid IPv6 teredo address] ********************** + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + + +Return Values +------------- +Common return values are documented `here `_, the following are the fields unique to this test: + +.. raw:: html + + + + + + + + + + + + +
KeyReturnedDescription
+
+ data + +
+ - +
+
+
If jinja test satisfies plugin expression true
+
If jinja test does not satisfy plugin expression false
+
+
+

+ + +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. diff --git a/docs/ansible.utils.ipv6_test.rst b/docs/ansible.utils.ipv6_test.rst new file mode 100644 index 0000000..e3f567c --- /dev/null +++ b/docs/ansible.utils.ipv6_test.rst @@ -0,0 +1,150 @@ +.. _ansible.utils.ipv6_test: + + +****************** +ansible.utils.ipv6 +****************** + +**Test if something is an IPv6 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 IPv6 addressing scheme + + + + +Parameters +---------- + +.. raw:: html + + + + + + + + + + + + + + +
ParameterChoices/DefaultsConfigurationComments
+
+ ip + +
+ string + / required +
+
+ + +
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']}
+
+
+ + + + +Examples +-------- + +.. code-block:: yaml + + #### Simple examples + + - name: Check if fe80::216:3eff:fee4:16f3 is a valid IPv6 address + ansible.builtin.set_fact: + data: "{{ 'fe80::216:3eff:fee4:16f3' is ansible.utils.ipv6 }}" + + # TASK [Check if fe80::216:3eff:fee4:16f3 is a valid IPv6 address] ************* + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + - name: Check if 2001:db8:a::/64 is a valid IPv6 address + ansible.builtin.set_fact: + data: "{{ '2001:db8:a::/64' is ansible.utils.ipv6 }}" + + # TASK [Check if 2001:db8:a::/64 is a valid IPv6 address] ********************** + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + - name: Check if 192.169.1.250 is not a valid IPv6 address + ansible.builtin.set_fact: + data: "{{ '192.169.1.250' is not ansible.utils.ipv6 }}" + + # TASK [Check if 192.169.1.250 is not a valid IPv6 address] ******************** + # ok: [localhost] => { + # "ansible_facts": { + # "data": true + # }, + # "changed": false + # } + + + +Return Values +------------- +Common return values are documented `here `_, the following are the fields unique to this test: + +.. raw:: html + + + + + + + + + + + + +
KeyReturnedDescription
+
+ data + +
+ - +
+
+
If jinja test satisfies plugin expression true
+
If jinja test does not satisfy plugin expression false
+
+
+

+ + +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. diff --git a/plugins/test/ipv6.py b/plugins/test/ipv6.py new file mode 100644 index 0000000..b8ea59c --- /dev/null +++ b/plugins/test/ipv6.py @@ -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: ipv6 +""" + +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: ipv6 + author: Priyam Sahoo (@priyamsahoo) + version_added: "2.2.0" + short_description: Test if something is an IPv6 address or network + description: + - This plugin checks if the provided value is a valid host or network IP address with IPv6 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 fe80::216:3eff:fee4:16f3 is a valid IPv6 address + ansible.builtin.set_fact: + data: "{{ 'fe80::216:3eff:fee4:16f3' is ansible.utils.ipv6 }}" + +# TASK [Check if fe80::216:3eff:fee4:16f3 is a valid IPv6 address] ************* +# ok: [localhost] => { +# "ansible_facts": { +# "data": true +# }, +# "changed": false +# } + +- name: Check if 2001:db8:a::/64 is a valid IPv6 address + ansible.builtin.set_fact: + data: "{{ '2001:db8:a::/64' is ansible.utils.ipv6 }}" + +# TASK [Check if 2001:db8:a::/64 is a valid IPv6 address] ********************** +# ok: [localhost] => { +# "ansible_facts": { +# "data": true +# }, +# "changed": false +# } + +- name: Check if 192.169.1.250 is not a valid IPv6 address + ansible.builtin.set_fact: + data: "{{ '192.169.1.250' is not ansible.utils.ipv6 }}" + +# TASK [Check if 192.169.1.250 is not a valid IPv6 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 _ipv6(ip): + """Test if something in an IPv6 address or network""" + + params = {"ip": ip} + _validate_args("ipv6", DOCUMENTATION, params) + + try: + return ip_network(ip).version == 6 + except Exception: + return False + + +class TestModule(object): + """ network jinja test""" + + test_map = {"ipv6": _ipv6} + + def tests(self): + return self.test_map diff --git a/plugins/test/ipv6_address.py b/plugins/test/ipv6_address.py new file mode 100644 index 0000000..cbb9984 --- /dev/null +++ b/plugins/test/ipv6_address.py @@ -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: ipv6_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: ipv6_address + author: Priyam Sahoo (@priyamsahoo) + version_added: "2.2.0" + short_description: Test if something is an IPv6 address + description: + - This plugin checks if the provided value is a valid host IP address with IPv6 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 fe80::216:3eff:fee4:16f3 is a valid IPv6 address + ansible.builtin.set_fact: + data: "{{ 'fe80::216:3eff:fee4:16f3' is ansible.utils.ipv6_address }}" + +# TASK [Check if fe80::216:3eff:fee4:16f3 is a valid IPv6 address] ********************* +# ok: [localhost] => { +# "ansible_facts": { +# "data": true +# }, +# "changed": false +# } + +- name: Check if 2001:db8:a::123/64 is not a valid IPv6 address + ansible.builtin.set_fact: + data: "{{ '2001:db8:a::123/64' is not ansible.utils.ipv6_address }}" + +# TASK [Check if 2001:db8:a::123/64 is not a valid IPv6 address] *********************** +# ok: [localhost] => { +# "ansible_facts": { +# "data": true +# }, +# "changed": false +# } + +- name: Check if 192.169.1.250 is not a valid IPv6 address + ansible.builtin.set_fact: + data: "{{ '192.169.1.250' is not ansible.utils.ipv6_address }}" + +# TASK [Check if 192.169.1.250 is not a valid IPv6 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 _ipv6_address(ip): + """Test if something in an IPv6 address""" + + params = {"ip": ip} + _validate_args("ipv6_address", DOCUMENTATION, params) + + try: + return ip_address(ip).version == 6 + except Exception: + return False + + +class TestModule(object): + """ network jinja test""" + + test_map = {"ipv6_address": _ipv6_address} + + def tests(self): + return self.test_map diff --git a/plugins/test/ipv6_ipv4_mapped.py b/plugins/test/ipv6_ipv4_mapped.py new file mode 100644 index 0000000..18c4f10 --- /dev/null +++ b/plugins/test/ipv6_ipv4_mapped.py @@ -0,0 +1,110 @@ +# -*- 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: ipv6_ipv4_mapped +""" + +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: ipv6_ipv4_mapped + author: Priyam Sahoo (@priyamsahoo) + version_added: "2.2.0" + short_description: Test if something appears to be a mapped IPv6 to IPv4 mapped address + description: + - This plugin checks if the provided value is a valid IPv4-mapped IPv6 address + options: + ip: + description: + - A string that represents the value against which the test is going to be performed + - For example: + - "::FFFF:10.1.1.1" + - "::AAAA:10.1.1.1" + - "helloworld" + type: str + required: True + notes: +""" + +EXAMPLES = r""" + +#### Simple examples + +- name: Check if ::FFFF:10.1.1.1 is a valid IPv4-mapped IPv6 address + ansible.builtin.set_fact: + data: "{{ '::FFFF:10.1.1.1' is ansible.utils.ipv6_ipv4_mapped }}" + +# TASK [Check if ::FFFF:10.1.1.1 is a valid IPv4-mapped IPv6 address] ************* +# ok: [localhost] => { +# "ansible_facts": { +# "data": true +# }, +# "changed": false +# } + +- name: Check if ::AAAA:10.1.1.1 is not a valid IPv4-mapped IPv6 address + ansible.builtin.set_fact: + data: "{{ '::AAAA:10.1.1.1' is not ansible.utils.ipv6_ipv4_mapped }}" + +# TASK [Check if ::AAAA:10.1.1.1 is not a valid IPv4-mapped IPv6 address] ****************** +# ok: [localhost] => { +# "ansible_facts": { +# "data": true +# }, +# "changed": false +# } + +- name: Check if helloworld is not a valid IPv4-mapped IPv6 address + ansible.builtin.set_fact: + data: "{{ 'helloworld' is not ansible.utils.ipv6_ipv4_mapped }}" + +# TASK [Check if helloworld is not a valid IPv4-mapped IPv6 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 _ipv6_ipv4_mapped(ip): + """ Test if something appears to be a mapped IPv6 to IPv4 mapped address """ + + params = {"ip": ip} + _validate_args("ipv6_ipv4_mapped", DOCUMENTATION, params) + + try: + if ip_address(ip).ipv4_mapped is None: + return False + return True + except Exception: + return False + + +class TestModule(object): + """ network jinja test""" + + test_map = {"ipv6_ipv4_mapped": _ipv6_ipv4_mapped} + + def tests(self): + return self.test_map diff --git a/plugins/test/ipv6_sixtofour.py b/plugins/test/ipv6_sixtofour.py new file mode 100644 index 0000000..09612b6 --- /dev/null +++ b/plugins/test/ipv6_sixtofour.py @@ -0,0 +1,110 @@ +# -*- 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: ipv6_sixtofour +""" + +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: ipv6_sixtofour + author: Priyam Sahoo (@priyamsahoo) + version_added: "2.2.0" + short_description: Test if something appears to be a 6to4 address + description: + - This plugin checks if the provided value is a valid 6to4 address + options: + ip: + description: + - A string that represents the value against which the test is going to be performed + - For example: + - "2002:c0a8:6301:1::1" + - "::AAAA:10.1.1.1" + - "hello_world" + type: str + required: True + notes: +""" + +EXAMPLES = r""" + +#### Simple examples + +- name: Check if 2002:c0a8:6301:1::1 is a valid 6to4 address + ansible.builtin.set_fact: + data: "{{ '2002:c0a8:6301:1::1' is ansible.utils.ipv6_sixtofour }}" + +# TASK [Check if 2002:c0a8:6301:1::1 is a valid 6to4 address] **************************** +# ok: [localhost] => { +# "ansible_facts": { +# "data": true +# }, +# "changed": false +# } + +- name: Check if 2001:c0a8:6301:1::1 is not a valid 6to4 address + ansible.builtin.set_fact: + data: "{{ '2001:c0a8:6301:1::1' is not ansible.utils.ipv6_sixtofour }}" + +# TASK [Check if 2001:c0a8:6301:1::1 is not a valid 6to4 address] ************************ +# ok: [localhost] => { +# "ansible_facts": { +# "data": true +# }, +# "changed": false +# } + +- name: Check if helloworld is not a valid 6to4 address + ansible.builtin.set_fact: + data: "{{ 'helloworld' is not ansible.utils.ipv6_sixtofour }}" + +# TASK [Check if helloworld is not a valid 6to4 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 _ipv6_sixtofour(ip): + """ Test if something appears to be a 6to4 address """ + + params = {"ip": ip} + _validate_args("ipv6_sixtofour", DOCUMENTATION, params) + + try: + if ip_address(ip).sixtofour is None: + return False + return True + except Exception: + return False + + +class TestModule(object): + """ network jinja test""" + + test_map = {"ipv6_sixtofour": _ipv6_sixtofour} + + def tests(self): + return self.test_map diff --git a/plugins/test/ipv6_teredo.py b/plugins/test/ipv6_teredo.py new file mode 100644 index 0000000..86964e4 --- /dev/null +++ b/plugins/test/ipv6_teredo.py @@ -0,0 +1,110 @@ +# -*- 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: ipv6_teredo +""" + +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: ipv6_teredo + author: Priyam Sahoo (@priyamsahoo) + version_added: "2.2.0" + short_description: Test if something appears to be an IPv6 teredo address + description: + - This plugin checks if the provided value is a valid IPv6 teredo address + options: + ip: + description: + - A string that represents the value against which the test is going to be performed + - For example: + - "2001::c0a8:6301:1" + - "2002::c0a8:6301:1" + - "hello_world" + type: str + required: True + notes: +""" + +EXAMPLES = r""" + +#### Simple examples + +- name: Check if 2001::c0a8:6301:1 is a valid IPv6 teredo address + ansible.builtin.set_fact: + data: "{{ '2001::c0a8:6301:1' is ansible.utils.ipv6_teredo }}" + +# TASK [Check if 2001::c0a8:6301:1 is a valid IPv6 teredo address] ******************** +# ok: [localhost] => { +# "ansible_facts": { +# "data": true +# }, +# "changed": false +# } + +- name: Check if 2002::c0a8:6301:1 is not a valid IPv6 teredo address + ansible.builtin.set_fact: + data: "{{ '2002::c0a8:6301:1' is not ansible.utils.ipv6_teredo }}" + +# TASK [Check if 2002::c0a8:6301:1 is not a valid IPv6 teredo address] **************** +# ok: [localhost] => { +# "ansible_facts": { +# "data": true +# }, +# "changed": false +# } + +- name: Check if hello_world is not a valid IPv6 teredo address + ansible.builtin.set_fact: + data: "{{ 'hello_world' is not ansible.utils.ipv6_teredo }}" + +# TASK [Check if hello_world is not a valid IPv6 teredo 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 _ipv6_teredo(ip): + """ Test if something is an IPv6 teredo address """ + + params = {"ip": ip} + _validate_args("ipv6_teredo", DOCUMENTATION, params) + + try: + if ip_address(ip).teredo is None: + return False + return True + except Exception: + return False + + +class TestModule(object): + """ network jinja test""" + + test_map = {"ipv6_teredo": _ipv6_teredo} + + def tests(self): + return self.test_map diff --git a/tests/integration/targets/netaddr_test/tasks/include/ipv6.yml b/tests/integration/targets/netaddr_test/tasks/include/ipv6.yml new file mode 100644 index 0000000..02defa2 --- /dev/null +++ b/tests/integration/targets/netaddr_test/tasks/include/ipv6.yml @@ -0,0 +1,24 @@ +--- +- name: "ipv6: Check if fe80::216:3eff:fee4:16f3 is a valid IPV6 address" + assert: + that: "{{ 'fe80::216:3eff:fee4:16f3' is ansible.utils.ipv6 }}" + +- name: "ipv6: Check if 2001:db8:a::/64 is a valid IPV6 address" + assert: + that: "{{ '2001:db8:a::/64' is ansible.utils.ipv6 }}" + +- name: "ipv6: Test invalidness" + ansible.builtin.set_fact: + criteria_check1: "{{ '10.1.1.1' is ansible.utils.ipv6 }}" + +- name: "ipv6: Assert invalidness" + assert: + that: "{{ criteria_check1 == false }}" + +- name: "ipv6: Test invalidness" + ansible.builtin.set_fact: + criteria_check2: "{{ 'string' is ansible.utils.ipv6 }}" + +- name: "ipv6: Assert invalidness" + assert: + that: "{{ criteria_check2 == false }}" diff --git a/tests/integration/targets/netaddr_test/tasks/include/ipv6_address.yml b/tests/integration/targets/netaddr_test/tasks/include/ipv6_address.yml new file mode 100644 index 0000000..373d077 --- /dev/null +++ b/tests/integration/targets/netaddr_test/tasks/include/ipv6_address.yml @@ -0,0 +1,28 @@ +--- +- name: "ipv6_address: Check if fe80::216:3eff:fee4:16f3 is a valid IPV6 address" + assert: + that: "{{ 'fe80::216:3eff:fee4:16f3' is ansible.utils.ipv6_address }}" + +- name: "ipv6_address: Test invalidness" + ansible.builtin.set_fact: + criteria_check1: "{{ '2001:db8:a::123/64' is ansible.utils.ipv6_address }}" + +- name: "ipv6_address: Assert invalidness" + assert: + that: "{{ criteria_check1 == false }}" + +- name: "ipv6_address: Test invalidness" + ansible.builtin.set_fact: + criteria_check2: "{{ '192.168.1.250' is ansible.utils.ipv6_address }}" + +- name: "ipv6_address: Assert invalidness" + assert: + that: "{{ criteria_check2 == false }}" + +- name: "ipv6_address: Test invalidness" + ansible.builtin.set_fact: + criteria_check3: "{{ 'string' is ansible.utils.ipv6_address }}" + +- name: "ipv6_address: Assert invalidness" + assert: + that: "{{ criteria_check3 == false }}" diff --git a/tests/integration/targets/netaddr_test/tasks/include/ipv6_ipv4_mapped.yml b/tests/integration/targets/netaddr_test/tasks/include/ipv6_ipv4_mapped.yml new file mode 100644 index 0000000..ea6b627 --- /dev/null +++ b/tests/integration/targets/netaddr_test/tasks/include/ipv6_ipv4_mapped.yml @@ -0,0 +1,20 @@ +--- +- name: "ipv6_ipv4_mapped: Check if ::FFFF:10.1.1.1 is a valid IPv4-mapped IPv6 address" + assert: + that: "{{ '::FFFF:10.1.1.1' is ansible.utils.ipv6_ipv4_mapped }}" + +- name: "ipv6_ipv4_mapped: Test invalidness" + ansible.builtin.set_fact: + criteria_check1: "{{ '::AAAA:10.1.1.1' is ansible.utils.ipv6_ipv4_mapped }}" + +- name: "ipv6_ipv4_mapped: Assert invalidness" + assert: + that: "{{ criteria_check1 == false }}" + +- name: "ipv6_ipv4_mapped: Test invalidness" + ansible.builtin.set_fact: + criteria_check2: "{{ 'hello-world' is ansible.utils.ipv6_ipv4_mapped }}" + +- name: "ipv6_ipv4_mapped: Assert invalidness" + assert: + that: "{{ criteria_check2 == false }}" diff --git a/tests/integration/targets/netaddr_test/tasks/include/ipv6_sixtofour.yml b/tests/integration/targets/netaddr_test/tasks/include/ipv6_sixtofour.yml new file mode 100644 index 0000000..4cd6278 --- /dev/null +++ b/tests/integration/targets/netaddr_test/tasks/include/ipv6_sixtofour.yml @@ -0,0 +1,20 @@ +--- +- name: "ipv6_sixtofour: Check if 2002:c0a8:6301:1::1 is a valid 6to4 address" + assert: + that: "{{ '2002:c0a8:6301:1::1' is ansible.utils.ipv6_sixtofour }}" + +- name: "ipv6_sixtofour: Test invalidness" + ansible.builtin.set_fact: + criteria_check1: "{{ '2001:c0a8:6301:1::1' is ansible.utils.ipv6_sixtofour }}" + +- name: "ipv6_sixtofour: Assert invalidness" + assert: + that: "{{ criteria_check1 == false }}" + +- name: "ipv6_sixtofour: Test invalidness" + ansible.builtin.set_fact: + criteria_check2: "{{ 'helloworld' is ansible.utils.ipv6_sixtofour }}" + +- name: "ipv6_sixtofour: Assert invalidness" + assert: + that: "{{ criteria_check2 == false }}" diff --git a/tests/integration/targets/netaddr_test/tasks/include/ipv6_teredo.yml b/tests/integration/targets/netaddr_test/tasks/include/ipv6_teredo.yml new file mode 100644 index 0000000..9f8bd49 --- /dev/null +++ b/tests/integration/targets/netaddr_test/tasks/include/ipv6_teredo.yml @@ -0,0 +1,20 @@ +--- +- name: "ipv6_teredo: Check if 2001::c0a8:6301:1 is a valid 6to4 address" + assert: + that: "{{ '2001::c0a8:6301:1' is ansible.utils.ipv6_teredo }}" + +- name: "ipv6_teredo: Test invalidness" + ansible.builtin.set_fact: + criteria_check1: "{{ '2002::c0a8:6301:1' is ansible.utils.ipv6_teredo }}" + +- name: "ipv6_teredo: Assert invalidness" + assert: + that: "{{ criteria_check1 == false }}" + +- name: "ipv6_teredo: Test invalidness" + ansible.builtin.set_fact: + criteria_check2: "{{ 'helloworld' is ansible.utils.ipv6_teredo }}" + +- name: "ipv6_teredo: Assert invalidness" + assert: + that: "{{ criteria_check2 == false }}" diff --git a/tests/unit/plugins/test/test_ipv6.py b/tests/unit/plugins/test/test_ipv6.py new file mode 100644 index 0000000..cee46d2 --- /dev/null +++ b/tests/unit/plugins/test/test_ipv6.py @@ -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: ipv6 +""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import unittest +from ansible_collections.ansible.utils.plugins.test.ipv6 import _ipv6 + + +class TestIpV6(unittest.TestCase): + def setUp(self): + pass + + def test_invalid_data(self): + """Check passing invalid argspec""" + + # missing argument + with self.assertRaises(TypeError) as error: + _ipv6() + self.assertIn("argument", str(error.exception)) + + def test_valid_data(self): + """Check passing valid data as per criteria""" + + result = _ipv6(ip="fe80::216:3eff:fee4:16f3") + self.assertEqual(result, True) + + result = _ipv6(ip="2001:db8:a::/64") + self.assertEqual(result, True) + + result = _ipv6(ip="10.1.1.1") + self.assertEqual(result, False) + + result = _ipv6(ip="string") + self.assertEqual(result, False) diff --git a/tests/unit/plugins/test/test_ipv6_address.py b/tests/unit/plugins/test/test_ipv6_address.py new file mode 100644 index 0000000..64e9ea5 --- /dev/null +++ b/tests/unit/plugins/test/test_ipv6_address.py @@ -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: ipv6_address +""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import unittest +from ansible_collections.ansible.utils.plugins.test.ipv6_address import ( + _ipv6_address, +) + + +class TestIpV6Address(unittest.TestCase): + def setUp(self): + pass + + def test_invalid_data(self): + """Check passing invalid argspec""" + + # missing argument + with self.assertRaises(TypeError) as error: + _ipv6_address() + self.assertIn("argument", str(error.exception)) + + def test_valid_data(self): + """Check passing valid data as per criteria""" + + result = _ipv6_address(ip="fe80::216:3eff:fee4:16f3") + self.assertEqual(result, True) + + result = _ipv6_address(ip="2001:db8:a::/64") + self.assertEqual(result, False) + + result = _ipv6_address(ip="10.1.1.1") + self.assertEqual(result, False) + + result = _ipv6_address(ip="string") + self.assertEqual(result, False) diff --git a/tests/unit/plugins/test/test_ipv6_ipv4_mapped.py b/tests/unit/plugins/test/test_ipv6_ipv4_mapped.py new file mode 100644 index 0000000..760906c --- /dev/null +++ b/tests/unit/plugins/test/test_ipv6_ipv4_mapped.py @@ -0,0 +1,42 @@ +# -*- 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: ipv6_ipv4_mapped +""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import unittest +from ansible_collections.ansible.utils.plugins.test.ipv6_ipv4_mapped import ( + _ipv6_ipv4_mapped, +) + + +class TestIpV6IpV4Mapped(unittest.TestCase): + def setUp(self): + pass + + def test_invalid_data(self): + """Check passing invalid argspec""" + + # missing argument + with self.assertRaises(TypeError) as error: + _ipv6_ipv4_mapped() + self.assertIn("argument", str(error.exception)) + + def test_valid_data(self): + """Check passing valid data as per criteria""" + + result = _ipv6_ipv4_mapped(ip="::FFFF:10.1.1.1") + self.assertEqual(result, True) + + result = _ipv6_ipv4_mapped(ip="::AAAA:10.1.1.1") + self.assertEqual(result, False) + + result = _ipv6_ipv4_mapped(ip="string") + self.assertEqual(result, False) diff --git a/tests/unit/plugins/test/test_ipv6_sixtofour.py b/tests/unit/plugins/test/test_ipv6_sixtofour.py new file mode 100644 index 0000000..52205af --- /dev/null +++ b/tests/unit/plugins/test/test_ipv6_sixtofour.py @@ -0,0 +1,42 @@ +# -*- 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: ipv6_sixtofour +""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import unittest +from ansible_collections.ansible.utils.plugins.test.ipv6_sixtofour import ( + _ipv6_sixtofour, +) + + +class TestIpV6SixToFour(unittest.TestCase): + def setUp(self): + pass + + def test_invalid_data(self): + """Check passing invalid argspec""" + + # missing argument + with self.assertRaises(TypeError) as error: + _ipv6_sixtofour() + self.assertIn("argument", str(error.exception)) + + def test_valid_data(self): + """Check passing valid data as per criteria""" + + result = _ipv6_sixtofour(ip="2002:c0a8:6301:1::1") + self.assertEqual(result, True) + + result = _ipv6_sixtofour(ip="2001:c0a8:6301:1::1") + self.assertEqual(result, False) + + result = _ipv6_sixtofour(ip="string") + self.assertEqual(result, False) diff --git a/tests/unit/plugins/test/test_ipv6_teredo.py b/tests/unit/plugins/test/test_ipv6_teredo.py new file mode 100644 index 0000000..1956099 --- /dev/null +++ b/tests/unit/plugins/test/test_ipv6_teredo.py @@ -0,0 +1,42 @@ +# -*- 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: ipv6_teredo +""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import unittest +from ansible_collections.ansible.utils.plugins.test.ipv6_teredo import ( + _ipv6_teredo, +) + + +class TestIpV6Teredo(unittest.TestCase): + def setUp(self): + pass + + def test_invalid_data(self): + """Check passing invalid argspec""" + + # missing argument + with self.assertRaises(TypeError) as error: + _ipv6_teredo() + self.assertIn("argument", str(error.exception)) + + def test_valid_data(self): + """Check passing valid data as per criteria""" + + result = _ipv6_teredo(ip="2001::c0a8:6301:1") + self.assertEqual(result, True) + + result = _ipv6_teredo(ip="2002::c0a8:6301:1") + self.assertEqual(result, False) + + result = _ipv6_teredo(ip="string") + self.assertEqual(result, False)