Move _start_connection() to module_utils/connection and fix Popen() call (#36249)

* Fix Popen() call to use ansible python

* Remove PATH searching, reuse location of ansible instead
pull/37009/head
Nathaniel Case 7 years ago committed by GitHub
parent 6cbc69447b
commit 0feea66988
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,6 +9,7 @@ import pty
import time import time
import json import json
import subprocess import subprocess
import sys
import traceback import traceback
from ansible import constants as C from ansible import constants as C
@ -855,7 +856,11 @@ class TaskExecutor:
Starts the persistent connection Starts the persistent connection
''' '''
master, slave = pty.openpty() master, slave = pty.openpty()
p = subprocess.Popen(["ansible-connection", to_text(os.getppid())], stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
python = sys.executable
# Assume ansible-connection is in the same dir as sys.argv[0]
ansible_connection = os.path.join(os.path.dirname(sys.argv[0]), 'ansible-connection')
p = subprocess.Popen([python, ansible_connection, to_text(os.getppid())], stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdin = os.fdopen(master, 'wb', 0) stdin = os.fdopen(master, 'wb', 0)
os.close(slave) os.close(slave)
@ -874,9 +879,12 @@ class TaskExecutor:
stdin.close() stdin.close()
if p.returncode == 0: if p.returncode == 0:
result = json.loads(to_text(stdout)) result = json.loads(to_text(stdout, errors='surrogate_then_replace'))
else: else:
result = json.loads(to_text(stderr)) try:
result = json.loads(to_text(stderr, errors='surrogate_then_replace'))
except json.decoder.JSONDecodeError:
result = {'error': to_text(stderr, errors='surrogate_then_replace')}
if 'messages' in result: if 'messages' in result:
for msg in result.get('messages'): for msg in result.get('messages'):
@ -884,8 +892,9 @@ class TaskExecutor:
if 'error' in result: if 'error' in result:
if self._play_context.verbosity > 2: if self._play_context.verbosity > 2:
msg = "The full traceback is:\n" + result['exception'] if result.get('exception'):
display.display(msg, color=C.COLOR_ERROR) msg = "The full traceback is:\n" + result['exception']
display.display(msg, color=C.COLOR_ERROR)
raise AnsibleError(result['error']) raise AnsibleError(result['error'])
return result['socket_path'] return result['socket_path']

@ -17,6 +17,7 @@ import os
import pty import pty
import json import json
import subprocess import subprocess
import sys
from ansible import constants as C from ansible import constants as C
from ansible.plugins.connection import ConnectionBase from ansible.plugins.connection import ConnectionBase
@ -75,7 +76,11 @@ class Connection(ConnectionBase):
Starts the persistent connection Starts the persistent connection
''' '''
master, slave = pty.openpty() master, slave = pty.openpty()
p = subprocess.Popen(["ansible-connection", to_text(os.getppid())], stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
python = sys.executable
# Assume ansible-connection is in the same dir as sys.argv[0]
ansible_connection = os.path.join(os.path.dirname(sys.argv[0]), 'ansible-connection')
p = subprocess.Popen([python, ansible_connection, to_text(os.getppid())], stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdin = os.fdopen(master, 'wb', 0) stdin = os.fdopen(master, 'wb', 0)
os.close(slave) os.close(slave)
@ -96,7 +101,10 @@ class Connection(ConnectionBase):
if p.returncode == 0: if p.returncode == 0:
result = json.loads(to_text(stdout, errors='surrogate_then_replace')) result = json.loads(to_text(stdout, errors='surrogate_then_replace'))
else: else:
result = json.loads(to_text(stderr, errors='surrogate_then_replace')) try:
result = json.loads(to_text(stderr, errors='surrogate_then_replace'))
except json.decoder.JSONDecodeError:
result = {'error': to_text(stderr, errors='surrogate_then_replace')}
if 'messages' in result: if 'messages' in result:
for msg in result.get('messages'): for msg in result.get('messages'):
@ -104,8 +112,9 @@ class Connection(ConnectionBase):
if 'error' in result: if 'error' in result:
if self._play_context.verbosity > 2: if self._play_context.verbosity > 2:
msg = "The full traceback is:\n" + result['exception'] if result.get('exception'):
display.display(msg, color=C.COLOR_ERROR) msg = "The full traceback is:\n" + result['exception']
display.display(msg, color=C.COLOR_ERROR)
raise AnsibleError(result['error']) raise AnsibleError(result['error'])
return result['socket_path'] return result['socket_path']

Loading…
Cancel
Save