2017-02-13 10:41:22 -05:00
|
|
|
#
|
|
|
|
# (c) 2016 Red Hat Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import copy
|
|
|
|
|
2017-08-01 23:15:45 +05:30
|
|
|
from ansible import constants as C
|
2017-02-13 10:41:22 -05:00
|
|
|
from ansible.plugins.action.normal import ActionModule as _ActionModule
|
2017-12-01 12:22:54 -05:00
|
|
|
from ansible.module_utils._text import to_text
|
|
|
|
from ansible.module_utils.connection import Connection
|
2017-12-03 21:42:30 +05:30
|
|
|
from ansible.module_utils.network.common.utils import load_provider
|
|
|
|
from ansible.module_utils.network.vyos.vyos import vyos_provider_spec
|
2018-11-20 17:06:51 -06:00
|
|
|
from ansible.utils.display import Display
|
2017-02-13 10:41:22 -05:00
|
|
|
|
2018-11-20 17:06:51 -06:00
|
|
|
display = Display()
|
2017-02-18 09:29:17 -05:00
|
|
|
|
2017-03-23 13:35:05 -07:00
|
|
|
|
2017-02-13 10:41:22 -05:00
|
|
|
class ActionModule(_ActionModule):
|
|
|
|
|
|
|
|
def run(self, tmp=None, task_vars=None):
|
2018-02-07 15:11:36 -08:00
|
|
|
del tmp # tmp no longer has any effect
|
|
|
|
|
2017-12-01 12:22:54 -05:00
|
|
|
socket_path = None
|
2017-02-15 06:27:54 -05:00
|
|
|
|
2017-12-01 12:22:54 -05:00
|
|
|
if self._play_context.connection == 'network_cli':
|
|
|
|
provider = self._task.args.get('provider', {})
|
|
|
|
if any(provider.values()):
|
|
|
|
display.warning('provider is unnecessary when using network_cli and will be ignored')
|
2018-05-08 12:06:37 -04:00
|
|
|
del self._task.args['provider']
|
2017-12-01 12:22:54 -05:00
|
|
|
elif self._play_context.connection == 'local':
|
|
|
|
provider = load_provider(vyos_provider_spec, self._task.args)
|
|
|
|
pc = copy.deepcopy(self._play_context)
|
|
|
|
pc.connection = 'network_cli'
|
|
|
|
pc.network_os = 'vyos'
|
|
|
|
pc.remote_addr = provider['host'] or self._play_context.remote_addr
|
|
|
|
pc.port = int(provider['port'] or self._play_context.port or 22)
|
|
|
|
pc.remote_user = provider['username'] or self._play_context.connection_user
|
|
|
|
pc.password = provider['password'] or self._play_context.password
|
|
|
|
pc.private_key_file = provider['ssh_keyfile'] or self._play_context.private_key_file
|
2017-02-18 09:29:17 -05:00
|
|
|
|
2018-01-10 13:06:47 -05:00
|
|
|
display.vvv('using connection plugin %s (was local)' % pc.connection, pc.remote_addr)
|
2017-11-09 15:04:40 -05:00
|
|
|
connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin)
|
2017-06-06 13:56:25 +05:30
|
|
|
|
2018-07-20 10:04:53 +05:30
|
|
|
command_timeout = int(provider['timeout']) if provider['timeout'] else connection.get_option('persistent_command_timeout')
|
|
|
|
connection.set_options(direct={'persistent_command_timeout': command_timeout})
|
2018-05-16 14:59:01 +02:00
|
|
|
|
2017-11-09 15:04:40 -05:00
|
|
|
socket_path = connection.run()
|
|
|
|
display.vvvv('socket_path: %s' % socket_path, pc.remote_addr)
|
|
|
|
if not socket_path:
|
|
|
|
return {'failed': True,
|
|
|
|
'msg': 'unable to open shell. Please see: ' +
|
|
|
|
'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}
|
2017-02-13 10:41:22 -05:00
|
|
|
|
2017-11-09 15:04:40 -05:00
|
|
|
task_vars['ansible_socket'] = socket_path
|
2018-01-11 16:47:48 -05:00
|
|
|
else:
|
|
|
|
return {'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection}
|
2017-02-13 10:41:22 -05:00
|
|
|
|
2017-12-01 12:22:54 -05:00
|
|
|
# make sure we are in the right cli context which should be
|
|
|
|
# enable mode and not config module
|
|
|
|
if socket_path is None:
|
|
|
|
socket_path = self._connection.socket_path
|
|
|
|
|
|
|
|
conn = Connection(socket_path)
|
|
|
|
out = conn.get_prompt()
|
|
|
|
while to_text(out, errors='surrogate_then_replace').strip().endswith(')#'):
|
|
|
|
display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr)
|
|
|
|
conn.send_command('abort')
|
|
|
|
out = conn.get_prompt()
|
|
|
|
|
2018-02-07 15:11:36 -08:00
|
|
|
result = super(ActionModule, self).run(task_vars=task_vars)
|
2017-04-07 14:34:47 -06:00
|
|
|
return result
|