mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-10 11:11:29 -07:00
refactors eos_config module to use network_cli (#20741)
* update eos_config to use eapi exclusively and remove cli transport * add unit test cases for eos_config * updates action plugin to handle both eapi and network_cli connections
This commit is contained in:
parent
a60c051952
commit
6c89c587cc
6 changed files with 352 additions and 69 deletions
|
@ -16,9 +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 = """
|
||||
---
|
||||
|
@ -32,7 +34,7 @@ description:
|
|||
an implementation for working with eos configuration sections in
|
||||
a deterministic way. This module works with either CLI or eAPI
|
||||
transports.
|
||||
extends_documentation_fragment: eos
|
||||
extends_documentation_fragment: eapi
|
||||
options:
|
||||
lines:
|
||||
description:
|
||||
|
@ -161,18 +163,8 @@ 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
|
||||
transport: cli
|
||||
|
||||
- eos_config:
|
||||
lines: hostname {{ inventory_hostname }}
|
||||
provider: "{{ cli }}"
|
||||
|
||||
- eos_config:
|
||||
lines:
|
||||
|
@ -184,7 +176,6 @@ vars:
|
|||
parents: ip access-list test
|
||||
before: no ip access-list test
|
||||
match: exact
|
||||
provider: "{{ cli }}"
|
||||
|
||||
- eos_config:
|
||||
lines:
|
||||
|
@ -195,39 +186,74 @@ vars:
|
|||
parents: ip access-list test
|
||||
before: no ip access-list test
|
||||
replace: block
|
||||
provider: "{{ cli }}"
|
||||
|
||||
- name: load configuration from file
|
||||
eos_config:
|
||||
src: eos.cfg
|
||||
provider: "{{ cli }}"
|
||||
"""
|
||||
|
||||
RETURN = """
|
||||
updates:
|
||||
commands:
|
||||
description: The set of commands that will be pushed to the remote device
|
||||
returned: Only when lines is specified.
|
||||
type: list
|
||||
sample: ['...', '...']
|
||||
sample: ['hostname switch01', 'interface Ethernet1', 'no shutdown']
|
||||
backup_path:
|
||||
description: The full path to the backup file
|
||||
returned: when backup is yes
|
||||
type: path
|
||||
sample: /playbooks/ansible/backup/eos_config.2016-07-16@22:28:34
|
||||
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 time
|
||||
from functools import partial
|
||||
|
||||
from ansible.module_utils import eos
|
||||
from ansible.module_utils import eapi
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.local import LocalAnsibleModule
|
||||
from ansible.module_utils.netcfg import NetworkConfig, dumps
|
||||
from ansible.module_utils.eos import NetworkModule, NetworkError
|
||||
from ansible.module_utils.basic import get_exception
|
||||
|
||||
SHARED_LIB = 'eos'
|
||||
|
||||
def get_ansible_module():
|
||||
if SHARED_LIB == 'eos':
|
||||
return LocalAnsibleModule
|
||||
return AnsibleModule
|
||||
|
||||
def invoke(name, *args, **kwargs):
|
||||
obj = globals().get(SHARED_LIB)
|
||||
func = getattr(obj, name)
|
||||
return func(*args, **kwargs)
|
||||
|
||||
run_commands = partial(invoke, 'run_commands')
|
||||
get_config = partial(invoke, 'get_config')
|
||||
load_config = partial(invoke, 'load_config')
|
||||
supports_sessions = partial(invoke, 'supports_sessions')
|
||||
|
||||
def check_args(module, warnings):
|
||||
if SHARED_LIB == 'eapi':
|
||||
eapi.check_args(module)
|
||||
|
||||
if module.params['force']:
|
||||
warnings.append('The force argument is deprecated, please use '
|
||||
'match=none instead. This argument will be '
|
||||
'removed in the future')
|
||||
|
||||
if not module.connection.supports_sessions():
|
||||
if not supports_sessions(module):
|
||||
warnings.append('The current version of EOS on the remote device does '
|
||||
'not support configuration sessions. The commit '
|
||||
'argument will be ignored')
|
||||
|
@ -241,25 +267,6 @@ def get_candidate(module):
|
|||
candidate.add(module.params['lines'], parents=parents)
|
||||
return candidate
|
||||
|
||||
def get_config(module, defaults=False):
|
||||
contents = module.params['config']
|
||||
if not contents:
|
||||
defaults = module.params['defaults']
|
||||
contents = module.config.get_config(include_defaults=defaults)
|
||||
return NetworkConfig(indent=3, contents=contents)
|
||||
|
||||
def load_config(module, commands, result):
|
||||
replace = module.params['replace'] == 'config'
|
||||
commit = not module.check_mode
|
||||
|
||||
diff = module.config.load_config(commands, replace=replace, commit=commit)
|
||||
|
||||
if diff and module.connection.supports_sessions():
|
||||
result['diff'] = dict(prepared=diff)
|
||||
result['changed'] = True
|
||||
elif diff:
|
||||
result['changed'] = True
|
||||
|
||||
def run(module, result):
|
||||
match = module.params['match']
|
||||
replace = module.params['replace']
|
||||
|
@ -267,7 +274,8 @@ def run(module, result):
|
|||
candidate = get_candidate(module)
|
||||
|
||||
if match != 'none' and replace != 'config':
|
||||
config = get_config(module)
|
||||
config_text = get_config(module)
|
||||
config = NetworkConfig(indent=3, contents=config_text)
|
||||
configobjs = candidate.difference(config, match=match, replace=replace)
|
||||
else:
|
||||
configobjs = candidate.items
|
||||
|
@ -282,14 +290,17 @@ def run(module, result):
|
|||
if module.params['after']:
|
||||
commands.extend(module.params['after'])
|
||||
|
||||
result['updates'] = commands
|
||||
result['commands'] = commands
|
||||
|
||||
module.log('commands: %s' % commands)
|
||||
load_config(module, commands, result)
|
||||
replace = module.params['replace'] == 'config'
|
||||
commit = not module.check_mode
|
||||
|
||||
response = load_config(module, commands, replace=replace, commit=commit)
|
||||
if 'diff' in response:
|
||||
result['diff'] = {'prepared': response['diff']}
|
||||
if 'session' in response:
|
||||
result['session'] = response['session']
|
||||
|
||||
if module.params['save']:
|
||||
if not module.check_mode:
|
||||
module.config.save_config()
|
||||
result['changed'] = True
|
||||
|
||||
def main():
|
||||
|
@ -307,17 +318,20 @@ def main():
|
|||
match=dict(default='line', choices=['line', 'strict', 'exact', 'none']),
|
||||
replace=dict(default='line', choices=['line', 'block', 'config']),
|
||||
|
||||
# this argument is deprecated in favor of setting match: none
|
||||
# it will be removed in a future version
|
||||
force=dict(default=False, type='bool'),
|
||||
|
||||
config=dict(),
|
||||
defaults=dict(type='bool', default=False),
|
||||
|
||||
backup=dict(type='bool', default=False),
|
||||
save=dict(default=False, type='bool'),
|
||||
|
||||
# deprecated arguments (Ansible 2.3)
|
||||
config=dict(),
|
||||
# this argument is deprecated in favor of setting match: none
|
||||
# it will be removed in a future version
|
||||
force=dict(default=False, type='bool'),
|
||||
)
|
||||
|
||||
argument_spec.update(eapi.eapi_argument_spec)
|
||||
|
||||
mutually_exclusive = [('lines', 'src')]
|
||||
|
||||
required_if = [('match', 'strict', ['lines']),
|
||||
|
@ -325,10 +339,12 @@ def main():
|
|||
('replace', 'block', ['lines']),
|
||||
('replace', 'config', ['src'])]
|
||||
|
||||
module = NetworkModule(argument_spec=argument_spec,
|
||||
mutually_exclusive=mutually_exclusive,
|
||||
required_if=required_if,
|
||||
supports_check_mode=True)
|
||||
cls = get_ansible_module()
|
||||
|
||||
module = cls(argument_spec=argument_spec,
|
||||
mutually_exclusive=mutually_exclusive,
|
||||
required_if=required_if,
|
||||
supports_check_mode=True)
|
||||
|
||||
if module.params['force'] is True:
|
||||
module.params['match'] = 'none'
|
||||
|
@ -336,19 +352,24 @@ def main():
|
|||
warnings = list()
|
||||
check_args(module, warnings)
|
||||
|
||||
result = dict(changed=False, warnings=warnings)
|
||||
result = {'changed': False}
|
||||
if warnings:
|
||||
result['warnings'] = warnings
|
||||
|
||||
if module.params['backup']:
|
||||
result['__backup__'] = module.config.get_config()
|
||||
result['__backup__'] = get_config(module)
|
||||
|
||||
try:
|
||||
if any((module.params['src'], module.params['lines'])):
|
||||
run(module, result)
|
||||
except NetworkError:
|
||||
exc = get_exception()
|
||||
module.fail_json(msg=str(exc), **exc.kwargs)
|
||||
|
||||
if module.params['save']:
|
||||
if not module.check_mode:
|
||||
run_commands(module, ['copy running-config startup-config'])
|
||||
result['changed'] = True
|
||||
|
||||
module.exit_json(**result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
SHARED_LIB = 'eapi'
|
||||
main()
|
||||
|
|
|
@ -19,10 +19,16 @@
|
|||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.plugins.action import ActionBase
|
||||
from ansible.plugins.action.net_config import ActionModule as NetActionModule
|
||||
from ansible.plugins.action.net_config import ActionModule as NetworkActionModule
|
||||
|
||||
class ActionModule(NetActionModule, ActionBase):
|
||||
pass
|
||||
try:
|
||||
from __main__ import display
|
||||
except ImportError:
|
||||
from ansible.utils.display import Display
|
||||
display = Display()
|
||||
|
||||
class ActionModule(NetworkActionModule):
|
||||
|
||||
def run(self, tmp=None, task_vars=None):
|
||||
display.vvvvv('Using connection plugin %s' % self._play_context.connection)
|
||||
return NetworkActionModule.run(self, tmp, task_vars)
|
||||
|
|
|
@ -27,6 +27,7 @@ import glob
|
|||
from ansible.plugins.action.network import ActionModule as _ActionModule
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.six.moves.urllib.parse import urlsplit
|
||||
from ansible.utils.vars import merge_hash
|
||||
|
||||
|
||||
PRIVATE_KEYS_RE = re.compile('__.+__')
|
||||
|
@ -42,13 +43,17 @@ class ActionModule(_ActionModule):
|
|||
except ValueError as exc:
|
||||
return dict(failed=True, msg=exc.message)
|
||||
|
||||
result = super(ActionModule, self).run(tmp, task_vars)
|
||||
if self._play_context.connection == 'local':
|
||||
result = self.normal(tmp, task_vars)
|
||||
else:
|
||||
result = super(ActionModule, self).run(tmp, task_vars)
|
||||
|
||||
if self._task.args.get('backup') and result.get('__backup__'):
|
||||
# User requested backup and no error occurred in module.
|
||||
# NOTE: If there is a parameter error, _backup key may not be in results.
|
||||
filepath = self._write_backup(task_vars['inventory_hostname'],
|
||||
result['__backup__'])
|
||||
|
||||
result['backup_path'] = filepath
|
||||
|
||||
# strip out any keys that have two leading and two trailing
|
||||
|
@ -59,6 +64,22 @@ class ActionModule(_ActionModule):
|
|||
|
||||
return result
|
||||
|
||||
def normal(self, tmp=None, task_vars=None):
|
||||
if task_vars is None:
|
||||
task_vars = dict()
|
||||
|
||||
#results = super(ActionModule, self).run(tmp, task_vars)
|
||||
# remove as modules might hide due to nolog
|
||||
#del results['invocation']['module_args']
|
||||
|
||||
results = {}
|
||||
results = merge_hash(results, self._execute_module(tmp=tmp, task_vars=task_vars))
|
||||
|
||||
# hack to keep --verbose from showing all the setup module results
|
||||
if self._task.action == 'setup':
|
||||
results['_ansible_verbose_override'] = True
|
||||
|
||||
return results
|
||||
def _get_working_path(self):
|
||||
cwd = self._loader.get_basedir()
|
||||
if self._task._role is not None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue