Add ipaddr Filter plugin (#115)

Add ipaddr Filter plugin

SUMMARY

depend-on: #113

ISSUE TYPE


New Module Pull Request

COMPONENT NAME

ADDITIONAL INFORMATION

Reviewed-by: Nathaniel Case <this.is@nathanielca.se>
Reviewed-by: Ashwini Mhatre <mashu97@gmail.com>
Reviewed-by: Sagar Paul <sagpaul@redhat.com>
Reviewed-by: None <None>
pull/129/head^2
Ashwini Mhatre 2022-01-24 16:45:16 +05:30 committed by GitHub
parent cfc7965400
commit 4488b79b04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 862 additions and 0 deletions

View File

@ -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.ipaddr](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipaddr_filter.rst)|This filter is designed to return the input value if a query is True, else False.
[ansible.utils.ipwrap](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipwrap_filter.rst)|This filter is designed to Wrap IPv6 addresses in [ ] brackets.
[ansible.utils.network_in_usable](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.network_in_usable_filter.rst)|The network_in_usable filter returns whether an address passed as an argument is usable in a network.
[ansible.utils.network_in_network](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.network_in_network_filter.rst)|This filter returns whether an address or a network passed as argument is in a network.

View File

@ -0,0 +1,3 @@
---
minor_changes:
- Add ipaddr filter plugin.

View File

@ -0,0 +1,330 @@
.. _ansible.utils.ipaddr_filter:
********************
ansible.utils.ipaddr
********************
**This filter is designed to return the input value if a query is True, else False.**
Version added: 2.5.0
.. contents::
:local:
:depth: 1
Synopsis
--------
- This filter is designed to return the input value if a query is True, and False if a query is False
- This way it can be easily used in chained filters
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>alias</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>
</td>
<td>
</td>
<td>
<div>type of filter. example ipaddr, ipv4, ipv6, ipwrap</div>
</td>
</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 ipaddr() filter.</div>
<div>The filter will then treat it as a query and return values modified by that query.</div>
<div>{&#x27;Types of queries include&#x27;: [{&#x27;query by name&#x27;: &quot;ansible.utils.ipaddr(&#x27;address&#x27;), ansible.utils.ipv4(&#x27;network&#x27;);&quot;}, {&#x27;query by CIDR range&#x27;: &quot;ansible.utils.ipaddr(&#x27;192.168.0.0/24&#x27;), ansible.utils.ipv6(&#x27;2001:db8::/32&#x27;);&quot;}, {&#x27;query by index number&#x27;: &quot;ansible.utils.ipaddr(&#x27;1&#x27;), ansible.utils.ipaddr(&#x27;-1&#x27;);&quot;}]}</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 ipaddr plugin</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>version</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">integer</span>
</div>
</td>
<td>
</td>
<td>
</td>
<td>
<div>Ip version 4 or 6</div>
</td>
</tr>
</table>
<br/>
Examples
--------
.. code-block:: yaml
#### examples
# Ipaddr filter plugin with different queries.
- name: Set value as input list
ansible.builtin.set_fact:
value:
- 192.24.2.1
- host.fqdn
- ::1
- ''
- 192.168.32.0/24
- fe80::100/10
- 42540766412265424405338506004571095040/64
- True
- debug:
msg: "{{ value|ansible.utils.ipaddr }}"
- name: Fetch only those elements that are host IP addresses and not network ranges
debug:
msg: "{{ value|ansible.utils.ipaddr('address') }}"
- name: |
Fetch only host IP addresses with their correct CIDR prefixes (as is common with IPv6 addressing), you can use
the ipaddr('host') filter.
debug:
msg: "{{ value|ansible.utils.ipaddr('host') }}"
- name: check if IP addresses or network ranges are accessible on a public Internet and return it.
debug:
msg: "{{ value|ansible.utils.ipaddr('public') }}"
- name: check if IP addresses or network ranges are accessible on a private Internet and return it.
debug:
msg: "{{ value|ansible.utils.ipaddr('private') }}"
- name: check which values are values are specifically network ranges and return it.
debug:
msg: "{{ value|ansible.utils.ipaddr('net') }}"
- name: check how many IP addresses can be in a certain range.
debug:
msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('size') }}"
- name: By specifying a network range as a query, you can check if a given value is in that range.
debug:
msg: "{{ value|ansible.utils.ipaddr('192.0.0.0/8') }}"
# First IP address (network address)
- name: |
If you specify a positive or negative integer as a query, ipaddr() will treat this as an index and will return
the specific IP address from a network range, in the "host/prefix" format.
debug:
msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('0') }}"
# Second IP address (usually the gateway host)
- debug:
msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('1') }}"
# Last IP address (the broadcast address in IPv4 networks)
- debug:
msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('-1') }}"
# PLAY [Ipaddr filter plugin with different queries.] ******************************************************************
# TASK [Set value as input list] ***************************************************************************************
# ok: [localhost] => {"ansible_facts": {"value": ["192.24.2.1", "host.fqdn", "::1", "", "192.168.32.0/24",
# "fe80::100/10", "42540766412265424405338506004571095040/64", true]}, "changed": false}
#
# TASK [debug] ********************************************************************************************************
# ok: [localhost] => {
# "msg": [
# "192.24.2.1",
# "::1",
# "192.168.32.0/24",
# "fe80::100/10",
# "2001:db8:32c:faad::/64"
# ]
# }
#
# TASK [Fetch only those elements that are host IP addresses and not network ranges] ***********************************
# ok: [localhost] => {
# "msg": [
# "192.24.2.1",
# "::1",
# "fe80::100",
# "2001:db8:32c:faad::"
# ]
# }
#
# TASK [Fetch only host IP addresses with their correct CIDR prefixes (as is common with IPv6 addressing), you can use
# the ipaddr('host') filter.] *****************
# ok: [localhost] => {
# "msg": [
# "192.24.2.1/32",
# "::1/128",
# "fe80::100/10"
# ]
# }
#
# TASK [check if IP addresses or network ranges are accessible on a public Internet and return it.] ********************
# ok: [localhost] => {
# "msg": [
# "192.24.2.1",
# "2001:db8:32c:faad::/64"
# ]
# }
#
# TASK [check if IP addresses or network ranges are accessible on a private Internet and return it.] *******************
# ok: [localhost] => {
# "msg": [
# "192.168.32.0/24",
# "fe80::100/10"
# ]
# }
#
# TASK [check which values are values are specifically network ranges and return it.] **********************************
# ok: [localhost] => {
# "msg": [
# "192.168.32.0/24",
# "2001:db8:32c:faad::/64"
# ]
# }
#
# TASK [check how many IP addresses can be in a certain range.] *********************************************************
# ok: [localhost] => {
# "msg": [
# 256,
# 18446744073709551616
# ]
# }
#
# TASK [By specifying a network range as a query, you can check if a given value is in that range.] ********************
# ok: [localhost] => {
# "msg": [
# "192.24.2.1",
# "192.168.32.0/24"
# ]
# }
#
# TASK [If you specify a positive or negative integer as a query, ipaddr() will treat this as an index and will
# return the specific IP address from a network range, in the "host/prefix" format.] ***
# ok: [localhost] => {
# "msg": [
# "192.168.32.0/24",
# "2001:db8:32c:faad::/64"
# ]
# }
#
# TASK [debug] *********************************************************************************************************
# ok: [localhost] => {
# "msg": [
# "192.168.32.1/24",
# "2001:db8:32c:faad::1/64"
# ]
# }
#
# TASK [debug] ********************************************************************************************************
# ok: [localhost] => {
# "msg": [
# "192.168.32.255/24",
# "2001:db8:32c:faad:ffff:ffff:ffff:ffff/64"
# ]
# }
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.

281
plugins/filter/ipaddr.py Normal file
View File

@ -0,0 +1,281 @@
# -*- 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: cidr_merge
"""
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: ipaddr
author: Ashwini Mhatre (@amhatre)
version_added: "2.5.0"
short_description: This filter is designed to return the input value if a query is True, else False.
description:
- This filter is designed to return the input value if a query is True, and False if a query is False
- This way it can be easily used in chained filters
options:
value:
description:
- list of subnets or individual address or any other values input for ipaddr plugin
type: list
elements: str
required: True
query:
description:
- You can provide a single argument to each ipaddr() filter.
- The filter will then treat it as a query and return values modified by that query.
- Types of queries include:
- query by name: ansible.utils.ipaddr('address'), ansible.utils.ipv4('network');
- query by CIDR range: ansible.utils.ipaddr('192.168.0.0/24'), ansible.utils.ipv6('2001:db8::/32');
- query by index number: ansible.utils.ipaddr('1'), ansible.utils.ipaddr('-1');
type: str
default: ''
version:
type: int
description: Ip version 4 or 6
alias:
type: str
description: type of filter. example ipaddr, ipv4, ipv6, ipwrap
notes:
"""
EXAMPLES = r"""
#### examples
# Ipaddr filter plugin with different queries.
- name: Set value as input list
ansible.builtin.set_fact:
value:
- 192.24.2.1
- host.fqdn
- ::1
- ''
- 192.168.32.0/24
- fe80::100/10
- 42540766412265424405338506004571095040/64
- True
- debug:
msg: "{{ value|ansible.utils.ipaddr }}"
- name: Fetch only those elements that are host IP addresses and not network ranges
debug:
msg: "{{ value|ansible.utils.ipaddr('address') }}"
- name: |
Fetch only host IP addresses with their correct CIDR prefixes (as is common with IPv6 addressing), you can use
the ipaddr('host') filter.
debug:
msg: "{{ value|ansible.utils.ipaddr('host') }}"
- name: check if IP addresses or network ranges are accessible on a public Internet and return it.
debug:
msg: "{{ value|ansible.utils.ipaddr('public') }}"
- name: check if IP addresses or network ranges are accessible on a private Internet and return it.
debug:
msg: "{{ value|ansible.utils.ipaddr('private') }}"
- name: check which values are values are specifically network ranges and return it.
debug:
msg: "{{ value|ansible.utils.ipaddr('net') }}"
- name: check how many IP addresses can be in a certain range.
debug:
msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('size') }}"
- name: By specifying a network range as a query, you can check if a given value is in that range.
debug:
msg: "{{ value|ansible.utils.ipaddr('192.0.0.0/8') }}"
# First IP address (network address)
- name: |
If you specify a positive or negative integer as a query, ipaddr() will treat this as an index and will return
the specific IP address from a network range, in the "host/prefix" format.
debug:
msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('0') }}"
# Second IP address (usually the gateway host)
- debug:
msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('1') }}"
# Last IP address (the broadcast address in IPv4 networks)
- debug:
msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('-1') }}"
# PLAY [Ipaddr filter plugin with different queries.] ******************************************************************
# TASK [Set value as input list] ***************************************************************************************
# ok: [localhost] => {"ansible_facts": {"value": ["192.24.2.1", "host.fqdn", "::1", "", "192.168.32.0/24",
# "fe80::100/10", "42540766412265424405338506004571095040/64", true]}, "changed": false}
#
# TASK [debug] ********************************************************************************************************
# ok: [localhost] => {
# "msg": [
# "192.24.2.1",
# "::1",
# "192.168.32.0/24",
# "fe80::100/10",
# "2001:db8:32c:faad::/64"
# ]
# }
#
# TASK [Fetch only those elements that are host IP addresses and not network ranges] ***********************************
# ok: [localhost] => {
# "msg": [
# "192.24.2.1",
# "::1",
# "fe80::100",
# "2001:db8:32c:faad::"
# ]
# }
#
# TASK [Fetch only host IP addresses with their correct CIDR prefixes (as is common with IPv6 addressing), you can use
# the ipaddr('host') filter.] *****************
# ok: [localhost] => {
# "msg": [
# "192.24.2.1/32",
# "::1/128",
# "fe80::100/10"
# ]
# }
#
# TASK [check if IP addresses or network ranges are accessible on a public Internet and return it.] ********************
# ok: [localhost] => {
# "msg": [
# "192.24.2.1",
# "2001:db8:32c:faad::/64"
# ]
# }
#
# TASK [check if IP addresses or network ranges are accessible on a private Internet and return it.] *******************
# ok: [localhost] => {
# "msg": [
# "192.168.32.0/24",
# "fe80::100/10"
# ]
# }
#
# TASK [check which values are values are specifically network ranges and return it.] **********************************
# ok: [localhost] => {
# "msg": [
# "192.168.32.0/24",
# "2001:db8:32c:faad::/64"
# ]
# }
#
# TASK [check how many IP addresses can be in a certain range.] *********************************************************
# ok: [localhost] => {
# "msg": [
# 256,
# 18446744073709551616
# ]
# }
#
# TASK [By specifying a network range as a query, you can check if a given value is in that range.] ********************
# ok: [localhost] => {
# "msg": [
# "192.24.2.1",
# "192.168.32.0/24"
# ]
# }
#
# TASK [If you specify a positive or negative integer as a query, ipaddr() will treat this as an index and will
# return the specific IP address from a network range, in the "host/prefix" format.] ***
# ok: [localhost] => {
# "msg": [
# "192.168.32.0/24",
# "2001:db8:32c:faad::/64"
# ]
# }
#
# TASK [debug] *********************************************************************************************************
# ok: [localhost] => {
# "msg": [
# "192.168.32.1/24",
# "2001:db8:32c:faad::1/64"
# ]
# }
#
# TASK [debug] ********************************************************************************************************
# ok: [localhost] => {
# "msg": [
# "192.168.32.255/24",
# "2001:db8:32c:faad:ffff:ffff:ffff:ffff/64"
# ]
# }
"""
RETURN = """
data:
type: list
elements: str
description:
- Returns list with values valid for a particular query.
"""
@pass_environment
def _ipaddr(*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", "version", "alias"]
data = dict(zip(keys, args[1:]))
data.update(kwargs)
aav = AnsibleArgSpecValidator(
data=data, schema=DOCUMENTATION, name="ipaddr"
)
valid, errors, updated_data = aav.validate()
if not valid:
raise AnsibleFilterError(errors)
return ipaddr(**updated_data)
class FilterModule(object):
"""IP address and network manipulation filters
"""
filter_map = {
# IP addresses and networks
"ipaddr": _ipaddr
}
def filters(self):
""" ipaddr filter """
if HAS_NETADDR:
return self.filter_map
else:
return dict(
(f, partial(_need_netaddr, f)) for f in self.filter_map
)

View File

@ -0,0 +1,44 @@
---
- name: set ipaddress list
ansible.builtin.set_fact:
value:
- 192.24.2.1
- host.fqdn
- ::1
- 192.168.32.0/24
- fe80::100/10
- "42540766412265424405338506004571095040/64"
- True
- name: ipaddr filter with empty string query
ansible.builtin.set_fact:
result1: "{{ value|ansible.utils.ipaddr }}"
- name: Assert result for ipaddr.
assert:
that: "{{ result1 == ipaddr_result1 }}"
- name: ipaddr filter with host string query
ansible.builtin.set_fact:
result2: "{{ value|ansible.utils.ipaddr('host') }}"
- name: Assert result for ipaddr host query
assert:
that: "{{ result2 == ipaddr_result2 }}"
- name: ipaddr filter with public network query
ansible.builtin.set_fact:
result3: "{{ value|ansible.utils.ipaddr('public') }}"
- name: Assert result for ipaddr public network query
assert:
that: "{{ result3== ipaddr_result3 }}"
- name: ipaddr filter with network query
ansible.builtin.set_fact:
result4: "{{ value|ansible.utils.ipaddr('net') }}"
- name: Assert result for ipaddr network query
assert:
that: "{{ result4 == ipaddr_result4 }}"

View File

@ -12,6 +12,26 @@ result2_val:
result3_val:
- "192.24.2.1"
ipaddr_result1:
- 192.24.2.1
- ::1
- 192.168.32.0/24
- fe80::100/10
- 2001:db8:32c:faad::/64
ipaddr_result2:
- 192.24.2.1/32
- ::1/128
- fe80::100/10
ipaddr_result3:
- 192.24.2.1
- 2001:db8:32c:faad::/64
ipaddr_result4:
- 192.168.32.0/24
- 2001:db8:32c:faad::/64
ipwrap_result1:
- "192.24.2.1"
- "host.fqdn"

View File

@ -0,0 +1,183 @@
# -*- 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 cidr_merge filter plugin
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import unittest
from ansible.errors import AnsibleFilterError
from ansible_collections.ansible.utils.plugins.filter.ipaddr import _ipaddr
VALID_DATA = [
"192.24.2.1",
"host.fqdn",
"::1",
"",
"192.168.32.0/24",
"fe80::100/10",
"42540766412265424405338506004571095040/64",
True,
]
VALID_DATA1 = ["192.168.32.0/24", "2001:db8:32c:faad::/64"]
VALID_OUTPUT = [
"192.24.2.1",
"::1",
"192.168.32.0/24",
"fe80::100/10",
"2001:db8:32c:faad::/64",
]
VALID_OUTPUT1 = ["192.24.2.1", "::1", "fe80::100", "2001:db8:32c:faad::"]
VALID_OUTPUT2 = ["192.24.2.1/32", "::1/128", "fe80::100/10"]
VALID_OUTPUT3 = ["192.24.2.1", "2001:db8:32c:faad::/64"]
VALID_OUTPUT4 = ["192.168.32.0/24", "fe80::100/10"]
VALID_OUTPUT5 = ["192.168.32.0/24", "2001:db8:32c:faad::/64"]
VALID_OUTPUT6 = [256, 18446744073709551616]
VALID_OUTPUT7 = ["192.24.2.1", "192.168.32.0/24"]
VALID_OUTPUT8 = ["192.168.32.0/24", "2001:db8:32c:faad::/64"]
# ansible_default_ipv4 = {
# "address": "192.168.0.11",
# "alias": "eth0",
# "broadcast": "192.168.0.255",
# "gateway": "192.168.0.1",
# "interface": "eth0",
# "macaddress": "fa:16:3e:c4:bd:89",
# "mtu": 1500,
# "netmask": "255.255.255.0",
# "network": "192.168.0.0",
# "type": "ether"
# }
net_mask = "192.168.0.0/255.255.255.0"
host_prefix = ["2001:db8:deaf:be11::ef3/64", "192.0.2.48/24", "192.168.0.0/16"]
class TestIpAddr(unittest.TestCase):
def setUp(self):
pass
def test_valid_data_empty(self):
"""Check passing invalid argspec"""
args = ["", VALID_DATA, ""]
result = _ipaddr(*args)
self.assertEqual(result, VALID_OUTPUT)
def test_valid_data_address(self):
"""Check passing invalid argspec"""
args = ["", VALID_DATA, "address"]
result = _ipaddr(*args)
self.assertEqual(result, VALID_OUTPUT1)
def test_valid_data_host(self):
"""Check passing invalid argspec"""
args = ["", VALID_DATA, "host"]
result = _ipaddr(*args)
self.assertEqual(result, VALID_OUTPUT2)
def test_valid_data_public(self):
"""Check passing invalid argspec"""
args = ["", VALID_DATA, "public"]
result = _ipaddr(*args)
self.assertEqual(result, VALID_OUTPUT3)
def test_valid_data_private(self):
"""Check passing invalid argspec"""
args = ["", VALID_DATA, "private"]
result = _ipaddr(*args)
self.assertEqual(result, VALID_OUTPUT4)
def test_valid_data_net(self):
"""Check passing invalid argspec"""
args = ["", VALID_DATA, "net"]
result = _ipaddr(*args)
self.assertEqual(result, VALID_OUTPUT5)
def test_valid_data_size(self):
"""Check passing invalid argspec"""
args = ["", VALID_DATA1, "size"]
result = _ipaddr(*args)
self.assertEqual(result, VALID_OUTPUT6)
def test_valid_data_network_range(self):
"""Check passing invalid argspec"""
args = ["", VALID_DATA, "192.0.0.0/8"]
result = _ipaddr(*args)
self.assertEqual(result, VALID_OUTPUT7)
def test_valid_data_with_index(self):
"""Check passing invalid argspec"""
args = ["", VALID_DATA1, "0"]
result = _ipaddr(*args)
self.assertEqual(result, VALID_OUTPUT8)
def test_invalid_data(self):
"""Check passing invalid argspec"""
args = ["", VALID_DATA, "tftftf"]
kwargs = {}
with self.assertRaises(AnsibleFilterError) as error:
_ipaddr(*args, **kwargs)
self.assertIn("unknown filter type: tftftf", str(error.exception))
def test_valid_data_with_prefix(self):
"""Check passing invalid argspec"""
args = ["", net_mask, "prefix"]
result = _ipaddr(*args)
self.assertEqual(result, [24])
def test_valid_data_with_host_prefix(self):
"""Check passing invalid argspec"""
args = ["", host_prefix, "host/prefix"]
result = _ipaddr(*args)
self.assertEqual(
result, ["2001:db8:deaf:be11::ef3/64", "192.0.2.48/24"]
)
def test_valid_data_with_network_prefix(self):
"""Check passing invalid argspec"""
ipaddress = "192.168.0.11/255.255.255.0"
args = ["", ipaddress, "network/prefix"]
result = _ipaddr(*args)
self.assertEqual(result, ["192.168.0.0/24"])
def test_valid_data_with_subnet(self):
"""Check passing invalid argspec"""
args = ["", host_prefix, "host/prefix"]
result1 = _ipaddr(*args)
args = ["", result1, "subnet"]
result = _ipaddr(*args)
self.assertEqual(result, ["2001:db8:deaf:be11::/64", "192.0.2.0/24"])