Add hwaddr, macaddr, slaac filter plugin (#129)
Add hwaddr, macaddr, slaac filter plugin SUMMARY Added hwaddr, macaddr, slaac filter plugin 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/110/head^2
parent
4488b79b04
commit
ea54e97359
|
@ -24,7 +24,10 @@ Name | Description
|
|||
[ansible.utils.cidr_merge](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.cidr_merge_filter.rst)|This filter can be used to merge subnets or individual addresses.
|
||||
[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.hwaddr](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.hwaddr_filter.rst)|HWaddr / MAC address filters
|
||||
[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.macaddr](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.macaddr_filter.rst)|macaddr / MAC address filters
|
||||
[ansible.utils.slaac](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.slaac_filter.rst)|This filter returns the SLAAC address within a network for a given HW/MAC address.
|
||||
[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.
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
minor_changes:
|
||||
- Add slaac,hwaddr,mac filter plugin.
|
|
@ -0,0 +1,166 @@
|
|||
.. _ansible.utils.hwaddr_filter:
|
||||
|
||||
|
||||
********************
|
||||
ansible.utils.hwaddr
|
||||
********************
|
||||
|
||||
**HWaddr / MAC address filters**
|
||||
|
||||
|
||||
Version added: 2.5.0
|
||||
|
||||
.. contents::
|
||||
:local:
|
||||
:depth: 1
|
||||
|
||||
|
||||
Synopsis
|
||||
--------
|
||||
- This filter check if string is a HW/MAC address and filter it
|
||||
- You can use the hwaddr() filter to check if a given string is a MAC address or convert it between various formats.
|
||||
|
||||
|
||||
|
||||
|
||||
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>
|
||||
<b>Default:</b><br/><div style="color: blue">"hwaddr"</div>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
<div>alias</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>query string. Example. cisco,linux,unix etc</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">string</span>
|
||||
/ <span style="color: red">required</span>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
<div>HW/MAC address.</div>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<br/>
|
||||
|
||||
|
||||
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
#### examples
|
||||
- name: Check if given string is a MAC address
|
||||
debug:
|
||||
msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.hwaddr }}"
|
||||
|
||||
- name: Convert HW address to Cisco format
|
||||
debug:
|
||||
msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.hwaddr('cisco') }}"
|
||||
|
||||
# TASK [Check if given string is a MAC address] ***************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "msg": "1a:2b:3c:4d:5e:6f"
|
||||
# }
|
||||
#
|
||||
# TASK [Convert HW address to Cisco format] ******************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "msg": "1a2b.3c4d.5e6f"
|
||||
# }
|
||||
|
||||
|
||||
|
||||
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">string</span>
|
||||
</div>
|
||||
</td>
|
||||
<td></td>
|
||||
<td>
|
||||
<div>mac/Hw address</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,148 @@
|
|||
.. _ansible.utils.macaddr_filter:
|
||||
|
||||
|
||||
*********************
|
||||
ansible.utils.macaddr
|
||||
*********************
|
||||
|
||||
**macaddr / MAC address filters**
|
||||
|
||||
|
||||
Version added: 2.5.0
|
||||
|
||||
.. contents::
|
||||
:local:
|
||||
:depth: 1
|
||||
|
||||
|
||||
Synopsis
|
||||
--------
|
||||
- This filter check if string is a MAC address and filter it
|
||||
- You can use the macaddr() filter to check if a given string is a MAC address or convert it between various formats.
|
||||
|
||||
|
||||
|
||||
|
||||
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>query string. Example. cisco,linux,unix etc</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">string</span>
|
||||
/ <span style="color: red">required</span>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
<div>HW/MAC address.</div>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<br/>
|
||||
|
||||
|
||||
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
#### examples
|
||||
- name: Check if given string is a MAC address
|
||||
debug:
|
||||
msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.macaddr }}"
|
||||
|
||||
- name: Convert MAC address to Cisco format
|
||||
debug:
|
||||
msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.macaddr('cisco') }}"
|
||||
|
||||
# TASK [Check if given string is a MAC address] ***************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "msg": "1a:2b:3c:4d:5e:6f"
|
||||
# }
|
||||
#
|
||||
# TASK [Convert MAC address to Cisco format] ******************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "msg": "1a2b.3c4d.5e6f"
|
||||
# }
|
||||
|
||||
|
||||
|
||||
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">string</span>
|
||||
</div>
|
||||
</td>
|
||||
<td></td>
|
||||
<td>
|
||||
<div>mac/Hw address</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,140 @@
|
|||
.. _ansible.utils.slaac_filter:
|
||||
|
||||
|
||||
*******************
|
||||
ansible.utils.slaac
|
||||
*******************
|
||||
|
||||
**This filter returns the SLAAC address within a network for a given HW/MAC address.**
|
||||
|
||||
|
||||
Version added: 2.5.0
|
||||
|
||||
.. contents::
|
||||
:local:
|
||||
:depth: 1
|
||||
|
||||
|
||||
Synopsis
|
||||
--------
|
||||
- This filter returns the SLAAC address within a network for a given HW/MAC address.
|
||||
- The filter slaac() generates an IPv6 address for a given network and a MAC Address in Stateless Configuration.
|
||||
|
||||
|
||||
|
||||
|
||||
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>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
<div>nth host</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">string</span>
|
||||
/ <span style="color: red">required</span>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
<div>The network address or range to test against.</div>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<br/>
|
||||
|
||||
|
||||
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
#### examples
|
||||
- name: The filter slaac() generates an IPv6 address for a given network and a MAC Address in Stateless Configuration.
|
||||
debug:
|
||||
msg: "{{ 'fdcf:1894:23b5:d38c:0000:0000:0000:0000' | slaac('c2:31:b3:83:bf:2b') }}"
|
||||
|
||||
# TASK [The filter slaac() generates an IPv6 address for a given network and a MAC Address in Stateless Configuration.] ***
|
||||
# task path: /Users/amhatre/ansible-collections/playbooks/test_slaac.yaml:7
|
||||
# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
|
||||
# ok: [localhost] => {
|
||||
# "msg": "fdcf:1894:23b5:d38c:c031:b3ff:fe83:bf2b"
|
||||
# }
|
||||
|
||||
|
||||
|
||||
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">string</span>
|
||||
</div>
|
||||
</td>
|
||||
<td></td>
|
||||
<td>
|
||||
<div>Returns nth host from network</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,130 @@
|
|||
# -*- 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: hwaddr
|
||||
"""
|
||||
from __future__ import absolute_import, division, print_function
|
||||
from functools import partial
|
||||
from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddr_utils import (
|
||||
_need_netaddr,
|
||||
hwaddr,
|
||||
)
|
||||
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: hwaddr
|
||||
author: Ashwini Mhatre (@amhatre)
|
||||
version_added: "2.5.0"
|
||||
short_description: HWaddr / MAC address filters
|
||||
description:
|
||||
- This filter check if string is a HW/MAC address and filter it
|
||||
- You can use the hwaddr() filter to check if a given string is a MAC address or convert it between various
|
||||
formats.
|
||||
options:
|
||||
value:
|
||||
description: HW/MAC address.
|
||||
type: str
|
||||
required: True
|
||||
query:
|
||||
description: query string. Example. cisco,linux,unix etc
|
||||
type: str
|
||||
default: ""
|
||||
alias:
|
||||
description: alias
|
||||
type: str
|
||||
default: hwaddr
|
||||
notes:
|
||||
"""
|
||||
|
||||
EXAMPLES = r"""
|
||||
#### examples
|
||||
- name: Check if given string is a MAC address
|
||||
debug:
|
||||
msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.hwaddr }}"
|
||||
|
||||
- name: Convert HW address to Cisco format
|
||||
debug:
|
||||
msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.hwaddr('cisco') }}"
|
||||
|
||||
# TASK [Check if given string is a MAC address] ***************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "msg": "1a:2b:3c:4d:5e:6f"
|
||||
# }
|
||||
#
|
||||
# TASK [Convert HW address to Cisco format] ******************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "msg": "1a2b.3c4d.5e6f"
|
||||
# }
|
||||
|
||||
|
||||
"""
|
||||
|
||||
RETURN = """
|
||||
data:
|
||||
type: str
|
||||
description:
|
||||
- mac/Hw address
|
||||
|
||||
"""
|
||||
|
||||
|
||||
@pass_environment
|
||||
def _hwaddr(*args, **kwargs):
|
||||
"""This filter check if string is a HW/MAC address and filter it """
|
||||
keys = ["value", "query", "alias"]
|
||||
data = dict(zip(keys, args[1:]))
|
||||
data.update(kwargs)
|
||||
aav = AnsibleArgSpecValidator(
|
||||
data=data, schema=DOCUMENTATION, name="hwaddr"
|
||||
)
|
||||
valid, errors, updated_data = aav.validate()
|
||||
if not valid:
|
||||
raise AnsibleFilterError(errors)
|
||||
return hwaddr(**updated_data)
|
||||
|
||||
|
||||
class FilterModule(object):
|
||||
"""IP address and network manipulation filters
|
||||
"""
|
||||
|
||||
filter_map = {
|
||||
# IP addresses and networks
|
||||
"hwaddr": _hwaddr
|
||||
}
|
||||
|
||||
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
|
||||
)
|
|
@ -0,0 +1,131 @@
|
|||
# -*- 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: macaddr
|
||||
"""
|
||||
from __future__ import absolute_import, division, print_function
|
||||
from functools import partial
|
||||
from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddr_utils import (
|
||||
_need_netaddr,
|
||||
hwaddr,
|
||||
)
|
||||
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: macaddr
|
||||
author: Ashwini Mhatre (@amhatre)
|
||||
version_added: "2.5.0"
|
||||
short_description: macaddr / MAC address filters
|
||||
description:
|
||||
- This filter check if string is a MAC address and filter it
|
||||
- You can use the macaddr() filter to check if a given string is a MAC address or convert it between various
|
||||
formats.
|
||||
options:
|
||||
value:
|
||||
description: HW/MAC address.
|
||||
type: str
|
||||
required: True
|
||||
query:
|
||||
description: query string. Example. cisco,linux,unix etc
|
||||
type: str
|
||||
default: ""
|
||||
notes:
|
||||
"""
|
||||
|
||||
EXAMPLES = r"""
|
||||
#### examples
|
||||
- name: Check if given string is a MAC address
|
||||
debug:
|
||||
msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.macaddr }}"
|
||||
|
||||
- name: Convert MAC address to Cisco format
|
||||
debug:
|
||||
msg: "{{ '1a:2b:3c:4d:5e:6f' | ansible.utils.macaddr('cisco') }}"
|
||||
|
||||
# TASK [Check if given string is a MAC address] ***************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "msg": "1a:2b:3c:4d:5e:6f"
|
||||
# }
|
||||
#
|
||||
# TASK [Convert MAC address to Cisco format] ******************************************************************
|
||||
# ok: [localhost] => {
|
||||
# "msg": "1a2b.3c4d.5e6f"
|
||||
# }
|
||||
|
||||
|
||||
|
||||
"""
|
||||
|
||||
RETURN = """
|
||||
data:
|
||||
type: str
|
||||
description:
|
||||
- mac/Hw address
|
||||
|
||||
"""
|
||||
|
||||
|
||||
@pass_environment
|
||||
def _macaddr(*args, **kwargs):
|
||||
"""This filter check if string is a HW/MAC address and filter it """
|
||||
keys = ["value", "query"]
|
||||
data = dict(zip(keys, args[1:]))
|
||||
data.update(kwargs)
|
||||
aav = AnsibleArgSpecValidator(
|
||||
data=data, schema=DOCUMENTATION, name="macaddr"
|
||||
)
|
||||
valid, errors, updated_data = aav.validate()
|
||||
if not valid:
|
||||
raise AnsibleFilterError(errors)
|
||||
return macaddr(**updated_data)
|
||||
|
||||
|
||||
def macaddr(value, query=""):
|
||||
return hwaddr(value, query, alias="macaddr")
|
||||
|
||||
|
||||
class FilterModule(object):
|
||||
"""IP address and network manipulation filters
|
||||
"""
|
||||
|
||||
filter_map = {
|
||||
# IP addresses and networks
|
||||
"macaddr": _macaddr
|
||||
}
|
||||
|
||||
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
|
||||
)
|
|
@ -0,0 +1,147 @@
|
|||
# -*- 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: slaac
|
||||
"""
|
||||
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,
|
||||
hwaddr,
|
||||
)
|
||||
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: slaac
|
||||
author: Ashwini Mhatre (@amhatre)
|
||||
version_added: "2.5.0"
|
||||
short_description: This filter returns the SLAAC address within a network for a given HW/MAC address.
|
||||
description:
|
||||
- This filter returns the SLAAC address within a network for a given HW/MAC address.
|
||||
- The filter slaac() generates an IPv6 address for a given network and a MAC Address in Stateless Configuration.
|
||||
options:
|
||||
value:
|
||||
description: The network address or range to test against.
|
||||
type: str
|
||||
required: True
|
||||
query:
|
||||
description: nth host
|
||||
type: str
|
||||
notes:
|
||||
"""
|
||||
|
||||
EXAMPLES = r"""
|
||||
#### examples
|
||||
- name: The filter slaac() generates an IPv6 address for a given network and a MAC Address in Stateless Configuration.
|
||||
debug:
|
||||
msg: "{{ 'fdcf:1894:23b5:d38c:0000:0000:0000:0000' | slaac('c2:31:b3:83:bf:2b') }}"
|
||||
|
||||
# TASK [The filter slaac() generates an IPv6 address for a given network and a MAC Address in Stateless Configuration.] ***
|
||||
# task path: /Users/amhatre/ansible-collections/playbooks/test_slaac.yaml:7
|
||||
# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
|
||||
# ok: [localhost] => {
|
||||
# "msg": "fdcf:1894:23b5:d38c:c031:b3ff:fe83:bf2b"
|
||||
# }
|
||||
|
||||
|
||||
"""
|
||||
|
||||
RETURN = """
|
||||
data:
|
||||
type: str
|
||||
description:
|
||||
- Returns nth host from network
|
||||
|
||||
"""
|
||||
|
||||
|
||||
@pass_environment
|
||||
def _slaac(*args, **kwargs):
|
||||
"""This filter returns whether an address or a network passed as argument is in a network."""
|
||||
keys = ["value", "query"]
|
||||
data = dict(zip(keys, args[1:]))
|
||||
data.update(kwargs)
|
||||
aav = AnsibleArgSpecValidator(
|
||||
data=data, schema=DOCUMENTATION, name="slaac"
|
||||
)
|
||||
valid, errors, updated_data = aav.validate()
|
||||
if not valid:
|
||||
raise AnsibleFilterError(errors)
|
||||
return slaac(**updated_data)
|
||||
|
||||
|
||||
def slaac(value, query=""):
|
||||
""" Get the SLAAC address within given network """
|
||||
try:
|
||||
vtype = ipaddr(value, "type")
|
||||
if vtype == "address":
|
||||
v = ipaddr(value, "cidr")
|
||||
elif vtype == "network":
|
||||
v = ipaddr(value, "subnet")
|
||||
|
||||
if ipaddr(value, "version") != 6:
|
||||
return False
|
||||
|
||||
value = netaddr.IPNetwork(v)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
if not query:
|
||||
return False
|
||||
|
||||
try:
|
||||
mac = hwaddr(query, alias="slaac")
|
||||
|
||||
eui = netaddr.EUI(mac)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
return str(eui.ipv6(value.network))
|
||||
|
||||
|
||||
class FilterModule(object):
|
||||
"""IP address and network manipulation filters
|
||||
"""
|
||||
|
||||
filter_map = {
|
||||
# IP addresses and networks
|
||||
"slaac": _slaac
|
||||
}
|
||||
|
||||
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
|
||||
)
|
|
@ -637,3 +637,90 @@ def _range_checker(ip_check, first, last):
|
|||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
# ---- HWaddr query helpers ----
|
||||
def _bare_query(v):
|
||||
v.dialect = netaddr.mac_bare
|
||||
return str(v)
|
||||
|
||||
|
||||
def _bool_hwaddr_query(v):
|
||||
if v:
|
||||
return True
|
||||
|
||||
|
||||
def _int_hwaddr_query(v):
|
||||
return int(v)
|
||||
|
||||
|
||||
def _cisco_query(v):
|
||||
v.dialect = netaddr.mac_cisco
|
||||
return str(v)
|
||||
|
||||
|
||||
def _empty_hwaddr_query(v, value):
|
||||
if v:
|
||||
return value
|
||||
|
||||
|
||||
def _linux_query(v):
|
||||
v.dialect = mac_linux
|
||||
return str(v)
|
||||
|
||||
|
||||
def _postgresql_query(v):
|
||||
v.dialect = netaddr.mac_pgsql
|
||||
import epdb
|
||||
|
||||
epdb.serve()
|
||||
return str(v)
|
||||
|
||||
|
||||
def _unix_query(v):
|
||||
v.dialect = netaddr.mac_unix
|
||||
return str(v)
|
||||
|
||||
|
||||
def _win_query(v):
|
||||
v.dialect = netaddr.mac_eui48
|
||||
return str(v)
|
||||
|
||||
|
||||
# ---- HWaddr / MAC address filters ----
|
||||
def hwaddr(value, query="", alias="hwaddr"):
|
||||
""" Check if string is a HW/MAC address and filter it """
|
||||
|
||||
query_func_extra_args = {"": ("value",)}
|
||||
|
||||
query_func_map = {
|
||||
"": _empty_hwaddr_query,
|
||||
"bare": _bare_query,
|
||||
"bool": _bool_hwaddr_query,
|
||||
"int": _int_hwaddr_query,
|
||||
"cisco": _cisco_query,
|
||||
"eui48": _win_query,
|
||||
"linux": _linux_query,
|
||||
"pgsql": _postgresql_query,
|
||||
"postgresql": _postgresql_query,
|
||||
"psql": _postgresql_query,
|
||||
"unix": _unix_query,
|
||||
"win": _win_query,
|
||||
}
|
||||
|
||||
try:
|
||||
v = netaddr.EUI(value)
|
||||
except Exception:
|
||||
v = None
|
||||
if query and query != "bool":
|
||||
raise AnsibleFilterError(
|
||||
alias + ": not a hardware address: %s" % value
|
||||
)
|
||||
|
||||
extras = []
|
||||
for arg in query_func_extra_args.get(query, tuple()):
|
||||
extras.append(locals()[arg])
|
||||
try:
|
||||
return query_func_map[query](v, *extras)
|
||||
except KeyError:
|
||||
raise AnsibleFilterError(alias + ": unknown filter type: %s" % query)
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
---
|
||||
- name: hwaddr filter test1
|
||||
ansible.builtin.set_fact:
|
||||
result1: "{{ '1a:2b:3c:4d:5e:6f'|ansible.utils.hwaddr }}"
|
||||
|
||||
- name: Assert result for hwaddr.
|
||||
assert:
|
||||
that: "{{ result1 == '1a:2b:3c:4d:5e:6f' }}"
|
||||
|
||||
|
||||
- name: hwaddr filter test2
|
||||
ansible.builtin.set_fact:
|
||||
result1: "{{ '1a:2b:3c:4d:5e:6f'|ansible.utils.hwaddr('cisco') }}"
|
||||
|
||||
- name: Assert result for hwaddr.
|
||||
assert:
|
||||
that: "{{ result1 == '1a2b.3c4d.5e6f' }}"
|
|
@ -0,0 +1,17 @@
|
|||
---
|
||||
- name: macaddr filter test1
|
||||
ansible.builtin.set_fact:
|
||||
result1: "{{ '1a:2b:3c:4d:5e:6f'|ansible.utils.macaddr }}"
|
||||
|
||||
- name: Assert result for macaddr.
|
||||
assert:
|
||||
that: "{{ result1 == '1a:2b:3c:4d:5e:6f' }}"
|
||||
|
||||
|
||||
- name: macaddr filter test2
|
||||
ansible.builtin.set_fact:
|
||||
result1: "{{ '1a:2b:3c:4d:5e:6f'|ansible.utils.macaddr('cisco') }}"
|
||||
|
||||
- name: Assert result for macaddr.
|
||||
assert:
|
||||
that: "{{ result1 == '1a2b.3c4d.5e6f' }}"
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
- name: slaac filter test1
|
||||
ansible.builtin.set_fact:
|
||||
result1: "{{ 'fdcf:1894:23b5:d38c:0000:0000:0000:0000'|ansible.utils.slaac('c2:31:b3:83:bf:2b') }}"
|
||||
|
||||
- name: Assert result for slaac.
|
||||
assert:
|
||||
that: "{{ result1 == 'fdcf:1894:23b5:d38c:c031:b3ff:fe83:bf2b' }}"
|
|
@ -0,0 +1,32 @@
|
|||
# -*- 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 hwaddr filter plugin
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import unittest
|
||||
from ansible_collections.ansible.utils.plugins.filter.hwaddr import _hwaddr
|
||||
|
||||
|
||||
class Test_hwaddr(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_hwaddr_filter_1(self):
|
||||
"""hwaddr filter"""
|
||||
args = ["", "1a:2b:3c:4d:5e:6f"]
|
||||
result = _hwaddr(*args)
|
||||
self.assertEqual(result, "1a:2b:3c:4d:5e:6f")
|
||||
|
||||
def test_hwaddr_filter_2(self):
|
||||
"""hwaddr filter"""
|
||||
args = ["", "1a:2b:3c:4d:5e:6f", "cisco"]
|
||||
result = _hwaddr(*args)
|
||||
self.assertEqual(result, "1a2b.3c4d.5e6f")
|
|
@ -0,0 +1,32 @@
|
|||
# -*- 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 macaddr filter plugin
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import unittest
|
||||
from ansible_collections.ansible.utils.plugins.filter.macaddr import _macaddr
|
||||
|
||||
|
||||
class Test_macaddr(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_macaddr_filter_1(self):
|
||||
"""macaddr filter"""
|
||||
args = ["", "1a:2b:3c:4d:5e:6f"]
|
||||
result = _macaddr(*args)
|
||||
self.assertEqual(result, "1a:2b:3c:4d:5e:6f")
|
||||
|
||||
def test_macaddr_filter_2(self):
|
||||
"""macaddr filter"""
|
||||
args = ["", "1a:2b:3c:4d:5e:6f", "cisco"]
|
||||
result = _macaddr(*args)
|
||||
self.assertEqual(result, "1a2b.3c4d.5e6f")
|
|
@ -0,0 +1,30 @@
|
|||
# -*- 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 slaac filter plugin
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import unittest
|
||||
from ansible_collections.ansible.utils.plugins.filter.slaac import _slaac
|
||||
|
||||
|
||||
class Test_slaac(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_slaac_filter_1(self):
|
||||
"""slaac filter"""
|
||||
args = [
|
||||
"",
|
||||
"fdcf:1894:23b5:d38c:0000:0000:0000:0000",
|
||||
"c2:31:b3:83:bf:2b",
|
||||
]
|
||||
result = _slaac(*args)
|
||||
self.assertEqual(result, "fdcf:1894:23b5:d38c:c031:b3ff:fe83:bf2b")
|
Loading…
Reference in New Issue