|
|
@ -30,25 +30,10 @@ from ansible.plugins.action import ActionBase
|
|
|
|
class ActionModule(ActionBase):
|
|
|
|
class ActionModule(ActionBase):
|
|
|
|
|
|
|
|
|
|
|
|
TRANSFERS_FILES = False
|
|
|
|
TRANSFERS_FILES = False
|
|
|
|
VALID_FILE_EXTENSIONS = ['yaml', 'yml', 'json']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _mutually_exclusive(self):
|
|
|
|
VALID_FILE_EXTENSIONS = ['yaml', 'yml', 'json']
|
|
|
|
dir_arguments = [
|
|
|
|
VALID_DIR_ARGUMENTS = [ 'dir', 'depth', 'files_matching', 'ignore_files', 'extensions', ]
|
|
|
|
self.source_dir, self.files_matching, self.ignore_files,
|
|
|
|
VALID_FILE_ARGUMENTS = ['file', '_raw_params', 'name']
|
|
|
|
self.depth
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
if self.source_file and None not in dir_arguments:
|
|
|
|
|
|
|
|
err_msg = (
|
|
|
|
|
|
|
|
"Can not include {0} with file argument"
|
|
|
|
|
|
|
|
.format(", ".join(self.VALID_DIR_ARGUMENTS))
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
raise AnsibleError(err_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif self.source_dir and self.source_file:
|
|
|
|
|
|
|
|
err_msg = (
|
|
|
|
|
|
|
|
"Need to pass either file or dir"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
raise AnsibleError(err_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _set_dir_defaults(self):
|
|
|
|
def _set_dir_defaults(self):
|
|
|
|
if not self.depth:
|
|
|
|
if not self.depth:
|
|
|
@ -72,22 +57,7 @@ class ActionModule(ActionBase):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def _set_args(self):
|
|
|
|
def _set_args(self):
|
|
|
|
""" Set instance variables based on the arguments that were passed
|
|
|
|
""" Set instance variables based on the arguments that were passed """
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.VALID_DIR_ARGUMENTS = [
|
|
|
|
|
|
|
|
'dir', 'depth', 'files_matching', 'ignore_files', 'extensions',
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
self.VALID_FILE_ARGUMENTS = ['file', '_raw_params']
|
|
|
|
|
|
|
|
self.GLOBAL_FILE_ARGUMENTS = ['name']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.VALID_ARGUMENTS = (
|
|
|
|
|
|
|
|
self.VALID_DIR_ARGUMENTS + self.VALID_FILE_ARGUMENTS +
|
|
|
|
|
|
|
|
self.GLOBAL_FILE_ARGUMENTS
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
for arg in self._task.args:
|
|
|
|
|
|
|
|
if arg not in self.VALID_ARGUMENTS:
|
|
|
|
|
|
|
|
err_msg = '{0} is not a valid option in debug'.format(arg)
|
|
|
|
|
|
|
|
raise AnsibleError(err_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.return_results_as_name = self._task.args.get('name', None)
|
|
|
|
self.return_results_as_name = self._task.args.get('name', None)
|
|
|
|
self.source_dir = self._task.args.get('dir', None)
|
|
|
|
self.source_dir = self._task.args.get('dir', None)
|
|
|
@ -99,22 +69,36 @@ class ActionModule(ActionBase):
|
|
|
|
self.files_matching = self._task.args.get('files_matching', None)
|
|
|
|
self.files_matching = self._task.args.get('files_matching', None)
|
|
|
|
self.ignore_files = self._task.args.get('ignore_files', None)
|
|
|
|
self.ignore_files = self._task.args.get('ignore_files', None)
|
|
|
|
self.valid_extensions = self._task.args.get('extensions', self.VALID_FILE_EXTENSIONS)
|
|
|
|
self.valid_extensions = self._task.args.get('extensions', self.VALID_FILE_EXTENSIONS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# convert/validate extensions list
|
|
|
|
if isinstance(self.valid_extensions, string_types):
|
|
|
|
if isinstance(self.valid_extensions, string_types):
|
|
|
|
self.valid_extensions = list(self.valid_extensions)
|
|
|
|
self.valid_extensions = list(self.valid_extensions)
|
|
|
|
|
|
|
|
|
|
|
|
# validate
|
|
|
|
|
|
|
|
if not isinstance(self.valid_extensions, list):
|
|
|
|
if not isinstance(self.valid_extensions, list):
|
|
|
|
raise AnsibleError('Invalid type for "extensions" option, it must be a list')
|
|
|
|
raise AnsibleError('Invalid type for "extensions" option, it must be a list')
|
|
|
|
|
|
|
|
|
|
|
|
self._mutually_exclusive()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(self, tmp=None, task_vars=None):
|
|
|
|
def run(self, tmp=None, task_vars=None):
|
|
|
|
""" Load yml files recursively from a directory.
|
|
|
|
""" Load yml files recursively from a directory.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
if not task_vars:
|
|
|
|
if task_vars is None:
|
|
|
|
task_vars = dict()
|
|
|
|
task_vars = dict()
|
|
|
|
|
|
|
|
|
|
|
|
self.show_content = True
|
|
|
|
self.show_content = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Validate arguments
|
|
|
|
|
|
|
|
dirs = 0
|
|
|
|
|
|
|
|
files = 0
|
|
|
|
|
|
|
|
for arg in self._task.args:
|
|
|
|
|
|
|
|
if arg in self.VALID_DIR_ARGUMENTS:
|
|
|
|
|
|
|
|
dirs += 1
|
|
|
|
|
|
|
|
elif arg in self.VALID_FILE_ARGUMENTS:
|
|
|
|
|
|
|
|
files += 1
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise AnsibleError('{0} is not a valid option in debug'.format(arg))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if dirs and files:
|
|
|
|
|
|
|
|
raise AnsibleError("Your are mixing file only and dir only arguments, these are incompatible")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# set internal vars from args
|
|
|
|
self._set_args()
|
|
|
|
self._set_args()
|
|
|
|
|
|
|
|
|
|
|
|
results = dict()
|
|
|
|
results = dict()
|
|
|
@ -123,18 +107,13 @@ class ActionModule(ActionBase):
|
|
|
|
self._set_root_dir()
|
|
|
|
self._set_root_dir()
|
|
|
|
if path.exists(self.source_dir):
|
|
|
|
if path.exists(self.source_dir):
|
|
|
|
for root_dir, filenames in self._traverse_dir_depth():
|
|
|
|
for root_dir, filenames in self._traverse_dir_depth():
|
|
|
|
failed, err_msg, updated_results = (
|
|
|
|
failed, err_msg, updated_results = (self._load_files_in_dir(root_dir, filenames))
|
|
|
|
self._load_files_in_dir(root_dir, filenames)
|
|
|
|
if failed:
|
|
|
|
)
|
|
|
|
|
|
|
|
if not failed:
|
|
|
|
|
|
|
|
results.update(updated_results)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
break
|
|
|
|
break
|
|
|
|
|
|
|
|
results.update(updated_results)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
failed = True
|
|
|
|
failed = True
|
|
|
|
err_msg = (
|
|
|
|
err_msg = ('{0} directory does not exist'.format(self.source_dir))
|
|
|
|
'{0} directory does not exist'.format(self.source_dir)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
self.source_file = self._find_needle('vars', self.source_file)
|
|
|
|
self.source_file = self._find_needle('vars', self.source_file)
|
|
|
@ -222,17 +201,11 @@ class ActionModule(ActionBase):
|
|
|
|
""" Verify if source file has a valid extension
|
|
|
|
""" Verify if source file has a valid extension
|
|
|
|
Args:
|
|
|
|
Args:
|
|
|
|
source_file (str): The full path of source file or source file.
|
|
|
|
source_file (str): The full path of source file or source file.
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Returns:
|
|
|
|
Bool
|
|
|
|
Bool
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
success = False
|
|
|
|
file_ext = path.splitext(source_file)
|
|
|
|
file_ext = source_file.split('.')
|
|
|
|
return bool(len(file_ext) > 1 and file_ext[-1] in self.valid_extensions)
|
|
|
|
if len(file_ext) >= 1:
|
|
|
|
|
|
|
|
if file_ext[-1] in self.valid_extensions:
|
|
|
|
|
|
|
|
success = True
|
|
|
|
|
|
|
|
return success
|
|
|
|
|
|
|
|
return success
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _load_files(self, filename, validate_extensions=False):
|
|
|
|
def _load_files(self, filename, validate_extensions=False):
|
|
|
|
""" Loads a file and converts the output into a valid Python dict.
|
|
|
|
""" Loads a file and converts the output into a valid Python dict.
|
|
|
@ -247,12 +220,8 @@ class ActionModule(ActionBase):
|
|
|
|
err_msg = ''
|
|
|
|
err_msg = ''
|
|
|
|
if validate_extensions and not self._is_valid_file_ext(filename):
|
|
|
|
if validate_extensions and not self._is_valid_file_ext(filename):
|
|
|
|
failed = True
|
|
|
|
failed = True
|
|
|
|
err_msg = (
|
|
|
|
err_msg = ('{0} does not have a valid extension: {1}' .format(filename, ', '.join(self.valid_extensions)))
|
|
|
|
'{0} does not have a valid extension: {1}'
|
|
|
|
else:
|
|
|
|
.format(filename, ', '.join(self.valid_extensions))
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
return failed, err_msg, results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
b_data, show_content = self._loader._get_file_contents(filename)
|
|
|
|
b_data, show_content = self._loader._get_file_contents(filename)
|
|
|
|
data = to_text(b_data, errors='surrogate_or_strict')
|
|
|
|
data = to_text(b_data, errors='surrogate_or_strict')
|
|
|
|
|
|
|
|
|
|
|
@ -262,12 +231,10 @@ class ActionModule(ActionBase):
|
|
|
|
data = dict()
|
|
|
|
data = dict()
|
|
|
|
if not isinstance(data, dict):
|
|
|
|
if not isinstance(data, dict):
|
|
|
|
failed = True
|
|
|
|
failed = True
|
|
|
|
err_msg = (
|
|
|
|
err_msg = ('{0} must be stored as a dictionary/hash' .format(filename))
|
|
|
|
'{0} must be stored as a dictionary/hash'
|
|
|
|
|
|
|
|
.format(filename)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
results.update(data)
|
|
|
|
results.update(data)
|
|
|
|
|
|
|
|
|
|
|
|
return failed, err_msg, results
|
|
|
|
return failed, err_msg, results
|
|
|
|
|
|
|
|
|
|
|
|
def _load_files_in_dir(self, root_dir, var_files):
|
|
|
|
def _load_files_in_dir(self, root_dir, var_files):
|
|
|
|