From 169209c32a69355c699f3f48cf9fe3f6f6e7bb52 Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Mon, 26 Mar 2018 12:49:30 -0400 Subject: [PATCH] Put back $PATH checking in ansible-connection call (#37933) --- lib/ansible/executor/task_executor.py | 18 +++++++++++++++--- lib/ansible/plugins/connection/persistent.py | 18 +++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py index bc1569c404..2efee392c5 100644 --- a/lib/ansible/executor/task_executor.py +++ b/lib/ansible/executor/task_executor.py @@ -858,9 +858,21 @@ class TaskExecutor: master, slave = pty.openpty() 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) + + def find_file_in_path(filename): + # Check $PATH first, followed by same directory as sys.argv[0] + paths = os.environ['PATH'].split(os.pathsep) + [os.path.dirname(sys.argv[0])] + for dirname in paths: + fullpath = os.path.join(dirname, filename) + if os.path.isfile(fullpath): + return fullpath + + raise AnsibleError("Unable to find location of '%s'" % filename) + + p = subprocess.Popen( + [python, find_file_in_path('ansible-connection'), to_text(os.getppid())], + stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) stdin = os.fdopen(master, 'wb', 0) os.close(slave) diff --git a/lib/ansible/plugins/connection/persistent.py b/lib/ansible/plugins/connection/persistent.py index 16faf1a3e6..1e377a75dd 100644 --- a/lib/ansible/plugins/connection/persistent.py +++ b/lib/ansible/plugins/connection/persistent.py @@ -78,9 +78,21 @@ class Connection(ConnectionBase): master, slave = pty.openpty() 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) + + def find_file_in_path(filename): + # Check $PATH first, followed by same directory as sys.argv[0] + paths = os.environ['PATH'].split(os.pathsep) + [os.path.dirname(sys.argv[0])] + for dirname in paths: + fullpath = os.path.join(dirname, filename) + if os.path.isfile(fullpath): + return fullpath + + raise AnsibleError("Unable to find location of '%s'" % filename) + + p = subprocess.Popen( + [python, find_file_in_path('ansible-connection'), to_text(os.getppid())], + stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) stdin = os.fdopen(master, 'wb', 0) os.close(slave)