2022-01-20 13:10:31 +00:00
|
|
|
# -*- 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
|
2022-05-26 17:18:57 +00:00
|
|
|
|
2022-01-20 13:10:31 +00:00
|
|
|
from functools import partial
|
2022-05-26 17:18:57 +00:00
|
|
|
|
2022-10-14 14:55:05 +00:00
|
|
|
from ansible.errors import AnsibleFilterError
|
2022-05-26 17:18:57 +00:00
|
|
|
|
2022-01-20 13:10:31 +00:00
|
|
|
from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import (
|
|
|
|
AnsibleArgSpecValidator,
|
|
|
|
)
|
2022-05-26 17:18:57 +00:00
|
|
|
from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddr_utils import (
|
|
|
|
_need_netaddr,
|
|
|
|
ipaddr,
|
|
|
|
)
|
|
|
|
|
2022-01-20 13:10:31 +00:00
|
|
|
|
|
|
|
__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
|
2022-03-04 11:43:59 +00:00
|
|
|
type: raw
|
2022-01-20 13:10:31 +00:00
|
|
|
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:
|
2023-11-13 05:58:43 +00:00
|
|
|
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
|
2022-01-20 13:10:31 +00:00
|
|
|
- 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:
|
2022-03-04 11:43:59 +00:00
|
|
|
type: raw
|
2022-01-20 13:10:31 +00:00
|
|
|
description:
|
2022-03-04 11:43:59 +00:00
|
|
|
- Returns values valid for a particular query.
|
2022-01-20 13:10:31 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
@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)
|
2022-03-04 11:43:59 +00:00
|
|
|
try:
|
|
|
|
if isinstance(data["value"], str):
|
|
|
|
pass
|
|
|
|
elif isinstance(data["value"], list):
|
|
|
|
pass
|
2022-11-21 09:48:35 +00:00
|
|
|
elif isinstance(data["value"], int):
|
|
|
|
pass
|
2022-03-04 11:43:59 +00:00
|
|
|
else:
|
2022-10-14 14:55:05 +00:00
|
|
|
raise AnsibleFilterError(
|
2022-06-01 12:35:10 +00:00
|
|
|
"Unrecognized type <{0}> for ipv6 filter <{1}>".format(
|
|
|
|
type(data["value"]),
|
|
|
|
"value",
|
|
|
|
),
|
2022-03-04 11:43:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
except (TypeError, ValueError):
|
2022-10-14 14:55:05 +00:00
|
|
|
raise AnsibleFilterError(
|
2022-06-01 12:35:10 +00:00
|
|
|
"Unrecognized type <{0}> for ipv6 filter <{1}>".format(type(data["value"]), "value"),
|
2022-03-04 11:43:59 +00:00
|
|
|
)
|
2022-01-20 13:10:31 +00:00
|
|
|
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):
|
2022-04-06 15:30:29 +00:00
|
|
|
"""IP address and network manipulation filters"""
|
2022-01-20 13:10:31 +00:00
|
|
|
|
|
|
|
filter_map = {
|
|
|
|
# IP addresses and networks
|
2022-06-01 12:35:10 +00:00
|
|
|
"ipv6": _ipv6,
|
2022-01-20 13:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def filters(self):
|
2022-04-06 15:30:29 +00:00
|
|
|
"""ipv6 filter"""
|
2022-01-20 13:10:31 +00:00
|
|
|
if HAS_NETADDR:
|
|
|
|
return self.filter_map
|
|
|
|
else:
|
2022-05-26 17:18:57 +00:00
|
|
|
return dict((f, partial(_need_netaddr, f)) for f in self.filter_map)
|