mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Some simplification of include_vars and renamed include_files to include_vars.
This commit is contained in:
parent
b69ab89eb1
commit
7f125567cb
3 changed files with 45 additions and 63 deletions
|
@ -29,31 +29,19 @@ class ActionModule(object):
|
||||||
self.runner = runner
|
self.runner = runner
|
||||||
|
|
||||||
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
|
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
|
||||||
if not module_args and not 'first_available_file' in inject:
|
|
||||||
result = dict(failed=True, msg="No Source file Given.")
|
if not module_args:
|
||||||
|
result = dict(failed=True, msg="No source file given")
|
||||||
return ReturnData(conn=conn, comm_ok=True, result=result)
|
return ReturnData(conn=conn, comm_ok=True, result=result)
|
||||||
if 'first_available_file' in inject:
|
|
||||||
found = False
|
source = module_args
|
||||||
for fn in self.runner.module_vars.get('first_available_file'):
|
|
||||||
fn_orig = fn
|
|
||||||
fnt = template.template(self.runner.basedir, fn, inject)
|
|
||||||
fnd = utils.path_dwim(self.runner.basedir, fnt)
|
|
||||||
if not os.path.exists(fnd) and '_original_file' in inject:
|
|
||||||
fnd = utils.path_dwim_relative(inject['_original_file'], 'templates', fnt, self.runner.basedir, check=False)
|
|
||||||
if os.path.exists(fnd):
|
|
||||||
source = fnd
|
|
||||||
found = True
|
|
||||||
break
|
|
||||||
if not found:
|
|
||||||
result = dict(failed=True, msg="could not find src in first_available_file list")
|
|
||||||
return ReturnData(conn=conn, comm_ok=False, result=result)
|
|
||||||
if not found:
|
|
||||||
source = module_args
|
|
||||||
source = template.template(self.runner.basedir, source, inject)
|
source = template.template(self.runner.basedir, source, inject)
|
||||||
|
|
||||||
if '_original_file' in inject:
|
if '_original_file' in inject:
|
||||||
source = utils.path_dwim_relative(inject['_original_file'], 'files', source, self.runner.basedir)
|
source = utils.path_dwim_relative(inject['_original_file'], 'files', source, self.runner.basedir)
|
||||||
else:
|
else:
|
||||||
source = utils.path_dwim(self.runner.basedir, source)
|
source = utils.path_dwim(self.runner.basedir, source)
|
||||||
|
|
||||||
if os.path.exists(source):
|
if os.path.exists(source):
|
||||||
data = utils.parse_yaml_from_file(source)
|
data = utils.parse_yaml_from_file(source)
|
||||||
if type(data) != dict:
|
if type(data) != dict:
|
|
@ -1,44 +0,0 @@
|
||||||
# -*- mode: python -*-
|
|
||||||
|
|
||||||
# Ansible is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
|
||||||
---
|
|
||||||
author: Benno Joy
|
|
||||||
module: include_files
|
|
||||||
short_description: Load variables in a file from a task
|
|
||||||
description:
|
|
||||||
- This module allows loading variables from a yaml file similar to vars_files but from a task.
|
|
||||||
it would be specially useful for conditionally loading vars file from a role.
|
|
||||||
- These variables will survive between plays.
|
|
||||||
options:
|
|
||||||
free-form:
|
|
||||||
description:
|
|
||||||
- Add the file name from which variables should be loaded, if called from a role it will look for
|
|
||||||
the file in files directory else the path would be relative to playbook. An absolute path can
|
|
||||||
also be provided.
|
|
||||||
required: false
|
|
||||||
version_added: "1.4"
|
|
||||||
'''
|
|
||||||
|
|
||||||
EXAMPLES = '''
|
|
||||||
# Example loading variable conditionally based on os distribution
|
|
||||||
- include_files: "{{ ansible_os_distribution }}.yml"
|
|
||||||
|
|
||||||
# Example loading multiple variable files
|
|
||||||
- include_files: "{{ item }}"
|
|
||||||
with_fileglob: "*.yml"
|
|
||||||
|
|
||||||
#Example loading the first available file
|
|
||||||
- include_files:
|
|
||||||
first_available_file:
|
|
||||||
- "foo.yml"
|
|
||||||
- "bar.yml"
|
|
||||||
|
|
||||||
'''
|
|
38
library/utilities/include_vars
Normal file
38
library/utilities/include_vars
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
# -*- mode: python -*-
|
||||||
|
|
||||||
|
# Ansible is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
|
author: Benno Joy
|
||||||
|
module: include_vars
|
||||||
|
short_description: Load variables from files, dynamically within a task.
|
||||||
|
description:
|
||||||
|
- Loads variables from a YAML file dynamically during task runtime. It can work with conditionals, or use host specific variables to determine the path name to load from.
|
||||||
|
options:
|
||||||
|
free-form:
|
||||||
|
description:
|
||||||
|
- The file name from which variables should be loaded, if called from a role it will look for
|
||||||
|
the file in files/ subdirectory of the role, otherwise the path would be relative to playbook. An absolute path can also be provided.
|
||||||
|
required: true
|
||||||
|
version_added: "1.4"
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = """
|
||||||
|
# Conditionally decide to load in variables when x is 0, otherwise do not.
|
||||||
|
- include_vars: contingency_plan.yml
|
||||||
|
when: x == 0
|
||||||
|
|
||||||
|
# Load a variable file based on the OS type, or a default if not found.
|
||||||
|
- include_vars: "{{ item }}"
|
||||||
|
with_first_found:
|
||||||
|
- "{{ ansible_os_distribution }}.yml"
|
||||||
|
- "default.yml"
|
||||||
|
|
||||||
|
"""
|
Loading…
Reference in a new issue