2014-11-04 22:16:11 +01:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2015-04-13 22:28:01 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2014-11-06 20:14:38 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
2016-01-25 19:04:52 +01:00
|
|
|
from ansible import constants as C
|
|
|
|
from ansible.compat.six import string_types
|
2016-09-06 02:07:58 +02:00
|
|
|
from ansible.errors import AnsibleParserError, AnsibleUndefinedVariable, AnsibleFileNotFound
|
2014-11-04 22:16:11 +01:00
|
|
|
|
2016-01-25 19:04:52 +01:00
|
|
|
try:
|
|
|
|
from __main__ import display
|
|
|
|
except ImportError:
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
display = Display()
|
|
|
|
|
2014-11-06 20:14:38 +01:00
|
|
|
|
2015-04-30 18:13:43 +02:00
|
|
|
def load_list_of_blocks(ds, play, parent_block=None, role=None, task_include=None, use_handlers=False, variable_manager=None, loader=None):
|
2014-11-04 22:16:11 +01:00
|
|
|
'''
|
|
|
|
Given a list of mixed task/block data (parsed from YAML),
|
|
|
|
return a list of Block() objects, where implicit blocks
|
|
|
|
are created for each bare Task.
|
|
|
|
'''
|
2016-05-22 15:43:44 +02:00
|
|
|
|
2014-11-04 22:16:11 +01:00
|
|
|
# we import here to prevent a circular dependency with imports
|
|
|
|
from ansible.playbook.block import Block
|
2016-11-03 17:57:23 +01:00
|
|
|
from ansible.playbook.task_include import TaskInclude
|
|
|
|
from ansible.playbook.role_include import IncludeRole
|
2014-11-04 22:16:11 +01:00
|
|
|
|
2015-12-02 17:35:20 +01:00
|
|
|
assert isinstance(ds, (list, type(None)))
|
2014-11-04 22:16:11 +01:00
|
|
|
|
|
|
|
block_list = []
|
|
|
|
if ds:
|
2016-08-01 21:10:02 +02:00
|
|
|
for block_ds in ds:
|
2014-11-14 23:14:08 +01:00
|
|
|
b = Block.load(
|
2016-08-01 21:10:02 +02:00
|
|
|
block_ds,
|
2015-04-30 18:13:43 +02:00
|
|
|
play=play,
|
2014-11-14 23:14:08 +01:00
|
|
|
parent_block=parent_block,
|
|
|
|
role=role,
|
|
|
|
task_include=task_include,
|
|
|
|
use_handlers=use_handlers,
|
|
|
|
variable_manager=variable_manager,
|
2016-11-11 09:34:01 +01:00
|
|
|
loader=loader,
|
2014-11-14 23:14:08 +01:00
|
|
|
)
|
2015-09-12 17:27:30 +02:00
|
|
|
# Implicit blocks are created by bare tasks listed in a play without
|
2015-08-17 19:45:07 +02:00
|
|
|
# an explicit block statement. If we have two implicit blocks in a row,
|
|
|
|
# squash them down to a single block to save processing time later.
|
|
|
|
if b._implicit and len(block_list) > 0 and block_list[-1]._implicit:
|
2015-09-12 17:27:30 +02:00
|
|
|
for t in b.block:
|
2016-11-03 17:57:23 +01:00
|
|
|
if isinstance(t._parent, (TaskInclude, IncludeRole)):
|
|
|
|
t._parent._parent = block_list[-1]
|
|
|
|
else:
|
|
|
|
t._parent = block_list[-1]
|
2015-08-17 19:45:07 +02:00
|
|
|
block_list[-1].block.extend(b.block)
|
|
|
|
else:
|
|
|
|
block_list.append(b)
|
2014-11-04 22:16:11 +01:00
|
|
|
|
|
|
|
return block_list
|
|
|
|
|
2014-11-05 15:00:00 +01:00
|
|
|
|
2015-04-30 18:13:43 +02:00
|
|
|
def load_list_of_tasks(ds, play, block=None, role=None, task_include=None, use_handlers=False, variable_manager=None, loader=None):
|
2014-11-04 22:16:11 +01:00
|
|
|
'''
|
|
|
|
Given a list of task datastructures (parsed from YAML),
|
2014-11-05 15:00:00 +01:00
|
|
|
return a list of Task() or TaskInclude() objects.
|
2014-11-04 22:16:11 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
# we import here to prevent a circular dependency with imports
|
2015-03-25 19:51:40 +01:00
|
|
|
from ansible.playbook.block import Block
|
2014-11-14 23:14:08 +01:00
|
|
|
from ansible.playbook.handler import Handler
|
2014-11-04 22:16:11 +01:00
|
|
|
from ansible.playbook.task import Task
|
2016-01-25 19:04:52 +01:00
|
|
|
from ansible.playbook.task_include import TaskInclude
|
2016-08-26 19:42:13 +02:00
|
|
|
from ansible.playbook.role_include import IncludeRole
|
2016-05-22 15:43:44 +02:00
|
|
|
from ansible.playbook.handler_task_include import HandlerTaskInclude
|
2016-01-25 19:04:52 +01:00
|
|
|
from ansible.template import Templar
|
2014-11-04 22:16:11 +01:00
|
|
|
|
2015-09-23 14:56:36 +02:00
|
|
|
assert isinstance(ds, list)
|
2014-11-04 22:16:11 +01:00
|
|
|
|
|
|
|
task_list = []
|
2016-01-25 19:04:52 +01:00
|
|
|
for task_ds in ds:
|
|
|
|
assert isinstance(task_ds, dict)
|
2014-11-05 15:00:00 +01:00
|
|
|
|
2016-01-25 19:04:52 +01:00
|
|
|
if 'block' in task_ds:
|
2015-03-25 19:51:40 +01:00
|
|
|
t = Block.load(
|
2016-01-25 19:04:52 +01:00
|
|
|
task_ds,
|
2015-04-30 18:13:43 +02:00
|
|
|
play=play,
|
2015-03-25 19:51:40 +01:00
|
|
|
parent_block=block,
|
|
|
|
role=role,
|
2016-08-01 21:10:02 +02:00
|
|
|
task_include=task_include,
|
2015-03-25 19:51:40 +01:00
|
|
|
use_handlers=use_handlers,
|
|
|
|
variable_manager=variable_manager,
|
|
|
|
loader=loader,
|
|
|
|
)
|
2016-01-25 19:04:52 +01:00
|
|
|
task_list.append(t)
|
2015-03-25 19:51:40 +01:00
|
|
|
else:
|
2016-01-25 19:04:52 +01:00
|
|
|
if 'include' in task_ds:
|
2016-05-22 15:43:44 +02:00
|
|
|
if use_handlers:
|
2016-08-01 21:10:02 +02:00
|
|
|
include_class = HandlerTaskInclude
|
2016-05-22 15:43:44 +02:00
|
|
|
else:
|
2016-08-01 21:10:02 +02:00
|
|
|
include_class = TaskInclude
|
|
|
|
|
|
|
|
t = include_class.load(
|
|
|
|
task_ds,
|
|
|
|
block=block,
|
|
|
|
role=role,
|
|
|
|
task_include=None,
|
|
|
|
variable_manager=variable_manager,
|
|
|
|
loader=loader
|
|
|
|
)
|
|
|
|
|
2016-01-25 19:04:52 +01:00
|
|
|
all_vars = variable_manager.get_vars(loader=loader, play=play, task=t)
|
|
|
|
templar = Templar(loader=loader, variables=all_vars)
|
|
|
|
|
2016-05-02 18:00:43 +02:00
|
|
|
# check to see if this include is dynamic or static:
|
|
|
|
# 1. the user has set the 'static' option to false or true
|
2016-01-25 19:04:52 +01:00
|
|
|
# 2. one of the appropriate config options was set
|
2016-05-02 18:00:43 +02:00
|
|
|
if t.static is not None:
|
|
|
|
is_static = t.static
|
|
|
|
else:
|
|
|
|
is_static = C.DEFAULT_TASK_INCLUDES_STATIC or \
|
2017-01-29 08:28:53 +01:00
|
|
|
(use_handlers and C.DEFAULT_HANDLER_INCLUDES_STATIC) or \
|
|
|
|
(not templar._contains_vars(t.args['_raw_params']) and t.all_parents_static() and not t.loop)
|
2016-01-25 19:04:52 +01:00
|
|
|
|
|
|
|
if is_static:
|
|
|
|
if t.loop is not None:
|
|
|
|
raise AnsibleParserError("You cannot use 'static' on an include with a loop", obj=task_ds)
|
|
|
|
|
2016-08-11 19:23:20 +02:00
|
|
|
# we set a flag to indicate this include was static
|
|
|
|
t.statically_loaded = True
|
2016-01-25 19:04:52 +01:00
|
|
|
|
2016-08-11 19:23:20 +02:00
|
|
|
# handle relative includes by walking up the list of parent include
|
|
|
|
# tasks and checking the relative result to see if it exists
|
|
|
|
parent_include = block
|
|
|
|
cumulative_path = None
|
|
|
|
|
|
|
|
found = False
|
2016-08-15 17:07:54 +02:00
|
|
|
subdir = 'tasks'
|
|
|
|
if use_handlers:
|
|
|
|
subdir = 'handlers'
|
2016-08-11 19:23:20 +02:00
|
|
|
while parent_include is not None:
|
|
|
|
if not isinstance(parent_include, TaskInclude):
|
|
|
|
parent_include = parent_include._parent
|
|
|
|
continue
|
|
|
|
parent_include_dir = templar.template(os.path.dirname(parent_include.args.get('_raw_params')))
|
|
|
|
if cumulative_path is None:
|
|
|
|
cumulative_path = parent_include_dir
|
|
|
|
elif not os.path.isabs(cumulative_path):
|
|
|
|
cumulative_path = os.path.join(parent_include_dir, cumulative_path)
|
|
|
|
include_target = templar.template(t.args['_raw_params'])
|
|
|
|
if t._role:
|
2016-08-15 17:07:54 +02:00
|
|
|
new_basedir = os.path.join(t._role._role_path, subdir, cumulative_path)
|
|
|
|
include_file = loader.path_dwim_relative(new_basedir, subdir, include_target)
|
2016-08-11 19:23:20 +02:00
|
|
|
else:
|
|
|
|
include_file = loader.path_dwim_relative(loader.get_basedir(), cumulative_path, include_target)
|
|
|
|
|
|
|
|
if os.path.exists(include_file):
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
parent_include = parent_include._parent
|
|
|
|
|
|
|
|
if not found:
|
2016-01-25 19:04:52 +01:00
|
|
|
try:
|
|
|
|
include_target = templar.template(t.args['_raw_params'])
|
2016-08-26 19:42:13 +02:00
|
|
|
except AnsibleUndefinedVariable:
|
2016-01-25 19:04:52 +01:00
|
|
|
raise AnsibleParserError(
|
2017-01-29 08:28:53 +01:00
|
|
|
"Error when evaluating variable in include name: %s.\n\n" \
|
|
|
|
"When using static includes, ensure that any variables used in their names are defined in vars/vars_files\n" \
|
|
|
|
"or extra-vars passed in from the command line. Static includes cannot use variables from inventory\n" \
|
|
|
|
"sources like group or host vars." % t.args['_raw_params'],
|
|
|
|
obj=task_ds,
|
|
|
|
suppress_extended_error=True,
|
|
|
|
)
|
2016-01-25 19:04:52 +01:00
|
|
|
if t._role:
|
2016-08-15 17:07:54 +02:00
|
|
|
include_file = loader.path_dwim_relative(t._role._role_path, subdir, include_target)
|
2016-01-25 19:04:52 +01:00
|
|
|
else:
|
|
|
|
include_file = loader.path_dwim(include_target)
|
|
|
|
|
2016-04-12 15:29:43 +02:00
|
|
|
try:
|
|
|
|
data = loader.load_from_file(include_file)
|
|
|
|
if data is None:
|
|
|
|
return []
|
|
|
|
elif not isinstance(data, list):
|
2016-08-26 14:53:28 +02:00
|
|
|
raise AnsibleParserError("included task files must contain a list of tasks", obj=data)
|
2016-08-11 19:23:20 +02:00
|
|
|
|
|
|
|
# since we can't send callbacks here, we display a message directly in
|
|
|
|
# the same fashion used by the on_include callback. We also do it here,
|
|
|
|
# because the recursive nature of helper methods means we may be loading
|
|
|
|
# nested includes, and we want the include order printed correctly
|
2016-10-06 15:55:24 +02:00
|
|
|
display.vv("statically included: %s" % include_file)
|
2016-08-26 19:42:13 +02:00
|
|
|
except AnsibleFileNotFound:
|
2016-04-12 15:29:43 +02:00
|
|
|
if t.static or \
|
|
|
|
C.DEFAULT_TASK_INCLUDES_STATIC or \
|
|
|
|
C.DEFAULT_HANDLER_INCLUDES_STATIC and use_handlers:
|
|
|
|
raise
|
|
|
|
display.deprecated(
|
|
|
|
"Included file '%s' not found, however since this include is not " \
|
|
|
|
"explicitly marked as 'static: yes', we will try and include it dynamically " \
|
|
|
|
"later. In the future, this will be an error unless 'static: no' is used " \
|
|
|
|
"on the include task. If you do not want missing includes to be considered " \
|
|
|
|
"dynamic, use 'static: yes' on the include or set the global ansible.cfg " \
|
|
|
|
"options to make all inclues static for tasks and/or handlers" % include_file,
|
|
|
|
)
|
|
|
|
task_list.append(t)
|
|
|
|
continue
|
2016-01-25 19:04:52 +01:00
|
|
|
|
2016-11-11 09:34:01 +01:00
|
|
|
ti_copy = t.copy(exclude_parent=True)
|
|
|
|
ti_copy._parent = block
|
2016-01-25 19:04:52 +01:00
|
|
|
included_blocks = load_list_of_blocks(
|
|
|
|
data,
|
|
|
|
play=play,
|
2016-08-01 21:10:02 +02:00
|
|
|
parent_block=None,
|
2016-11-11 09:34:01 +01:00
|
|
|
task_include=ti_copy,
|
2016-01-25 19:04:52 +01:00
|
|
|
role=role,
|
|
|
|
use_handlers=use_handlers,
|
|
|
|
loader=loader,
|
|
|
|
variable_manager=variable_manager,
|
|
|
|
)
|
|
|
|
|
|
|
|
# pop tags out of the include args, if they were specified there, and assign
|
|
|
|
# them to the include. If the include already had tags specified, we raise an
|
|
|
|
# error so that users know not to specify them both ways
|
2016-11-11 09:34:01 +01:00
|
|
|
tags = ti_copy.vars.pop('tags', [])
|
2016-01-25 19:04:52 +01:00
|
|
|
if isinstance(tags, string_types):
|
|
|
|
tags = tags.split(',')
|
|
|
|
|
|
|
|
if len(tags) > 0:
|
2016-11-11 09:34:01 +01:00
|
|
|
if len(ti_copy.tags) > 0:
|
2016-01-25 19:04:52 +01:00
|
|
|
raise AnsibleParserError(
|
2016-08-01 21:10:02 +02:00
|
|
|
"Include tasks should not specify tags in more than one way (both via args and directly on the task). " \
|
|
|
|
"Mixing styles in which tags are specified is prohibited for whole import hierarchy, not only for single import statement",
|
2016-01-25 19:04:52 +01:00
|
|
|
obj=task_ds,
|
|
|
|
suppress_extended_error=True,
|
|
|
|
)
|
|
|
|
display.deprecated("You should not specify tags in the include parameters. All tags should be specified using the task-level option")
|
|
|
|
else:
|
2016-11-11 09:34:01 +01:00
|
|
|
tags = ti_copy.tags[:]
|
2016-01-25 19:04:52 +01:00
|
|
|
|
|
|
|
# now we extend the tags on each of the included blocks
|
|
|
|
for b in included_blocks:
|
|
|
|
b.tags = list(set(b.tags).union(tags))
|
|
|
|
# END FIXME
|
|
|
|
|
|
|
|
# FIXME: handlers shouldn't need this special handling, but do
|
|
|
|
# right now because they don't iterate blocks correctly
|
|
|
|
if use_handlers:
|
|
|
|
for b in included_blocks:
|
|
|
|
task_list.extend(b.block)
|
|
|
|
else:
|
|
|
|
task_list.extend(included_blocks)
|
|
|
|
else:
|
|
|
|
task_list.append(t)
|
2016-08-26 19:42:13 +02:00
|
|
|
|
|
|
|
elif 'include_role' in task_ds:
|
2016-09-06 02:07:58 +02:00
|
|
|
|
|
|
|
ir = IncludeRole.load(
|
2017-01-29 08:28:53 +01:00
|
|
|
task_ds,
|
|
|
|
block=block,
|
|
|
|
role=role,
|
|
|
|
task_include=None,
|
|
|
|
variable_manager=variable_manager,
|
|
|
|
loader=loader
|
|
|
|
)
|
2016-09-06 02:07:58 +02:00
|
|
|
|
|
|
|
# 1. the user has set the 'static' option to false or true
|
|
|
|
# 2. one of the appropriate config options was set
|
|
|
|
if ir.static is not None:
|
|
|
|
is_static = ir.static
|
|
|
|
else:
|
|
|
|
display.debug('Determine if include_role is static')
|
|
|
|
# Check to see if this include is dynamic or static:
|
|
|
|
all_vars = variable_manager.get_vars(loader=loader, play=play, task=ir)
|
|
|
|
templar = Templar(loader=loader, variables=all_vars)
|
|
|
|
needs_templating = False
|
|
|
|
for param in ir.args:
|
|
|
|
if templar._contains_vars(ir.args[param]):
|
|
|
|
if not templar.templatable(ir.args[param]):
|
|
|
|
needs_templating = True
|
|
|
|
break
|
|
|
|
is_static = C.DEFAULT_TASK_INCLUDES_STATIC or \
|
2017-01-27 22:04:59 +01:00
|
|
|
(use_handlers and C.DEFAULT_HANDLER_INCLUDES_STATIC) or \
|
2016-09-06 02:07:58 +02:00
|
|
|
(not needs_templating and ir.all_parents_static() and not ir.loop)
|
|
|
|
display.debug('Determined that if include_role static is %s' % str(is_static))
|
|
|
|
if is_static:
|
|
|
|
# uses compiled list from object
|
|
|
|
t = task_list.extend(ir.get_block_list(variable_manager=variable_manager, loader=loader))
|
|
|
|
else:
|
|
|
|
# passes task object itself for latter generation of list
|
|
|
|
t = task_list.append(ir)
|
2014-11-14 23:14:08 +01:00
|
|
|
else:
|
2016-05-22 15:43:44 +02:00
|
|
|
if use_handlers:
|
|
|
|
t = Handler.load(task_ds, block=block, role=role, task_include=task_include, variable_manager=variable_manager, loader=loader)
|
|
|
|
else:
|
|
|
|
t = Task.load(task_ds, block=block, role=role, task_include=task_include, variable_manager=variable_manager, loader=loader)
|
2016-08-26 19:42:13 +02:00
|
|
|
|
2016-01-25 19:04:52 +01:00
|
|
|
task_list.append(t)
|
2014-11-04 22:16:11 +01:00
|
|
|
|
|
|
|
return task_list
|
|
|
|
|
2014-11-05 15:00:00 +01:00
|
|
|
|
2015-08-03 22:25:08 +02:00
|
|
|
def load_list_of_roles(ds, play, current_role_path=None, variable_manager=None, loader=None):
|
2014-11-04 22:16:11 +01:00
|
|
|
'''
|
|
|
|
Loads and returns a list of RoleInclude objects from the datastructure
|
|
|
|
list of role definitions
|
|
|
|
'''
|
|
|
|
|
|
|
|
# we import here to prevent a circular dependency with imports
|
|
|
|
from ansible.playbook.role.include import RoleInclude
|
|
|
|
|
2015-09-23 14:56:36 +02:00
|
|
|
assert isinstance(ds, list)
|
2014-11-04 22:16:11 +01:00
|
|
|
|
|
|
|
roles = []
|
|
|
|
for role_def in ds:
|
2015-08-03 22:25:08 +02:00
|
|
|
i = RoleInclude.load(role_def, play=play, current_role_path=current_role_path, variable_manager=variable_manager, loader=loader)
|
2014-11-04 22:16:11 +01:00
|
|
|
roles.append(i)
|
|
|
|
|
|
|
|
return roles
|
|
|
|
|