mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
adds feature to try to auto determine network_os (#18674)
This updates the network_cli connection plugin to attempt to automatically determine the remote device os. The device network os discovery can be overridden by setting the ansible_network_os value.
This commit is contained in:
parent
6fe9a5e40c
commit
8137c7207d
8 changed files with 56 additions and 7 deletions
|
@ -39,14 +39,8 @@ class Connection(_Connection):
|
|||
def __init__(self, play_context, new_stdin, *args, **kwargs):
|
||||
super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs)
|
||||
|
||||
assert self._play_context.network_os, 'ansible_network_os must be set'
|
||||
|
||||
self._terminal = terminal_loader.get(self._play_context.network_os, self)
|
||||
if not self._terminal:
|
||||
raise AnsibleConnectionFailure('network os %s is not supported' % self._play_context.network_os)
|
||||
|
||||
self._terminal = None
|
||||
self._shell = None
|
||||
|
||||
self._matched_prompt = None
|
||||
self._matched_pattern = None
|
||||
self._last_response = None
|
||||
|
@ -64,6 +58,24 @@ class Connection(_Connection):
|
|||
|
||||
def _connect(self):
|
||||
super(Connection, self)._connect()
|
||||
|
||||
network_os = self._play_context.network_os
|
||||
if not network_os:
|
||||
for cls in terminal_loader.all(class_only=True):
|
||||
network_os = cls.guess_network_os(self.ssh)
|
||||
if network_os:
|
||||
break
|
||||
|
||||
if not network_os:
|
||||
raise AnsibleConnectionFailure(
|
||||
'unable to determine device network os. Please configure '
|
||||
'ansible_network_os value'
|
||||
)
|
||||
|
||||
self._terminal = terminal_loader.get(network_os, self)
|
||||
if not self._terminal:
|
||||
raise AnsibleConnectionFailure('network os %s is not supported' % network_os)
|
||||
|
||||
return (0, 'connected', '')
|
||||
|
||||
def open_shell(self, timeout=10):
|
||||
|
|
|
@ -69,3 +69,6 @@ class TerminalBase(with_metaclass(ABCMeta, object)):
|
|||
def on_deauthorize(self):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def guess_network_os(conn):
|
||||
pass
|
||||
|
|
|
@ -79,4 +79,9 @@ class TerminalModule(TerminalBase):
|
|||
elif prompt.endswith('#'):
|
||||
self._exec_cli_command('disable')
|
||||
|
||||
@staticmethod
|
||||
def guess_network_os(conn):
|
||||
stdin, stdout, stderr = conn.exec_command('show version')
|
||||
if 'Arista' in stdout.read():
|
||||
return 'eos'
|
||||
|
||||
|
|
|
@ -80,3 +80,9 @@ class TerminalModule(TerminalBase):
|
|||
self._exec_cli_command('disable')
|
||||
|
||||
|
||||
@staticmethod
|
||||
def guess_network_os(conn):
|
||||
stdin, stdout, stderr = conn.exec_command('show version')
|
||||
if 'Cisco IOS Software' in stdout.read():
|
||||
return 'ios'
|
||||
|
||||
|
|
|
@ -52,4 +52,10 @@ class TerminalModule(TerminalBase):
|
|||
except AnsibleConnectionFailure:
|
||||
raise AnsibleConnectionFailure('unable to set terminal parameters')
|
||||
|
||||
@staticmethod
|
||||
def guess_network_os(conn):
|
||||
stdin, stdout, stderr = conn.exec_command('show version')
|
||||
if 'Cisco IOS XR' in stdout.read():
|
||||
return 'iosxr'
|
||||
|
||||
|
||||
|
|
|
@ -45,3 +45,9 @@ class TerminalModule(TerminalBase):
|
|||
except AnsibleConnectionFailure:
|
||||
raise AnsibleConnectionFailure('unable to set terminal parameters')
|
||||
|
||||
@staticmethod
|
||||
def guess_network_os(conn):
|
||||
stdin, stdout, stderr = conn.exec_command('show version')
|
||||
if 'Junos' in stdout.read():
|
||||
return 'junos'
|
||||
|
||||
|
|
|
@ -52,5 +52,11 @@ class TerminalModule(TerminalBase):
|
|||
except AnsibleConnectionFailure:
|
||||
raise AnsibleConnectionFailure('unable to set terminal parameters')
|
||||
|
||||
@staticmethod
|
||||
def guess_network_os(conn):
|
||||
stdin, stdout, stderr = conn.exec_command('show version')
|
||||
if 'NX-OS' in stdout.read():
|
||||
return 'nxos'
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -44,4 +44,9 @@ class TerminalModule(TerminalBase):
|
|||
except AnsibleConnectionFailure:
|
||||
raise AnsibleConnectionFailure('unable to set terminal parameters')
|
||||
|
||||
@staticmethod
|
||||
def guess_network_os(conn):
|
||||
stdin, stdout, stderr = conn.exec_command('cat /proc/version')
|
||||
if 'vyos' in stdout.read():
|
||||
return 'vyos'
|
||||
|
||||
|
|
Loading…
Reference in a new issue