Allow conditionals on PlaybookInclude statements

Fixes #11852
This commit is contained in:
James Cammarata 2015-08-04 15:24:28 -04:00
parent b6c3e5d797
commit 61e76fd707
2 changed files with 22 additions and 3 deletions

View file

@ -28,6 +28,12 @@ from ansible.playbook.play import Play
from ansible.playbook.playbook_include import PlaybookInclude
from ansible.plugins import get_all_plugin_loaders
try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
__all__ = ['Playbook']
@ -77,7 +83,10 @@ class Playbook:
if 'include' in entry:
pb = PlaybookInclude.load(entry, basedir=self._basedir, variable_manager=variable_manager, loader=self._loader)
self._entries.extend(pb._entries)
if pb is not None:
self._entries.extend(pb._entries)
else:
display.display("skipping playbook include '%s' due to conditional test failure" % entry.get('include', entry), color='cyan')
else:
entry_obj = Play.load(entry, variable_manager=variable_manager, loader=self._loader)
self._entries.append(entry_obj)

View file

@ -21,14 +21,16 @@ __metaclass__ = type
import os
from ansible.errors import AnsibleParserError
from ansible.parsing.splitter import split_args, parse_kv
from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject, AnsibleMapping
from ansible.playbook.attribute import FieldAttribute
from ansible.playbook.base import Base
from ansible.playbook.conditional import Conditional
from ansible.playbook.taggable import Taggable
from ansible.errors import AnsibleParserError
from ansible.template import Templar
class PlaybookInclude(Base, Taggable):
class PlaybookInclude(Base, Conditional, Taggable):
_name = FieldAttribute(isa='string')
_include = FieldAttribute(isa='string')
@ -52,6 +54,14 @@ class PlaybookInclude(Base, Taggable):
# playbook objects
new_obj = super(PlaybookInclude, self).load_data(ds, variable_manager, loader)
all_vars = dict()
if variable_manager:
all_vars = variable_manager.get_vars(loader=loader)
templar = Templar(loader=loader, variables=all_vars)
if not new_obj.evaluate_conditional(templar=templar, all_vars=all_vars):
return None
# then we use the object to load a Playbook
pb = Playbook(loader=loader)