Check the user's ansible.cfg for role/collection paths.

There's no other way to add our new paths reliably without breaking things.
This commit is contained in:
Bill Nottingham
2019-10-10 15:08:20 -04:00
parent ed52e8348b
commit 31bdde00c9
2 changed files with 33 additions and 3 deletions

View File

@@ -5,11 +5,15 @@
import codecs
import re
import os
import logging
from itertools import islice
from configparser import ConfigParser
# Django
from django.utils.encoding import smart_str
logger = logging.getLogger('awx.main.utils.ansible')
__all__ = ['skip_directory', 'could_be_playbook', 'could_be_inventory']
@@ -97,3 +101,20 @@ def could_be_inventory(project_path, dir_path, filename):
except IOError:
return None
return inventory_rel_path
def read_ansible_config(project_path, variables_of_interest):
fnames = ['/etc/ansible/ansible.cfg']
if project_path:
fnames.insert(0, os.path.join(project_path, 'ansible.cfg'))
values = {}
try:
parser = ConfigParser()
parser.read(fnames)
if 'defaults' in parser:
for var in variables_of_interest:
if var in parser['defaults']:
values[var] = parser['defaults'][var]
except Exception as e:
logger.warn('Failed to read ansible configuration(s) {}: {}'.format(fnames, e))
return values