1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

prep for role passthrough instead of detection

(cherry picked from commit 2125a23f2ab0903065a13f9ee68ae43ed348d6fb)
This commit is contained in:
Brian Coca 2017-02-21 16:45:57 -05:00
parent 7798297317
commit 2a6145ca1c

View file

@ -22,12 +22,11 @@ __metaclass__ = type
import copy import copy
import os import os
import json import json
import subprocess
import tempfile import tempfile
from yaml import YAMLError from yaml import YAMLError
from ansible.compat.six import text_type, string_types from ansible.compat.six import text_type, string_types
from ansible.errors import AnsibleFileNotFound, AnsibleParserError, AnsibleError from ansible.errors import AnsibleFileNotFound, AnsibleParserError
from ansible.errors.yaml_strings import YAML_SYNTAX_ERROR from ansible.errors.yaml_strings import YAML_SYNTAX_ERROR
from ansible.module_utils.basic import is_executable from ansible.module_utils.basic import is_executable
from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.module_utils._text import to_bytes, to_native, to_text
@ -227,7 +226,7 @@ class DataLoader():
basedir = to_text(self._basedir, errors='surrogate_or_strict') basedir = to_text(self._basedir, errors='surrogate_or_strict')
return os.path.abspath(os.path.join(basedir, given)) return os.path.abspath(os.path.join(basedir, given))
def path_dwim_relative(self, path, dirname, source): def path_dwim_relative(self, path, dirname, source, is_role=False):
''' '''
find one file in either a role or playbook dir with or without find one file in either a role or playbook dir with or without
explicitly named dirname subdirs explicitly named dirname subdirs
@ -247,11 +246,13 @@ class DataLoader():
search.append(os.path.join(path, dirname, source)) search.append(os.path.join(path, dirname, source))
basedir = unfrackpath(path) basedir = unfrackpath(path)
#FIXME: this role detection will not work with alternate/missing main.yml
# is it a role and if so make sure you get correct base path # is it a role and if so make sure you get correct base path
if path.endswith('tasks') and os.path.exists(to_bytes(os.path.join(path,'main.yml'), errors='surrogate_or_strict')) \ if path.endswith('tasks') and os.path.exists(to_bytes(os.path.join(path,'main.yml'), errors='surrogate_or_strict')) \
or os.path.exists(to_bytes(os.path.join(path,'tasks/main.yml'), errors='surrogate_or_strict')): or os.path.exists(to_bytes(os.path.join(path,'tasks/main.yml'), errors='surrogate_or_strict')):
isrole = True is_role = True
if path.endswith('tasks'):
if is_role and path.endswith('tasks'):
basedir = unfrackpath(os.path.dirname(path)) basedir = unfrackpath(os.path.dirname(path))
cur_basedir = self._basedir cur_basedir = self._basedir
@ -277,7 +278,7 @@ class DataLoader():
return candidate return candidate
def path_dwim_relative_stack(self, paths, dirname, source): def path_dwim_relative_stack(self, paths, dirname, source, is_role=False):
''' '''
find one file in first path in stack taking roles into account and adding play basedir as fallback find one file in first path in stack taking roles into account and adding play basedir as fallback
@ -307,10 +308,11 @@ class DataLoader():
b_upath = to_bytes(upath, errors='surrogate_or_strict') b_upath = to_bytes(upath, errors='surrogate_or_strict')
b_mydir = os.path.dirname(b_upath) b_mydir = os.path.dirname(b_upath)
# FIXME: this detection fails with non main.yml roles
# if path is in role and 'tasks' not there already, add it into the search # if path is in role and 'tasks' not there already, add it into the search
if b_upath.endswith(b'tasks') and os.path.exists(os.path.join(b_upath, b'main.yml')) \ if is_role or (b_upath.endswith(b'tasks') and os.path.exists(os.path.join(b_upath, b'main.yml')) \
or os.path.exists(os.path.join(b_upath, b'tasks/main.yml')) \ or os.path.exists(os.path.join(b_upath, b'tasks/main.yml')) \
or os.path.exists(os.path.join(b_mydir, b'tasks/main.yml')): or os.path.exists(os.path.join(b_mydir, b'tasks/main.yml'))):
if b_mydir.endswith(b'tasks'): if b_mydir.endswith(b'tasks'):
search.append(os.path.join(os.path.dirname(b_mydir), b_dirname, b_source)) search.append(os.path.join(os.path.dirname(b_mydir), b_dirname, b_source))
search.append(os.path.join(b_mydir, b_source)) search.append(os.path.join(b_mydir, b_source))