From 61e76fd7070e89d597a4deebd78342f35aa6c116 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Tue, 4 Aug 2015 15:24:28 -0400 Subject: [PATCH] Allow conditionals on PlaybookInclude statements Fixes #11852 --- lib/ansible/playbook/__init__.py | 11 ++++++++++- lib/ansible/playbook/playbook_include.py | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/ansible/playbook/__init__.py b/lib/ansible/playbook/__init__.py index 6f7aee35ad9..ab3732b41fe 100644 --- a/lib/ansible/playbook/__init__.py +++ b/lib/ansible/playbook/__init__.py @@ -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) diff --git a/lib/ansible/playbook/playbook_include.py b/lib/ansible/playbook/playbook_include.py index f1629b4f150..5e83f0d3a33 100644 --- a/lib/ansible/playbook/playbook_include.py +++ b/lib/ansible/playbook/playbook_include.py @@ -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)