1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Parse ansible_ssh_common_args looking for ProxyCommand, for use in paramiko

This commit is contained in:
Matt Martz 2015-12-24 15:01:41 -06:00
parent a8e0763d1e
commit 0296209bc1

View file

@ -32,6 +32,7 @@ import tempfile
import traceback import traceback
import fcntl import fcntl
import sys import sys
import re
from termios import tcflush, TCIFLUSH from termios import tcflush, TCIFLUSH
from binascii import hexlify from binascii import hexlify
@ -55,6 +56,9 @@ The %s key fingerprint is %s.
Are you sure you want to continue connecting (yes/no)? Are you sure you want to continue connecting (yes/no)?
""" """
# SSH Options Regex
SETTINGS_REGEX = re.compile(r'(\w+)(?:\s*=\s*|\s+)(.+)')
# prevent paramiko warning noise -- see http://stackoverflow.com/questions/3920502/ # prevent paramiko warning noise -- see http://stackoverflow.com/questions/3920502/
HAVE_PARAMIKO=False HAVE_PARAMIKO=False
with warnings.catch_warnings(): with warnings.catch_warnings():
@ -158,14 +162,34 @@ class Connection(ConnectionBase):
pass # file was not found, but not required to function pass # file was not found, but not required to function
ssh.load_system_host_keys() ssh.load_system_host_keys()
proxy_command = None
# Parse ansible_ssh_common_args, specifically looking for ProxyCommand
ssh_common_args = getattr(self._play_context, 'ssh_common_args', None)
if ssh_common_args is not None:
args = self._split_ssh_args(ssh_common_args)
for i, arg in enumerate(args):
if arg.lower() == 'proxycommand':
# _split_ssh_args split ProxyCommand from the command itself
proxy_command = args[i + 1]
else:
# ProxyCommand and the command itself are a single string
match = SETTINGS_REGEX.match(arg)
if match:
if match.group(1).lower() == 'proxycommand':
proxy_command = match.group(2)
if proxy_command:
break
proxy_command = proxy_command or C.PARAMIKO_PROXY_COMMAND
sock_kwarg = {} sock_kwarg = {}
if C.PARAMIKO_PROXY_COMMAND: if proxy_command:
replacers = { replacers = {
'%h': self._play_context.remote_addr, '%h': self._play_context.remote_addr,
'%p': port, '%p': port,
'%r': self._play_context.remote_user '%r': self._play_context.remote_user
} }
proxy_command = C.PARAMIKO_PROXY_COMMAND
for find, replace in replacers.items(): for find, replace in replacers.items():
proxy_command = proxy_command.replace(find, str(replace)) proxy_command = proxy_command.replace(find, str(replace))
try: try: