mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
refactor eos_eapi module (#20740)
* eos_eapi module now requires network_cli plugin * adds unit test cases for eos_eapi module
This commit is contained in:
parent
cd14d9dcfc
commit
57660abf33
6 changed files with 437 additions and 158 deletions
|
@ -16,10 +16,11 @@
|
|||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
|
||||
ANSIBLE_METADATA = {'status': ['preview'],
|
||||
'supported_by': 'core',
|
||||
'version': '1.0'}
|
||||
ANSIBLE_METADATA = {
|
||||
'status': ['preview'],
|
||||
'supported_by': 'core',
|
||||
'version': '1.0'
|
||||
}
|
||||
|
||||
DOCUMENTATION = """
|
||||
---
|
||||
|
@ -37,7 +38,6 @@ description:
|
|||
Unix socket server. Use the options listed below to override the
|
||||
default configuration.
|
||||
- Requires EOS v4.12 or greater.
|
||||
extends_documentation_fragment: eos
|
||||
options:
|
||||
http:
|
||||
description:
|
||||
|
@ -141,18 +141,9 @@ options:
|
|||
"""
|
||||
|
||||
EXAMPLES = """
|
||||
# Note: examples below use the following provider dict to handle
|
||||
# transport and authentication to the node.
|
||||
vars:
|
||||
cli:
|
||||
host: "{{ inventory_hostname }}"
|
||||
username: admin
|
||||
password: admin
|
||||
|
||||
- name: Enable eAPI access with default configuration
|
||||
eos_eapi:
|
||||
state: started
|
||||
provider: "{{ cli }}"
|
||||
|
||||
- name: Enable eAPI with no HTTP, HTTPS at port 9443, local HTTP at port 80, and socket enabled
|
||||
eos_eapi:
|
||||
|
@ -162,153 +153,161 @@ vars:
|
|||
local_http: yes
|
||||
local_http_port: 80
|
||||
socket: yes
|
||||
provider: "{{ cli }}"
|
||||
|
||||
- name: Shutdown eAPI access
|
||||
eos_eapi:
|
||||
state: stopped
|
||||
provider: "{{ cli }}"
|
||||
"""
|
||||
|
||||
RETURN = """
|
||||
updates:
|
||||
description:
|
||||
- Set of commands to be executed on remote device
|
||||
commands:
|
||||
description: The list of configuration mode commands to send to the device
|
||||
returned: always
|
||||
type: list
|
||||
sample: ['management api http-commands', 'shutdown']
|
||||
sample:
|
||||
- management api http-commands
|
||||
- protocol http port 81
|
||||
- no protocol https
|
||||
urls:
|
||||
description: Hash of URL endpoints eAPI is listening on per interface
|
||||
returned: when eAPI is started
|
||||
type: dict
|
||||
sample: {'Management1': ['http://172.26.10.1:80']}
|
||||
session_name:
|
||||
description: The EOS config session name used to load the configuration
|
||||
returned: when changed is True
|
||||
type: str
|
||||
sample: ansible_1479315771
|
||||
start:
|
||||
description: The time the job started
|
||||
returned: always
|
||||
type: str
|
||||
sample: "2016-11-16 10:38:15.126146"
|
||||
end:
|
||||
description: The time the job ended
|
||||
returned: always
|
||||
type: str
|
||||
sample: "2016-11-16 10:38:25.595612"
|
||||
delta:
|
||||
description: The time elapsed to perform all operations
|
||||
returned: always
|
||||
type: str
|
||||
sample: "0:00:10.469466"
|
||||
"""
|
||||
import re
|
||||
import time
|
||||
|
||||
import ansible.module_utils.eos
|
||||
from ansible.module_utils.local import LocalAnsibleModule
|
||||
from ansible.module_utils.eos import run_commands, load_config
|
||||
from ansible.module_utils.six import iteritems
|
||||
|
||||
from ansible.module_utils.basic import get_exception
|
||||
from ansible.module_utils.network import NetworkModule, NetworkError
|
||||
from ansible.module_utils.netcfg import NetworkConfig, dumps
|
||||
|
||||
PRIVATE_KEYS_RE = re.compile('__.+__')
|
||||
|
||||
|
||||
def invoke(name, *args, **kwargs):
|
||||
func = globals().get(name)
|
||||
if func:
|
||||
return func(*args, **kwargs)
|
||||
|
||||
def get_instance(module):
|
||||
try:
|
||||
resp = module.cli('show management api http-commands', 'json')
|
||||
return dict(
|
||||
http=resp[0]['httpServer']['configured'],
|
||||
http_port=resp[0]['httpServer']['port'],
|
||||
https=resp[0]['httpsServer']['configured'],
|
||||
https_port=resp[0]['httpsServer']['port'],
|
||||
local_http=resp[0]['localHttpServer']['configured'],
|
||||
local_http_port=resp[0]['localHttpServer']['port'],
|
||||
socket=resp[0]['unixSocketServer']['configured'],
|
||||
vrf=resp[0]['vrf']
|
||||
)
|
||||
except NetworkError:
|
||||
exc = get_exception()
|
||||
module.fail_json(msg=str(exc), **exc.kwargs)
|
||||
|
||||
def started(module, instance, commands):
|
||||
commands.append('no shutdown')
|
||||
setters = set()
|
||||
for key, value in module.argument_spec.items():
|
||||
if module.params[key] is not None:
|
||||
setter = value.get('setter') or 'set_%s' % key
|
||||
if setter not in setters:
|
||||
setters.add(setter)
|
||||
invoke(setter, module, instance, commands)
|
||||
|
||||
def stopped(module, instance, commands):
|
||||
commands.append('shutdown')
|
||||
|
||||
def set_protocol_http(module, instance, commands):
|
||||
port = module.params['http_port']
|
||||
if not 1 <= port <= 65535:
|
||||
def validate_http_port(value, module):
|
||||
if not 1 <= value <= 65535:
|
||||
module.fail_json(msg='http_port must be between 1 and 65535')
|
||||
elif any((module.params['http'], instance['http'])):
|
||||
commands.append('protocol http port %s' % port)
|
||||
elif module.params['http'] is False:
|
||||
commands.append('no protocol http')
|
||||
|
||||
def set_protocol_https(module, instance, commands):
|
||||
port = module.params['https_port']
|
||||
if not 1 <= port <= 65535:
|
||||
module.fail_json(msg='https_port must be between 1 and 65535')
|
||||
elif any((module.params['https'], instance['https'])):
|
||||
commands.append('protocol https port %s' % port)
|
||||
elif module.params['https'] is False:
|
||||
commands.append('no protocol https')
|
||||
def validate_https_port(value, module):
|
||||
if not 1 <= value <= 65535:
|
||||
module.fail_json(msg='http_port must be between 1 and 65535')
|
||||
|
||||
def set_local_http(module, instance, commands):
|
||||
port = module.params['local_http_port']
|
||||
if not 1 <= port <= 65535:
|
||||
module.fail_json(msg='local_http_port must be between 1 and 65535')
|
||||
elif any((module.params['local_http'], instance['local_http'])):
|
||||
commands.append('protocol http localhost port %s' % port)
|
||||
elif module.params['local_http'] is False:
|
||||
commands.append('no protocol http localhost port 8080')
|
||||
def validate_local_http_port(value, module):
|
||||
if not 1 <= value <= 65535:
|
||||
module.fail_json(msg='http_port must be between 1 and 65535')
|
||||
|
||||
def set_socket(module, instance, commands):
|
||||
if any((module.params['socket'], instance['socket'])):
|
||||
commands.append('protocol unix-socket')
|
||||
elif module.params['socket'] is False:
|
||||
commands.append('no protocol unix-socket')
|
||||
def validate_vrf(value, module):
|
||||
rc, out, err = run_commands(module, ['show vrf'])
|
||||
configured_vrfs = re.findall('^\s+(\w+)(?=\s)', out[0],re.M)
|
||||
configured_vrfs.append('default')
|
||||
if value not in configured_vrfs:
|
||||
module.fail_json(msg='vrf `%s` is not configured on the system' % value)
|
||||
|
||||
def set_vrf(module, instance, commands):
|
||||
vrf = module.params['vrf']
|
||||
if vrf != 'default':
|
||||
resp = module.cli(['show vrf'])
|
||||
if vrf not in resp[0]:
|
||||
module.fail_json(msg="vrf '%s' is not configured" % vrf)
|
||||
commands.append('vrf %s' % vrf)
|
||||
def map_obj_to_commands(updates, module):
|
||||
commands = list()
|
||||
want, have = updates
|
||||
|
||||
def get_config(module):
|
||||
contents = module.params['config']
|
||||
if not contents:
|
||||
cmd = 'show running-config all section management api http-commands'
|
||||
contents = module.cli([cmd])
|
||||
config = NetworkConfig(indent=3, contents=contents[0])
|
||||
return config
|
||||
needs_update = lambda x: want.get(x) is not None and (want.get(x) != have.get(x))
|
||||
|
||||
def load_config(module, instance, commands, result):
|
||||
commit = not module.check_mode
|
||||
diff = module.config.load_config(commands, commit=commit)
|
||||
if diff:
|
||||
result['diff'] = dict(prepared=diff)
|
||||
result['changed'] = True
|
||||
def add(cmd):
|
||||
if 'management api http-commands' not in commands:
|
||||
commands.insert(0, 'management api http-commands')
|
||||
commands.append(cmd)
|
||||
|
||||
def load(module, instance, commands, result):
|
||||
candidate = NetworkConfig(indent=3)
|
||||
candidate.add(commands, parents=['management api http-commands'])
|
||||
if any((needs_update('http'), needs_update('http_port'))):
|
||||
if want['http'] is False:
|
||||
add('no protocol http')
|
||||
else:
|
||||
port = want['http_port'] or 80
|
||||
add('protocol http port %s' % port)
|
||||
|
||||
config = get_config(module)
|
||||
configobjs = candidate.difference(config)
|
||||
if any((needs_update('https'), needs_update('https_port'))):
|
||||
if want['https'] is False:
|
||||
add('no protocol https')
|
||||
else:
|
||||
port = want['https_port'] or 443
|
||||
add('protocol https port %s' % port)
|
||||
|
||||
if configobjs:
|
||||
commands = dumps(configobjs, 'commands').split('\n')
|
||||
result['updates'] = commands
|
||||
load_config(module, instance, commands, result)
|
||||
if any((needs_update('local_http'), needs_update('local_http_port'))):
|
||||
if want['local_http'] is False:
|
||||
add('no protocol http localhost')
|
||||
else:
|
||||
port = want['local_http_port'] or 8080
|
||||
add('protocol http localhost port %s' % port)
|
||||
|
||||
def clean_result(result):
|
||||
# strip out any keys that have two leading and two trailing
|
||||
# underscore characters
|
||||
for key in result.keys():
|
||||
if PRIVATE_KEYS_RE.match(key):
|
||||
del result[key]
|
||||
if needs_update('vrf'):
|
||||
add('vrf %s' % want['vrf'])
|
||||
|
||||
if needs_update('state'):
|
||||
if want['state'] == 'stopped':
|
||||
add('shutdown')
|
||||
elif want['state'] == 'started':
|
||||
add('no shutdown')
|
||||
|
||||
return commands
|
||||
|
||||
def parse_state(data):
|
||||
if data[0]['enabled']:
|
||||
return 'started'
|
||||
else:
|
||||
return 'stopped'
|
||||
|
||||
|
||||
def map_config_to_obj(module):
|
||||
rc, out, err = run_commands(module, ['show management api http-commands | json'])
|
||||
return {
|
||||
'http': out[0]['httpServer']['configured'],
|
||||
'http_port': out[0]['httpServer']['port'],
|
||||
'https': out[0]['httpsServer']['configured'],
|
||||
'https_port': out[0]['httpsServer']['port'],
|
||||
'local_http': out[0]['localHttpServer']['configured'],
|
||||
'local_http_port': out[0]['localHttpServer']['port'],
|
||||
'socket': out[0]['unixSocketServer']['configured'],
|
||||
'vrf': out[0]['vrf'],
|
||||
'state': parse_state(out)
|
||||
}
|
||||
|
||||
def map_params_to_obj(module):
|
||||
obj = {
|
||||
'http': module.params['http'],
|
||||
'http_port': module.params['http_port'],
|
||||
'https': module.params['https'],
|
||||
'https_port': module.params['https_port'],
|
||||
'local_http': module.params['local_http'],
|
||||
'local_http_port': module.params['local_http_port'],
|
||||
'socket': module.params['socket'],
|
||||
'vrf': module.params['vrf'],
|
||||
'state': module.params['state']
|
||||
}
|
||||
|
||||
for key, value in iteritems(obj):
|
||||
if value:
|
||||
validator = globals().get('validate_%s' % key)
|
||||
if validator:
|
||||
validator(value, module)
|
||||
|
||||
return obj
|
||||
|
||||
def collect_facts(module, result):
|
||||
resp = module.cli(['show management api http-commands'], output='json')
|
||||
rc, out, err = run_commands(module, ['show management api http-commands | json'])
|
||||
facts = dict(eos_eapi_urls=dict())
|
||||
for each in resp[0]['urls']:
|
||||
for each in out[0]['urls']:
|
||||
intf, url = each.split(' : ')
|
||||
key = str(intf).strip()
|
||||
if key not in facts['eos_eapi_urls']:
|
||||
|
@ -316,54 +315,46 @@ def collect_facts(module, result):
|
|||
facts['eos_eapi_urls'][key].append(str(url).strip())
|
||||
result['ansible_facts'] = facts
|
||||
|
||||
|
||||
def main():
|
||||
""" main entry point for module execution
|
||||
"""
|
||||
|
||||
argument_spec = dict(
|
||||
http=dict(aliases=['enable_http'], default=False, type='bool', setter='set_protocol_http'),
|
||||
http_port=dict(default=80, type='int', setter='set_protocol_http'),
|
||||
http=dict(aliases=['enable_http'], type='bool'),
|
||||
http_port=dict(type='int'),
|
||||
|
||||
https=dict(aliases=['enable_https'], default=True, type='bool', setter='set_protocol_https'),
|
||||
https_port=dict(default=443, type='int', setter='set_protocol_https'),
|
||||
https=dict(aliases=['enable_https'], type='bool'),
|
||||
https_port=dict(type='int'),
|
||||
|
||||
local_http=dict(aliases=['enable_local_http'], default=False, type='bool', setter='set_local_http'),
|
||||
local_http_port=dict(default=8080, type='int', setter='set_local_http'),
|
||||
local_http=dict(aliases=['enable_local_http'], type='bool'),
|
||||
local_http_port=dict(type='int'),
|
||||
|
||||
socket=dict(aliases=['enable_socket'], default=False, type='bool'),
|
||||
socket=dict(aliases=['enable_socket'], type='bool'),
|
||||
|
||||
vrf=dict(default='default'),
|
||||
|
||||
config=dict(),
|
||||
|
||||
# Only allow use of transport cli when configuring eAPI
|
||||
transport=dict(default='cli', choices=['cli']),
|
||||
|
||||
state=dict(default='started', choices=['stopped', 'started']),
|
||||
)
|
||||
|
||||
module = NetworkModule(argument_spec=argument_spec,
|
||||
connect_on_load=False,
|
||||
supports_check_mode=True)
|
||||
module = LocalAnsibleModule(argument_spec=argument_spec,
|
||||
supports_check_mode=True)
|
||||
|
||||
state = module.params['state']
|
||||
result = {'changed': False}
|
||||
|
||||
result = dict(changed=False)
|
||||
want = map_params_to_obj(module)
|
||||
have = map_config_to_obj(module)
|
||||
|
||||
commands = list()
|
||||
instance = get_instance(module)
|
||||
commands = map_obj_to_commands((want, have), module)
|
||||
result['commands'] = commands
|
||||
|
||||
invoke(state, module, instance, commands)
|
||||
|
||||
try:
|
||||
load(module, instance, commands, result)
|
||||
except NetworkError:
|
||||
exc = get_exception()
|
||||
module.fail_json(msg=str(exc), **exc.kwargs)
|
||||
if commands:
|
||||
commit = not module.check_mode
|
||||
response = load_config(module, commands, commit=commit)
|
||||
if response.get('diff') and module._diff:
|
||||
result['diff'] = {'prepared': response.get('diff')}
|
||||
result['session_name'] = response.get('session')
|
||||
result['changed'] = True
|
||||
|
||||
collect_facts(module, result)
|
||||
clean_result(result)
|
||||
|
||||
module.exit_json(**result)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue