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

verifies cli context for iosxr #21550

Merged
merged 1 commit into from
Feb 17, 2017
Merged
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
30 changes: 27 additions & 3 deletions lib/ansible/plugins/action/iosxr.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,24 @@
from ansible.module_utils.iosxr import iosxr_argument_spec
from ansible.module_utils.basic import AnsibleFallbackNotFound

try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()


class ActionModule(_ActionModule):

def run(self, tmp=None, task_vars=None):

if self._play_context.connection != 'local':
return dict(
failed=True,
msg='invalid connection specified, expected connection=local, '
'got %s' % self._play_context.connection
)

provider = self.load_provider()

pc = copy.deepcopy(self._play_context)
Expand All @@ -44,15 +58,25 @@ def run(self, tmp=None, task_vars=None):
pc.remote_user = provider['username'] or self._play_context.connection_user
pc.password = provider['password'] or self._play_context.password

connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin)

socket_path = self._get_socket_path(pc)
if not os.path.exists(socket_path):
# start the connection if it isn't started
connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin)
version = connection.exec_command('EXEC: show version')
rc, out, err = connection.exec_command('open_shell()')
if rc != 0:
return {'failed': True, 'msg': 'unable to open shell', 'rc': rc}

task_vars['ansible_socket'] = socket_path

return super(ActionModule, self).run(tmp, task_vars)
result = super(ActionModule, self).run(tmp, task_vars)

# need to make sure to leave config mode if the module didn't clean up
rc, out, err = connection.exec_command('prompt()')
if str(out).strip().endswith(')#'):
connection.exec_command('exit')

return result

def _get_socket_path(self, play_context):
ssh = connection_loader.get('ssh', class_only=True)
Expand Down