2016-09-02 21:42:31 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
|
|
|
'status': ['preview'],
|
2017-08-16 04:10:36 +00:00
|
|
|
'supported_by': 'network'}
|
2016-12-06 10:35:05 +00:00
|
|
|
|
2016-09-02 21:42:31 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: nxos_overlay_global
|
2017-03-06 13:53:33 +00:00
|
|
|
extends_documentation_fragment: nxos
|
2016-09-02 21:42:31 +00:00
|
|
|
version_added: "2.2"
|
2016-09-02 21:47:19 +00:00
|
|
|
short_description: Configures anycast gateway MAC of the switch.
|
2016-09-02 21:42:31 +00:00
|
|
|
description:
|
2017-07-10 13:23:52 +00:00
|
|
|
- Configures anycast gateway MAC of the switch.
|
2016-09-02 21:42:31 +00:00
|
|
|
author: Gabriele Gerbino (@GGabriele)
|
|
|
|
notes:
|
2017-08-24 12:27:39 +00:00
|
|
|
- Tested against NXOSv 7.3.(0)D1(1) on VIRL
|
2017-07-10 13:23:52 +00:00
|
|
|
- Default restores params default value
|
|
|
|
- Supported MAC address format are "E.E.E", "EE-EE-EE-EE-EE-EE",
|
|
|
|
"EE:EE:EE:EE:EE:EE" and "EEEE.EEEE.EEEE"
|
2016-09-02 21:42:31 +00:00
|
|
|
options:
|
2017-07-10 13:23:52 +00:00
|
|
|
anycast_gateway_mac:
|
|
|
|
description:
|
|
|
|
- Anycast gateway mac of the switch.
|
|
|
|
required: true
|
2016-09-02 21:42:31 +00:00
|
|
|
'''
|
2016-12-08 02:33:38 +00:00
|
|
|
|
2016-09-02 21:42:31 +00:00
|
|
|
EXAMPLES = '''
|
|
|
|
- nxos_overlay_global:
|
2016-09-20 02:22:12 +00:00
|
|
|
anycast_gateway_mac: "b.b.b"
|
2016-09-02 21:42:31 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
2017-07-10 13:23:52 +00:00
|
|
|
commands:
|
2016-09-02 21:42:31 +00:00
|
|
|
description: commands sent to the device
|
|
|
|
returned: always
|
|
|
|
type: list
|
2017-07-10 13:23:52 +00:00
|
|
|
sample: ["fabric forwarding anycast-gateway-mac 000B.000B.000B"]
|
2016-09-02 21:42:31 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
import re
|
2017-12-03 16:12:30 +00:00
|
|
|
from ansible.module_utils.network.nxos.nxos import get_config, load_config
|
|
|
|
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_args
|
2017-02-15 16:43:09 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2017-12-03 16:12:30 +00:00
|
|
|
from ansible.module_utils.network.common.config import CustomNetworkConfig
|
2016-09-02 21:42:31 +00:00
|
|
|
|
|
|
|
PARAM_TO_COMMAND_KEYMAP = {
|
|
|
|
'anycast_gateway_mac': 'fabric forwarding anycast-gateway-mac',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_existing(module, args):
|
|
|
|
existing = {}
|
2016-12-08 02:33:38 +00:00
|
|
|
config = str(get_config(module))
|
2016-09-02 21:42:31 +00:00
|
|
|
|
|
|
|
for arg in args:
|
2017-07-10 13:23:52 +00:00
|
|
|
command = PARAM_TO_COMMAND_KEYMAP[arg]
|
2017-08-14 10:39:53 +00:00
|
|
|
has_command = re.findall(r'(?:{0}\s)(?P<value>.*)$'.format(command), config, re.M)
|
2017-07-10 13:23:52 +00:00
|
|
|
value = ''
|
|
|
|
if has_command:
|
2017-08-14 10:39:53 +00:00
|
|
|
value = has_command[0]
|
2017-07-10 13:23:52 +00:00
|
|
|
existing[arg] = value
|
2017-08-14 10:39:53 +00:00
|
|
|
|
2016-09-02 21:42:31 +00:00
|
|
|
return existing
|
|
|
|
|
|
|
|
|
|
|
|
def apply_key_map(key_map, table):
|
|
|
|
new_dict = {}
|
|
|
|
for key, value in table.items():
|
|
|
|
new_key = key_map.get(key)
|
2017-07-10 13:23:52 +00:00
|
|
|
if value:
|
|
|
|
new_dict[new_key] = value
|
2016-09-02 21:42:31 +00:00
|
|
|
return new_dict
|
|
|
|
|
|
|
|
|
|
|
|
def get_commands(module, existing, proposed, candidate):
|
|
|
|
commands = list()
|
|
|
|
proposed_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, proposed)
|
|
|
|
existing_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, existing)
|
|
|
|
|
2017-09-13 12:03:06 +00:00
|
|
|
for key, proposed in proposed_commands.items():
|
|
|
|
existing_value = existing_commands.get(key)
|
|
|
|
if proposed == 'default' and existing_value:
|
|
|
|
commands.append('no {0} {1}'.format(key, existing_value))
|
|
|
|
elif 'anycast-gateway-mac' in key and proposed != 'default':
|
|
|
|
proposed = normalize_mac(proposed, module)
|
|
|
|
existing_value = normalize_mac(existing_value, module)
|
|
|
|
if proposed != existing_value:
|
|
|
|
command = '{0} {1}'.format(key, proposed)
|
|
|
|
commands.append(command)
|
2016-09-02 21:42:31 +00:00
|
|
|
if commands:
|
|
|
|
candidate.add(commands, parents=[])
|
|
|
|
|
|
|
|
|
|
|
|
def normalize_mac(proposed_mac, module):
|
2017-09-13 12:03:06 +00:00
|
|
|
if proposed_mac is None:
|
|
|
|
return ''
|
2016-09-02 21:42:31 +00:00
|
|
|
try:
|
|
|
|
if '-' in proposed_mac:
|
|
|
|
splitted_mac = proposed_mac.split('-')
|
|
|
|
if len(splitted_mac) != 6:
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
for octect in splitted_mac:
|
|
|
|
if len(octect) != 2:
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
elif '.' in proposed_mac:
|
|
|
|
splitted_mac = []
|
|
|
|
splitted_dot_mac = proposed_mac.split('.')
|
|
|
|
if len(splitted_dot_mac) != 3:
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
for octect in splitted_dot_mac:
|
|
|
|
if len(octect) > 4:
|
|
|
|
raise ValueError
|
|
|
|
else:
|
|
|
|
octect_len = len(octect)
|
|
|
|
padding = 4 - octect_len
|
2017-12-07 16:27:06 +00:00
|
|
|
splitted_mac.append(octect.zfill(padding + 1))
|
2016-09-02 21:42:31 +00:00
|
|
|
|
|
|
|
elif ':' in proposed_mac:
|
|
|
|
splitted_mac = proposed_mac.split(':')
|
|
|
|
if len(splitted_mac) != 6:
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
for octect in splitted_mac:
|
|
|
|
if len(octect) != 2:
|
|
|
|
raise ValueError
|
|
|
|
else:
|
|
|
|
raise ValueError
|
|
|
|
except ValueError:
|
2017-07-10 13:23:52 +00:00
|
|
|
module.fail_json(msg='Invalid MAC address format', proposed_mac=proposed_mac)
|
2016-09-02 21:42:31 +00:00
|
|
|
|
|
|
|
joined_mac = ''.join(splitted_mac)
|
2017-12-07 16:27:06 +00:00
|
|
|
mac = [joined_mac[i:i + 4] for i in range(0, len(joined_mac), 4)]
|
2016-09-02 21:42:31 +00:00
|
|
|
return '.'.join(mac).upper()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = dict(
|
2017-01-29 07:28:53 +00:00
|
|
|
anycast_gateway_mac=dict(required=True, type='str'),
|
2016-09-02 21:42:31 +00:00
|
|
|
)
|
2017-02-15 16:43:09 +00:00
|
|
|
|
|
|
|
argument_spec.update(nxos_argument_spec)
|
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec,
|
|
|
|
supports_check_mode=True)
|
|
|
|
|
|
|
|
warnings = list()
|
|
|
|
check_args(module, warnings)
|
2017-07-10 13:23:52 +00:00
|
|
|
result = {'changed': False, 'commands': [], 'warnings': warnings}
|
2016-09-02 21:42:31 +00:00
|
|
|
|
2017-07-10 13:23:52 +00:00
|
|
|
args = PARAM_TO_COMMAND_KEYMAP.keys()
|
2016-09-02 21:42:31 +00:00
|
|
|
|
2017-07-10 13:23:52 +00:00
|
|
|
existing = get_existing(module, args)
|
2016-12-12 23:16:23 +00:00
|
|
|
proposed = dict((k, v) for k, v in module.params.items()
|
2017-12-07 16:27:06 +00:00
|
|
|
if v is not None and k in args)
|
2016-09-02 21:42:31 +00:00
|
|
|
|
|
|
|
candidate = CustomNetworkConfig(indent=3)
|
2017-07-10 13:23:52 +00:00
|
|
|
get_commands(module, existing, proposed, candidate)
|
2016-09-02 21:42:31 +00:00
|
|
|
|
2017-07-10 13:23:52 +00:00
|
|
|
if candidate:
|
|
|
|
candidate = candidate.items_text()
|
|
|
|
result['commands'] = candidate
|
2016-09-02 21:42:31 +00:00
|
|
|
|
2017-07-10 13:23:52 +00:00
|
|
|
if not module.check_mode:
|
|
|
|
load_config(module, candidate)
|
|
|
|
result['changed'] = True
|
2017-02-15 16:43:09 +00:00
|
|
|
|
2016-09-02 21:42:31 +00:00
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|