From 1ac91f1f3115378a9cc7a6f94f06d1db20689aba Mon Sep 17 00:00:00 2001 From: Mathieu Bultel Date: Thu, 3 Sep 2020 11:08:01 +0200 Subject: [PATCH] Allow to set python interper for ansible execution Some environment still use python2.7 and so /usr/bin/python interpreter for ansible. In that case, Ansible will fail to execute. This option allow the user to pass a specific python interpeter for the remote. In None python_interpreter, and connection local is set we use the local sys.executable, if both are None we use the default. Change-Id: I4bd25ed50091a963d0b1e5e3a727ece19b90747e --- validations_libs/ansible.py | 11 +++++++---- validations_libs/validation_actions.py | 5 +++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/validations_libs/ansible.py b/validations_libs/ansible.py index 04c9063a..cb4b14c6 100644 --- a/validations_libs/ansible.py +++ b/validations_libs/ansible.py @@ -122,7 +122,7 @@ class Ansible(object): def _ansible_env_var(self, output_callback, ssh_user, workdir, connection, gathering_policy, module_path, key, extra_env_variables, ansible_timeout, - callback_whitelist, base_dir): + callback_whitelist, base_dir, python_interpreter): """Handle Ansible env var for Ansible config execution""" cwd = os.getcwd() env = os.environ.copy() @@ -226,7 +226,9 @@ class Ansible(object): if self.uuid: env['ANSIBLE_UUID'] = self.uuid - if connection == 'local': + if python_interpreter: + env['ANSIBLE_PYTHON_INTERPRETER'] = python_interpreter + elif connection == 'local': env['ANSIBLE_PYTHON_INTERPRETER'] = sys.executable if gathering_policy in ('smart', 'explicit', 'implicit'): @@ -280,7 +282,7 @@ class Ansible(object): extra_env_variables=None, parallel_run=False, callback_whitelist=None, ansible_cfg=None, ansible_timeout=30, ansible_artifact_path=None, - log_path=None, run_async=False): + log_path=None, run_async=False, python_interpreter=None): if not playbook_dir: playbook_dir = workdir @@ -305,7 +307,8 @@ class Ansible(object): env = self._ansible_env_var(output_callback, ssh_user, workdir, connection, gathering_policy, module_path, key, extra_env_variables, ansible_timeout, - callback_whitelist, base_dir) + callback_whitelist, base_dir, + python_interpreter) if not ansible_artifact_path: ansible_artifact_path = constants.VALIDATION_ANSIBLE_ARTIFACT_PATH if 'ANSIBLE_CONFIG' not in env and not ansible_cfg: diff --git a/validations_libs/validation_actions.py b/validations_libs/validation_actions.py index 2829d26b..3a7df1c1 100644 --- a/validations_libs/validation_actions.py +++ b/validations_libs/validation_actions.py @@ -68,7 +68,7 @@ class ValidationActions(object): extra_env_vars=None, ansible_cfg=None, quiet=True, workdir=None, limit_hosts=None, run_async=False, base_dir=constants.DEFAULT_VALIDATIONS_BASEDIR, - log_path=None): + log_path=None, python_interpreter=None): self.log = logging.getLogger(__name__ + ".run_validations") playbooks = [] validations_dir = (validations_dir if validations_dir @@ -115,7 +115,8 @@ class ValidationActions(object): gathering_policy='explicit', ansible_artifact_path=artifacts_dir, log_path=log_path, - run_async=run_async) + run_async=run_async, + python_interpreter=python_interpreter) results.append({'playbook': _playbook, 'rc_code': _rc, 'status': _status,