From bb564075b55a748731f8ff8f57727b0dfd906ab2 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Wed, 20 Jan 2016 01:32:45 -0500 Subject: [PATCH] Forward conditionals onto included plays when conditional eval errors When using a playbook-level include, we now catch any errors raised during the conditional evaluation step and set a flag to indicate we need to pass those conditionals on to the included play (most likely because they contain inventory variables for evaluation). Fixes #14003 --- lib/ansible/playbook/playbook_include.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/ansible/playbook/playbook_include.py b/lib/ansible/playbook/playbook_include.py index d9af2ba5237..2ce076edb11 100644 --- a/lib/ansible/playbook/playbook_include.py +++ b/lib/ansible/playbook/playbook_include.py @@ -22,7 +22,7 @@ __metaclass__ = type import os from ansible.compat.six import iteritems -from ansible.errors import AnsibleParserError +from ansible.errors import AnsibleParserError, AnsibleError from ansible.parsing.splitter import split_args, parse_kv from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject, AnsibleMapping from ansible.playbook.attribute import FieldAttribute @@ -60,8 +60,15 @@ class PlaybookInclude(Base, Conditional, Taggable): all_vars.update(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 + + try: + forward_conditional = False + if not new_obj.evaluate_conditional(templar=templar, all_vars=all_vars): + return None + except AnsibleError: + # conditional evaluation raised an error, so we set a flag to indicate + # we need to forward the conditionals on to the included play(s) + forward_conditional = True # then we use the object to load a Playbook pb = Playbook(loader=loader) @@ -85,6 +92,13 @@ class PlaybookInclude(Base, Conditional, Taggable): if entry._included_path is None: entry._included_path = os.path.dirname(file_name) + # Check to see if we need to forward the conditionals on to the included + # plays. If so, we can take a shortcut here and simply prepend them to + # those attached to each block (if any) + if forward_conditional: + for task_block in entry.tasks: + task_block.when = self.when[:] + task_block.when + return pb def preprocess_data(self, ds):