Add IPV6 filter plugin (#121)
Add IPV6 filter plugin SUMMARY ISSUE TYPE New Module Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Nathaniel Case <this.is@nathanielca.se> Reviewed-by: None <None>pull/122/head^2
parent
79b28faaaa
commit
c04c648b7a
|
@ -25,6 +25,7 @@ Name | Description
|
|||
[ansible.utils.from_xml](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.from_xml_filter.rst)|Convert given XML string to native python dictionary.
|
||||
[ansible.utils.get_path](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.get_path_filter.rst)|Retrieve the value in a variable using a path
|
||||
[ansible.utils.index_of](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.index_of_filter.rst)|Find the indices of items in a list matching some criteria
|
||||
[ansible.utils.ipv6](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipv6_filter.rst)|To filter only Ipv6 addresses Ipv6 filter is used.
|
||||
[ansible.utils.ip4_hex](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ip4_hex_filter.rst)|This filter is designed to convert IPv4 address to Hexadecimal notation with optional delimiter.
|
||||
[ansible.utils.ipmath](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipmath_filter.rst)|This filter is designed to do simple IP math/arithmetic.
|
||||
[ansible.utils.nthhost](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.nthhost_filter.rst)|This filter returns the nth host within a network described by value.
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
minor_changes:
|
||||
- Add ipv6 filter plugin.
|
|
@ -0,0 +1,200 @@
|
|||
.. _ansible.utils.ipv6_filter:
|
||||
|
||||
|
||||
******************
|
||||
ansible.utils.ipv6
|
||||
******************
|
||||
|
||||
**To filter only Ipv6 addresses Ipv6 filter is used.**
|
||||
|
||||
|
||||
Version added: 2.5.0
|
||||
|
||||
.. contents::
|
||||
:local:
|
||||
:depth: 1
|
||||
|
||||
|
||||
Synopsis
|
||||
--------
|
||||
- Sometimes you need only IPv6 addresses. To filter only Ipv6 addresses Ipv6 filter is used.
|
||||
|
||||
|
||||
|
||||
|
||||
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>query</b>
|
||||
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
|
||||
<div style="font-size: small">
|
||||
<span style="color: purple">string</span>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<b>Default:</b><br/><div style="color: blue">""</div>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
<div>You can provide a single argument to each ipv6() filter.</div>
|
||||
<div>Example. query type 'ipv4' to convert ipv6 into ipv4</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="1">
|
||||
<div class="ansibleOptionAnchor" id="parameter-"></div>
|
||||
<b>value</b>
|
||||
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
|
||||
<div style="font-size: small">
|
||||
<span style="color: purple">list</span>
|
||||
/ <span style="color: purple">elements=string</span>
|
||||
/ <span style="color: red">required</span>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
<div>list of subnets or individual address or any other values input for ipv6 plugin</div>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<br/>
|
||||
|
||||
|
||||
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
#### examples
|
||||
# Ipv6 filter plugin with different queries.
|
||||
- name: Set value as input list
|
||||
ansible.builtin.set_fact:
|
||||
value:
|
||||
- 192.24.2.1
|
||||
- ::ffff:192.168.32.0/120
|
||||
- ''
|
||||
- ::ffff:192.24.2.1/128
|
||||
- 192.168.32.0/24
|
||||
- fe80::100/10
|
||||
- True
|
||||
- name: IPv6 filter to filter Ipv6 Address
|
||||
debug:
|
||||
msg: "{{ value|ansible.utils.ipv6 }}"
|
||||
|
||||
- name: convert IPv6 addresses into IPv4 addresses.
|
||||
debug:
|
||||
msg: "{{ value|ansible.utils.ipv6('ipv4') }}"
|
||||
|
||||
- name: filter only IPv6 addresses.
|
||||
debug:
|
||||
msg: "{{ value|ansible.utils.ipv6('address') }}"
|
||||
|
||||
|
||||
# PLAY [Ipv6 filter plugin with different queries.] ******************************************************************
|
||||
# TASK [Set value as input list] ***************************************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "ansible_facts": {
|
||||
# "value": [
|
||||
# "192.24.2.1",
|
||||
# "::ffff:192.168.32.0/120",
|
||||
# "",
|
||||
# "::ffff:192.24.2.1/128",
|
||||
# "192.168.32.0/24",
|
||||
# "fe80::100/10",
|
||||
# true
|
||||
# ]
|
||||
# },
|
||||
# "changed": false
|
||||
# }
|
||||
#
|
||||
# TASK [IPv6 filter to filter Ipv6 Address] ****************************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "msg": [
|
||||
# "::ffff:192.168.32.0/120",
|
||||
# "::ffff:192.24.2.1/128",
|
||||
# "fe80::100/10"
|
||||
# ]
|
||||
# }
|
||||
#
|
||||
# TASK [convert IPv6 addresses into IPv4 addresses.] *******************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "msg": [
|
||||
# "192.168.32.0/24",
|
||||
# "192.24.2.1/32"
|
||||
# ]
|
||||
# }
|
||||
#
|
||||
# TASK [filter only IPv6 addresses] *******************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "msg": [
|
||||
# "::ffff:192.168.32.0",
|
||||
# "::ffff:192.24.2.1",
|
||||
# "fe80::100"
|
||||
# ]
|
||||
# }
|
||||
#
|
||||
|
||||
|
||||
|
||||
Return Values
|
||||
-------------
|
||||
Common return values are documented `here <https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#common-return-values>`_, the following are the fields unique to this filter:
|
||||
|
||||
.. raw:: html
|
||||
|
||||
<table border=0 cellpadding=0 class="documentation-table">
|
||||
<tr>
|
||||
<th colspan="1">Key</th>
|
||||
<th>Returned</th>
|
||||
<th width="100%">Description</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="1">
|
||||
<div class="ansibleOptionAnchor" id="return-"></div>
|
||||
<b>data</b>
|
||||
<a class="ansibleOptionLink" href="#return-" title="Permalink to this return value"></a>
|
||||
<div style="font-size: small">
|
||||
<span style="color: purple">list</span>
|
||||
/ <span style="color: purple">elements=string</span>
|
||||
</div>
|
||||
</td>
|
||||
<td></td>
|
||||
<td>
|
||||
<div>Returns list with values valid for a particular query.</div>
|
||||
<br/>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<br/><br/>
|
||||
|
||||
|
||||
Status
|
||||
------
|
||||
|
||||
|
||||
Authors
|
||||
~~~~~~~
|
||||
|
||||
- Ashwini Mhatre (@amhatre)
|
||||
|
||||
|
||||
.. hint::
|
||||
Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.
|
|
@ -0,0 +1,179 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2021 Red Hat
|
||||
# GNU General Public License v3.0+
|
||||
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
"""
|
||||
filter plugin file for ipaddr filters: ipv6
|
||||
"""
|
||||
from __future__ import absolute_import, division, print_function
|
||||
from functools import partial
|
||||
from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddr_utils import (
|
||||
ipaddr,
|
||||
_need_netaddr,
|
||||
)
|
||||
from ansible.errors import AnsibleFilterError
|
||||
from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import (
|
||||
AnsibleArgSpecValidator,
|
||||
)
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
try:
|
||||
from jinja2.filters import pass_environment
|
||||
except ImportError:
|
||||
from jinja2.filters import environmentfilter as pass_environment
|
||||
|
||||
try:
|
||||
import netaddr
|
||||
|
||||
HAS_NETADDR = True
|
||||
except ImportError:
|
||||
# in this case, we'll make the filters return error messages (see bottom)
|
||||
HAS_NETADDR = False
|
||||
else:
|
||||
|
||||
class mac_linux(netaddr.mac_unix):
|
||||
pass
|
||||
|
||||
mac_linux.word_fmt = "%.2x"
|
||||
|
||||
DOCUMENTATION = """
|
||||
name: ipv6
|
||||
author: Ashwini Mhatre (@amhatre)
|
||||
version_added: "2.5.0"
|
||||
short_description: To filter only Ipv6 addresses Ipv6 filter is used.
|
||||
description:
|
||||
- Sometimes you need only IPv6 addresses. To filter only Ipv6 addresses Ipv6 filter is used.
|
||||
options:
|
||||
value:
|
||||
description:
|
||||
- list of subnets or individual address or any other values input for ipv6 plugin
|
||||
type: list
|
||||
elements: str
|
||||
required: True
|
||||
query:
|
||||
description:
|
||||
- You can provide a single argument to each ipv6() filter.
|
||||
- Example. query type 'ipv4' to convert ipv6 into ipv4
|
||||
type: str
|
||||
default: ''
|
||||
notes:
|
||||
"""
|
||||
|
||||
EXAMPLES = r"""
|
||||
#### examples
|
||||
# Ipv6 filter plugin with different queries.
|
||||
- name: Set value as input list
|
||||
ansible.builtin.set_fact:
|
||||
value:
|
||||
- 192.24.2.1
|
||||
- ::ffff:192.168.32.0/120
|
||||
- ''
|
||||
- ::ffff:192.24.2.1/128
|
||||
- 192.168.32.0/24
|
||||
- fe80::100/10
|
||||
- True
|
||||
- name: IPv6 filter to filter Ipv6 Address
|
||||
debug:
|
||||
msg: "{{ value|ansible.utils.ipv6 }}"
|
||||
|
||||
- name: convert IPv6 addresses into IPv4 addresses.
|
||||
debug:
|
||||
msg: "{{ value|ansible.utils.ipv6('ipv4') }}"
|
||||
|
||||
- name: filter only IPv6 addresses.
|
||||
debug:
|
||||
msg: "{{ value|ansible.utils.ipv6('address') }}"
|
||||
|
||||
|
||||
# PLAY [Ipv6 filter plugin with different queries.] ******************************************************************
|
||||
# TASK [Set value as input list] ***************************************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "ansible_facts": {
|
||||
# "value": [
|
||||
# "192.24.2.1",
|
||||
# "::ffff:192.168.32.0/120",
|
||||
# "",
|
||||
# "::ffff:192.24.2.1/128",
|
||||
# "192.168.32.0/24",
|
||||
# "fe80::100/10",
|
||||
# true
|
||||
# ]
|
||||
# },
|
||||
# "changed": false
|
||||
# }
|
||||
#
|
||||
# TASK [IPv6 filter to filter Ipv6 Address] ****************************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "msg": [
|
||||
# "::ffff:192.168.32.0/120",
|
||||
# "::ffff:192.24.2.1/128",
|
||||
# "fe80::100/10"
|
||||
# ]
|
||||
# }
|
||||
#
|
||||
# TASK [convert IPv6 addresses into IPv4 addresses.] *******************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "msg": [
|
||||
# "192.168.32.0/24",
|
||||
# "192.24.2.1/32"
|
||||
# ]
|
||||
# }
|
||||
#
|
||||
# TASK [filter only IPv6 addresses] *******************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "msg": [
|
||||
# "::ffff:192.168.32.0",
|
||||
# "::ffff:192.24.2.1",
|
||||
# "fe80::100"
|
||||
# ]
|
||||
# }
|
||||
#
|
||||
|
||||
"""
|
||||
|
||||
RETURN = """
|
||||
data:
|
||||
type: list
|
||||
elements: str
|
||||
description:
|
||||
- Returns list with values valid for a particular query.
|
||||
"""
|
||||
|
||||
|
||||
@pass_environment
|
||||
def _ipv6(*args, **kwargs):
|
||||
"""This filter is designed to return the input value if a query is True, and False if a query is False"""
|
||||
keys = ["value", "query"]
|
||||
data = dict(zip(keys, args[1:]))
|
||||
data.update(kwargs)
|
||||
aav = AnsibleArgSpecValidator(data=data, schema=DOCUMENTATION, name="ipv6")
|
||||
valid, errors, updated_data = aav.validate()
|
||||
if not valid:
|
||||
raise AnsibleFilterError(errors)
|
||||
return ipv6(**updated_data)
|
||||
|
||||
|
||||
def ipv6(value, query=""):
|
||||
return ipaddr(value, query, version=6, alias="ipv6")
|
||||
|
||||
|
||||
class FilterModule(object):
|
||||
"""IP address and network manipulation filters
|
||||
"""
|
||||
|
||||
filter_map = {
|
||||
# IP addresses and networks
|
||||
"ipv6": _ipv6
|
||||
}
|
||||
|
||||
def filters(self):
|
||||
""" ipv6 filter """
|
||||
if HAS_NETADDR:
|
||||
return self.filter_map
|
||||
else:
|
||||
return dict(
|
||||
(f, partial(_need_netaddr, f)) for f in self.filter_map
|
||||
)
|
|
@ -0,0 +1,35 @@
|
|||
---
|
||||
- name: set ipaddress list
|
||||
ansible.builtin.set_fact:
|
||||
value:
|
||||
- 192.24.2.1
|
||||
- ::ffff:192.168.32.0/120
|
||||
- ''
|
||||
- ::ffff:192.24.2.1/128
|
||||
- 192.168.32.0/24
|
||||
- fe80::100/10
|
||||
- True
|
||||
|
||||
- name: ipv6 filter
|
||||
ansible.builtin.set_fact:
|
||||
result1: "{{ value|ansible.utils.ipv6 }}"
|
||||
|
||||
- name: Assert result for ipv6.
|
||||
assert:
|
||||
that: "{{ result1 == ipv6_result1 }}"
|
||||
|
||||
- name: convert ipv6 to ipv4 filter
|
||||
ansible.builtin.set_fact:
|
||||
result2: "{{ value|ansible.utils.ipv6('ipv4') }}"
|
||||
|
||||
- name: Assert result for ipv4.
|
||||
assert:
|
||||
that: "{{ result2 == ipv6_result2 }}"
|
||||
|
||||
- name: Ipv6 filter with address query
|
||||
ansible.builtin.set_fact:
|
||||
result3: "{{ value|ansible.utils.ipv6('address') }}"
|
||||
|
||||
- name: Assert result for ipv6 filter with address query.
|
||||
assert:
|
||||
that: "{{ result3 == ipv6_result3 }}"
|
|
@ -10,6 +10,20 @@ result2_val:
|
|||
result3_val:
|
||||
- "192.24.2.1"
|
||||
|
||||
ipv6_result1:
|
||||
- "::ffff:192.168.32.0/120"
|
||||
- "::ffff:192.24.2.1/128"
|
||||
- "fe80::100/10"
|
||||
|
||||
ipv6_result2:
|
||||
- "192.168.32.0/24"
|
||||
- "192.24.2.1/32"
|
||||
|
||||
ipv6_result3:
|
||||
- "::ffff:192.168.32.0"
|
||||
- "::ffff:192.24.2.1"
|
||||
- "fe80::100"
|
||||
|
||||
cidr_result1:
|
||||
- 192.168.0.0/16
|
||||
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
# -*- 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 ipwrap filter plugin
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import unittest
|
||||
from ansible_collections.ansible.utils.plugins.filter.ipv6 import _ipv6
|
||||
|
||||
|
||||
VALID_DATA = [
|
||||
"192.24.2.1",
|
||||
"::ffff:192.168.32.0/120",
|
||||
"",
|
||||
"::ffff:192.24.2.1/128",
|
||||
"192.168.32.0/24",
|
||||
"fe80::100/10",
|
||||
True,
|
||||
]
|
||||
|
||||
|
||||
VALID_OUTPUT = [
|
||||
"::ffff:192.168.32.0/120",
|
||||
"::ffff:192.24.2.1/128",
|
||||
"fe80::100/10",
|
||||
]
|
||||
|
||||
VALID_OUTPUT1 = ["192.168.32.0/24", "192.24.2.1/32"]
|
||||
|
||||
VALID_OUTPUT2 = ["::ffff:192.168.32.0", "::ffff:192.24.2.1", "fe80::100"]
|
||||
|
||||
|
||||
class TestIp6(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_ipv6_filter_empty_query(self):
|
||||
"""Check ipv6 filter empty query"""
|
||||
args = ["", VALID_DATA, ""]
|
||||
result = _ipv6(*args)
|
||||
self.assertEqual(result, VALID_OUTPUT)
|
||||
|
||||
def test_ipv6_ipv4_conversion(self):
|
||||
"""Check ipv6 to ipv4 conversion"""
|
||||
args = ["", VALID_DATA, "ipv4"]
|
||||
result = _ipv6(*args)
|
||||
self.assertEqual(result, VALID_OUTPUT1)
|
||||
|
||||
def test_ipv6_filter_address_query(self):
|
||||
"""Check ipv6 filter address query"""
|
||||
args = ["", VALID_DATA, "address"]
|
||||
result = _ipv6(*args)
|
||||
self.assertEqual(result, VALID_OUTPUT2)
|
Loading…
Reference in New Issue