From a1026dbce55fffaf46f69f6e73c138a2de82a51a Mon Sep 17 00:00:00 2001 From: Deepak Agrawal Date: Tue, 13 Mar 2018 17:13:23 +0530 Subject: [PATCH] Eos :do not push config to device if check_mode is enabled (#37287) * eos can not check config without config session support * add testcase for check_mode without config session * fix eos eapi to read use_session env var --- lib/ansible/module_utils/network/eos/eos.py | 22 ++++++++++++-- .../eos_config/tests/cli/check_mode.yaml | 29 +++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/lib/ansible/module_utils/network/eos/eos.py b/lib/ansible/module_utils/network/eos/eos.py index a05a2c0854..d254ea71d7 100644 --- a/lib/ansible/module_utils/network/eos/eos.py +++ b/lib/ansible/module_utils/network/eos/eos.py @@ -231,7 +231,12 @@ class Cli: pass if not all((bool(use_session), self.supports_sessions)): - return self.configure(self, commands) + if commit: + return self.configure(self, commands) + else: + self._module.warn("EOS can not check config without config session") + result = {'changed': True} + return result conn = self._get_connection() session = 'ansible_%s' % int(time.time()) @@ -407,8 +412,19 @@ class Eapi: fallback to using configure() to load the commands. If that happens, there will be no returned diff or session values """ - if not self.supports_sessions: - return self.configure(self, config) + use_session = os.getenv('ANSIBLE_EOS_USE_SESSIONS', True) + try: + use_session = int(use_session) + except ValueError: + pass + + if not all((bool(use_session), self.supports_sessions)): + if commit: + return self.configure(self, config) + else: + self._module.warn("EOS can not check config without config session") + result = {'changed': True} + return result session = 'ansible_%s' % int(time.time()) result = {'session': session} diff --git a/test/integration/targets/eos_config/tests/cli/check_mode.yaml b/test/integration/targets/eos_config/tests/cli/check_mode.yaml index efcdcb833f..42f5d19f2e 100644 --- a/test/integration/targets/eos_config/tests/cli/check_mode.yaml +++ b/test/integration/targets/eos_config/tests/cli/check_mode.yaml @@ -37,4 +37,33 @@ that: - "config.session not in result.stdout[0].sessions" +- name: invalid configuration in check mode + no config session + eos_config: + lines: + - ip address 119.31.1.1 255.255.255.256 + parents: interface Loopback911 + check_mode: 1 + environment: + ANSIBLE_EOS_USE_SESSIONS: 0 + register: result + ignore_errors: yes + +- assert: + that: + - "result.changed == true" + +- name: valid configuration in check mode + no config session + eos_config: + lines: + - ip address 119.31.1.1 255.255.255.255 + parents: interface Loopback911 + check_mode: yes + register: result + environment: + ANSIBLE_EOS_USE_SESSIONS: 0 + +- assert: + that: + - "result.changed == true" + - debug: msg="END cli/check_mode.yaml on connection={{ ansible_connection }}"