Fix virtualenv ARA setup helper to work with system Ansible

Exports PYTHONPATH as part of the setup
when VIRTUAL_ENV is defined. This prevents
"No module named ara" while calling ansible.

Change-Id: I7b948c7816c896d0357a0309669276df86e29384
Story: 2001894
Signed-off-by: Sorin Sbarnea <ssbarnea@redhat.com>
This commit is contained in:
Sorin Sbarnea 2018-04-23 18:38:26 +01:00 committed by David Moreau Simard
parent 87272840bf
commit efd8d17fb5
2 changed files with 21 additions and 3 deletions

View File

@ -25,10 +25,11 @@ Quickstart
# Install ARA
pip install ara
# Make Ansible use the ARA callback plugin regardless of location or python version
export ANSIBLE_CALLBACK_PLUGINS="$(python -m ara.setup.callback_plugins)"
# Load environment variables that inform Ansible to use ARA regardless
# of its location or python version
source <(python -m ara.setup.env)
# Run your playbook
# Run your Ansible playbook or commands
# ansible-playbook myplaybook.yml
# Start the ARA standalone webserver

View File

@ -17,6 +17,8 @@
from __future__ import print_function
from . import callback_plugins, action_plugins, library
import os
from distutils.sysconfig import get_python_lib
exports = """
export ANSIBLE_CALLBACK_PLUGINS={}
@ -24,5 +26,20 @@ export ANSIBLE_ACTION_PLUGINS={}
export ANSIBLE_LIBRARY={}
""".format(callback_plugins, action_plugins, library)
if 'VIRTUAL_ENV' in os.environ:
""" PYTHONPATH may be exported when 'ara' module is installed in a
virtualenv and ansible is installed on system python to avoid ansible
failure to find ara module.
"""
# inspired by https://stackoverflow.com/a/122340/99834
lib = get_python_lib()
if 'PYTHONPATH' in os.environ:
python_paths = os.environ['PYTHONPATH'].split(os.pathsep)
else:
python_paths = []
if lib not in python_paths:
python_paths.append(lib)
exports += "export PYTHONPATH=%s\n" % os.pathsep.join(python_paths)
if __name__ == "__main__":
print(exports.strip())