From 4c279f5e450202d83c9441c291f0c5e531b3340c Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Tue, 22 May 2018 12:52:57 -0400 Subject: [PATCH] EdgeOS module improvements (#39530) (#40548) * Update docs and use to_text on strings * Add warning to use network_cli (cherry picked from commit d5dbd8c76d87eebd445c79e0dc79924c5fd4c478) --- .../edgeos-module-connection-warning.yml | 2 ++ .../modules/network/edgeos/edgeos_command.py | 10 ++++++--- .../modules/network/edgeos/edgeos_config.py | 22 ++++++++++++------- lib/ansible/plugins/action/edgeos_config.py | 2 ++ lib/ansible/plugins/terminal/edgeos.py | 3 +-- 5 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 changelogs/fragments/edgeos-module-connection-warning.yml diff --git a/changelogs/fragments/edgeos-module-connection-warning.yml b/changelogs/fragments/edgeos-module-connection-warning.yml new file mode 100644 index 00000000000..c74f704edec --- /dev/null +++ b/changelogs/fragments/edgeos-module-connection-warning.yml @@ -0,0 +1,2 @@ +bugfixes: + - edgeos modules - add note and warning that the modules require network_cli connection (https://github.com/ansible/ansible/issues/39499) diff --git a/lib/ansible/modules/network/edgeos/edgeos_command.py b/lib/ansible/modules/network/edgeos/edgeos_command.py index ddacd33d8b4..ac827a422b6 100644 --- a/lib/ansible/modules/network/edgeos/edgeos_command.py +++ b/lib/ansible/modules/network/edgeos/edgeos_command.py @@ -25,6 +25,9 @@ description: use a custom pager that can cause this module to hang. If the value of the environment variable C(ANSIBLE_EDGEOS_TERMINAL_LENGTH) is not set, the default number of 10000 is used. + - "This is a network module and requires C(connection: network_cli) + in order to work properly." + - For more information please see the L(Network Guide,../network/getting_started/index.html). options: commands: description: @@ -95,9 +98,10 @@ stdout_lines: import time +from ansible.module_utils._text import to_text from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.network.common.utils import ComplexList from ansible.module_utils.network.common.parsing import Conditional +from ansible.module_utils.network.common.utils import ComplexList from ansible.module_utils.network.edgeos.edgeos import run_commands from ansible.module_utils.six import string_types @@ -105,7 +109,7 @@ from ansible.module_utils.six import string_types def to_lines(stdout): for item in stdout: if isinstance(item, string_types): - item = str(item).split('\n') + item = to_text(item).split('\n') yield item @@ -149,7 +153,7 @@ def main(): try: conditionals = [Conditional(c) for c in wait_for] except AttributeError as e: - module.fail_json(msg=str(e)) + module.fail_json(msg=to_text(e)) retries = module.params['retries'] interval = module.params['interval'] diff --git a/lib/ansible/modules/network/edgeos/edgeos_config.py b/lib/ansible/modules/network/edgeos/edgeos_config.py index 4ebe7022528..c59a0043641 100644 --- a/lib/ansible/modules/network/edgeos/edgeos_config.py +++ b/lib/ansible/modules/network/edgeos/edgeos_config.py @@ -25,6 +25,9 @@ description: configuration file and state of the active configuration. All configuration statements are based on `set` and `delete` commands in the device configuration. + - "This is a network module and requires the C(connection: network_cli) in order + to work properly." + - For more information please see the L(Network Guide,../network/getting_started/index.html). notes: - Tested against EdgeOS 1.9.7 - Setting C(ANSIBLE_PERSISTENT_COMMAND_TIMEOUT) to 30 is recommended since @@ -59,13 +62,14 @@ options: choices: ['line', 'none'] backup: description: - - The C(backup) argument will backup the current devices active + - The C(backup) argument will backup the current device's active configuration to the Ansible control host prior to making any changes. The backup file will be located in the backup folder - in the root of the playbook - required: false - default: false - choices: ['yes', 'no'] + in the playbook root directory or role root directory if the + playbook is part of an ansible role. If the directory does not + exist, it is created. + type: bool + default: 'no' comment: description: - Allows a commit description to be specified to be included @@ -126,10 +130,12 @@ backup_path: import re +from ansible.module_utils._text import to_native from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.network.common.config import NetworkConfig from ansible.module_utils.network.edgeos.edgeos import load_config, get_config, run_commands + DEFAULT_COMMENT = 'configured by edgeos_config' CONFIG_FILTERS = [ @@ -154,7 +160,7 @@ def config_to_commands(config): commands = ['set %s' % cmd.replace(' {', '') for cmd in commands] else: - commands = str(candidate).split('\n') + commands = to_native(candidate).split('\n') return commands @@ -169,13 +175,13 @@ def get_candidate(module): def diff_config(commands, config): - config = [str(c).replace("'", '') for c in config.splitlines()] + config = [to_native(c).replace("'", '') for c in config.splitlines()] updates = list() visited = set() for line in commands: - item = str(line).replace("'", '') + item = to_native(line).replace("'", '') if not item.startswith('set') and not item.startswith('delete'): raise ValueError('line must start with either `set` or `delete`') diff --git a/lib/ansible/plugins/action/edgeos_config.py b/lib/ansible/plugins/action/edgeos_config.py index 8fa91ea5cd8..7c8334d1662 100644 --- a/lib/ansible/plugins/action/edgeos_config.py +++ b/lib/ansible/plugins/action/edgeos_config.py @@ -35,6 +35,8 @@ PRIVATE_KEYS_RE = re.compile('__.+__') class ActionModule(_ActionModule): def run(self, tmp=None, task_vars=None): + if self._play_context.connection != 'network_cli': + return {'failed': True, 'msg': 'Connection type %s is not valid for this module. Must use network_cli.' % self._play_context.connection} if self._task.args.get('src'): try: diff --git a/lib/ansible/plugins/terminal/edgeos.py b/lib/ansible/plugins/terminal/edgeos.py index 7184db27dd9..50f5901663c 100644 --- a/lib/ansible/plugins/terminal/edgeos.py +++ b/lib/ansible/plugins/terminal/edgeos.py @@ -1,6 +1,5 @@ # Copyright: (c) 2018, Ansible Project -# GNU General Public License v3.0+ -# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import (absolute_import, division, print_function) __metaclass__ = type