Skip to content

Commit

Permalink
Merge pull request ansible#4301 from chrismeyersfsu/tower_modules
Browse files Browse the repository at this point in the history
Global collections path

Reviewed-by: https://github.com/softwarefactory-project-zuul[bot]
  • Loading branch information
softwarefactory-project-zuul[bot] authored Jul 17, 2019
2 parents 9eb7042 + 9b95cc2 commit f5fee8e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
13 changes: 12 additions & 1 deletion awx/main/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,10 +859,16 @@ def build_params_process_isolation(self, instance, private_data_dir, cwd):
'''
process_isolation_params = dict()
if self.should_use_proot(instance):
show_paths = self.proot_show_paths + [private_data_dir, cwd] + \
settings.AWX_PROOT_SHOW_PATHS

# Help the user out by including the collections path inside the bubblewrap environment
if getattr(settings, 'AWX_ANSIBLE_COLLECTIONS_PATHS', []):
show_paths.extend(settings.AWX_ANSIBLE_COLLECTIONS_PATHS)
process_isolation_params = {
'process_isolation': True,
'process_isolation_path': settings.AWX_PROOT_BASE_PATH,
'process_isolation_show_paths': self.proot_show_paths + [private_data_dir, cwd] + settings.AWX_PROOT_SHOW_PATHS,
'process_isolation_show_paths': show_paths,
'process_isolation_hide_paths': [
settings.AWX_PROOT_BASE_PATH,
'/etc/tower',
Expand Down Expand Up @@ -936,6 +942,11 @@ def build_env(self, instance, private_data_dir, isolated, private_data_files=Non
if self.should_use_proot(instance):
env['PROOT_TMP_DIR'] = settings.AWX_PROOT_BASE_PATH
env['AWX_PRIVATE_DATA_DIR'] = private_data_dir

if 'ANSIBLE_COLLECTIONS_PATHS' in env:
env['ANSIBLE_COLLECTIONS_PATHS'] += os.pathsep + os.pathsep.join(settings.AWX_ANSIBLE_COLLECTIONS_PATHS)
else:
env['ANSIBLE_COLLECTIONS_PATHS'] = os.pathsep.join(settings.AWX_ANSIBLE_COLLECTIONS_PATHS)
return env

def should_use_proot(self, instance):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ def substitute_run(envvars=None, **_kw):
"""
private_data_dir = envvars.pop('AWX_PRIVATE_DATA_DIR')
assert envvars.pop('ANSIBLE_INVENTORY_ENABLED') == ('auto' if use_plugin else 'script')
assert envvars.pop('ANSIBLE_COLLECTIONS_PATHS') == os.pathsep.join(settings.AWX_ANSIBLE_COLLECTIONS_PATHS)
set_files = bool(os.getenv("MAKE_INVENTORY_REFERENCE_FILES", 'false').lower()[0] not in ['f', '0'])
env, content = read_content(private_data_dir, envvars, inventory_update)
base_dir = os.path.join(DATA, script_or_plugin)
Expand Down
16 changes: 16 additions & 0 deletions awx/main/tests/unit/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ def test_uses_process_isolation(self, settings):
settings.AWX_PROOT_HIDE_PATHS = ['/AWX_PROOT_HIDE_PATHS1', '/AWX_PROOT_HIDE_PATHS2']
settings.ANSIBLE_VENV_PATH = '/ANSIBLE_VENV_PATH'
settings.AWX_VENV_PATH = '/AWX_VENV_PATH'
settings.AWX_ANSIBLE_COLLECTIONS_PATHS = ['/AWX_COLLECTION_PATH1', '/AWX_COLLECTION_PATH2']

process_isolation_params = task.build_params_process_isolation(job, private_data_dir, cwd)
assert True is process_isolation_params['process_isolation']
Expand All @@ -450,6 +451,10 @@ def test_uses_process_isolation(self, settings):
"The per-job private data dir should be in the list of directories the user can see."
assert cwd in process_isolation_params['process_isolation_show_paths'], \
"The current working directory should be in the list of directories the user can see."
assert '/AWX_COLLECTION_PATH1' in process_isolation_params['process_isolation_show_paths'], \
"AWX global collection directory 1 of 2 should get added to the list of directories the user can see."
assert '/AWX_COLLECTION_PATH2' in process_isolation_params['process_isolation_show_paths'], \
"AWX global collection directory 2 of 2 should get added to the list of directories the user can see."

for p in [settings.AWX_PROOT_BASE_PATH,
'/etc/tower',
Expand Down Expand Up @@ -509,6 +514,17 @@ def test_awx_task_env(self, patch_Job, private_data_dir):
env = task.build_env(job, private_data_dir)
assert env['FOO'] == 'BAR'

def test_awx_task_env_respects_ansible_collections_paths(self, patch_Job, private_data_dir):
job = Job(project=Project(), inventory=Inventory())

task = tasks.RunJob()
task._write_extra_vars_file = mock.Mock()

with mock.patch('awx.main.tasks.settings.AWX_ANSIBLE_COLLECTIONS_PATHS', ['/AWX_COLLECTION_PATH']):
with mock.patch('awx.main.tasks.settings.AWX_TASK_ENV', {'ANSIBLE_COLLECTIONS_PATHS': '/MY_COLLECTION1:/MY_COLLECTION2'}):
env = task.build_env(job, private_data_dir)
assert env['ANSIBLE_COLLECTIONS_PATHS'] == '/MY_COLLECTION1:/MY_COLLECTION2:/AWX_COLLECTION_PATH'

def test_valid_custom_virtualenv(self, patch_Job, private_data_dir):
job = Job(project=Project(), inventory=Inventory())

Expand Down
3 changes: 3 additions & 0 deletions awx/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,9 @@ def IS_TESTING(argv=None):
# Delete temporary directories created to store playbook run-time
AWX_CLEANUP_PATHS = True

# Expose collections to Ansible playbooks
AWX_ANSIBLE_COLLECTIONS_PATHS = ['/var/lib/awx/collections']

MIDDLEWARE = [
'awx.main.middleware.TimingMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand Down
2 changes: 2 additions & 0 deletions awx/settings/local_settings.py.docker_compose
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,5 @@ TEST_OPENSTACK_PROJECT = ''
# Azure credentials.
TEST_AZURE_USERNAME = ''
TEST_AZURE_KEY_DATA = ''

AWX_ANSIBLE_COLLECTIONS_PATHS = ['/tmp/collections']
6 changes: 6 additions & 0 deletions docs/collections.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Collections Support

AWX supports Ansible collections by appending the directories specified in `AWX_ANSIBLE_COLLECTIONS_PATHS`
to the environment variable `ANSIBLE_COLLECTIONS_PATHS`. The default value of `AWX_ANSIBLE_COLLECTIONS_PATHS`
contains `/varlib/awx/collections`. It is recommended that place your collections that you wish to call in
your playbooks into this path.

0 comments on commit f5fee8e

Please sign in to comment.