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
This commit is contained in:
parent
6cbc69447b
commit
0feea66988
2 changed files with 27 additions and 9 deletions
|
@ -9,6 +9,7 @@ import pty
|
|||
import time
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
from ansible import constants as C
|
||||
|
@ -855,7 +856,11 @@ class TaskExecutor:
|
|||
Starts the persistent connection
|
||||
'''
|
||||
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)
|
||||
os.close(slave)
|
||||
|
||||
|
@ -874,9 +879,12 @@ class TaskExecutor:
|
|||
stdin.close()
|
||||
|
||||
if p.returncode == 0:
|
||||
result = json.loads(to_text(stdout))
|
||||
result = json.loads(to_text(stdout, errors='surrogate_then_replace'))
|
||||
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:
|
||||
for msg in result.get('messages'):
|
||||
|
@ -884,6 +892,7 @@ class TaskExecutor:
|
|||
|
||||
if 'error' in result:
|
||||
if self._play_context.verbosity > 2:
|
||||
if result.get('exception'):
|
||||
msg = "The full traceback is:\n" + result['exception']
|
||||
display.display(msg, color=C.COLOR_ERROR)
|
||||
raise AnsibleError(result['error'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import os
|
|||
import pty
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from ansible import constants as C
|
||||
from ansible.plugins.connection import ConnectionBase
|
||||
|
@ -75,7 +76,11 @@ class Connection(ConnectionBase):
|
|||
Starts the persistent connection
|
||||
'''
|
||||
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)
|
||||
os.close(slave)
|
||||
|
||||
|
@ -96,7 +101,10 @@ class Connection(ConnectionBase):
|
|||
if p.returncode == 0:
|
||||
result = json.loads(to_text(stdout, errors='surrogate_then_replace'))
|
||||
else:
|
||||
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:
|
||||
for msg in result.get('messages'):
|
||||
|
@ -104,6 +112,7 @@ class Connection(ConnectionBase):
|
|||
|
||||
if 'error' in result:
|
||||
if self._play_context.verbosity > 2:
|
||||
if result.get('exception'):
|
||||
msg = "The full traceback is:\n" + result['exception']
|
||||
display.display(msg, color=C.COLOR_ERROR)
|
||||
raise AnsibleError(result['error'])
|
||||
|
|
Loading…
Reference in a new issue