Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eos_config module exit session gracefully #37920

Merged
merged 1 commit into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/ansible/module_utils/network/eos/eos.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

from ansible.module_utils._text import to_text, to_native
from ansible.module_utils.basic import env_fallback, return_values
from ansible.module_utils.connection import Connection
from ansible.module_utils.connection import Connection, ConnectionError
from ansible.module_utils.network.common.utils import to_list, ComplexList
from ansible.module_utils.six import iteritems
from ansible.module_utils.urls import fetch_url
Expand Down Expand Up @@ -121,6 +121,13 @@ def _get_connection(self):

return self._connection

def close_session(self, session):
conn = self._get_connection()
# to close session gracefully execute abort in top level session prompt.
conn.get('end')
conn.get('configure session %s' % session)
conn.get('abort')

@property
def supports_sessions(self):
if self._session_support is not None:
Expand Down Expand Up @@ -206,8 +213,10 @@ def configure(self, commands):

try:
self.send_config(commands)
except:
conn.get('abort')
except ConnectionError as exc:
conn.get('end')
message = getattr(exc, 'err', exc)
self._module.fail_json(msg="Error on executing commands %s" % commands, data=to_text(message, errors='surrogate_then_replace'))

conn.get('end')
return {}
Expand Down Expand Up @@ -235,8 +244,10 @@ def load_config(self, commands, commit=False, replace=False):

try:
self.send_config(commands)
except:
conn.get('abort')
except ConnectionError as exc:
self.close_session(session)
message = getattr(exc, 'err', exc)
self._module.fail_json(msg="Error on executing commands %s" % commands, data=to_text(message, errors='surrogate_then_replace'))

out = conn.get('show session-config diffs')
if out:
Expand All @@ -245,7 +256,7 @@ def load_config(self, commands, commit=False, replace=False):
if commit:
conn.get('commit')
else:
conn.get('abort')
self.close_session(session)

return result

Expand Down
4 changes: 4 additions & 0 deletions test/integration/targets/eos_config/templates/basic/cmds.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ip access-list test
10 permit ip host 192.168.0.2 host 192.168.0.1
20 permit ip host 192.168.0.1 host 192.168.0.2
!
40 changes: 40 additions & 0 deletions test/integration/targets/eos_config/tests/cli/check_mode.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
- debug: msg="START cli/check_mode.yaml on connection={{ ansible_connection }}"

- name: invalid configuration in check mode
eos_config:
lines:
- ip address 119.31.1.1 255.255.255.256
parents: interface Loopback911
check_mode: 1
environment:
ANSIBLE_EOS_USE_SESSIONS: 1
register: result
ignore_errors: yes

- assert:
that:
- "result.msg is defined"
- "result.failed == true"
- "'Error on executing commands' in result.msg"

- name: valid configuration in check mode
eos_config:
before:
- "no ip access-list test"
src: basic/cmds.j2
check_mode: yes
register: config

- name: check if session is removed
eos_command:
commands:
- show configuration sessions | json
provider: "{{ cli }}"
register: result

- assert:
that:
- "config.session not in result.stdout[0].sessions"

- debug: msg="END cli/check_mode.yaml on connection={{ ansible_connection }}"