From 40235d7b995f4d03c58d62234a60f7b774485401 Mon Sep 17 00:00:00 2001 From: jctanner Date: Tue, 21 Feb 2017 14:46:10 -0500 Subject: [PATCH] Skip fact gathering if the entire play was included via conditional and False (#21734) Addresses #21528 --- lib/ansible/executor/play_iterator.py | 3 +++ lib/ansible/playbook/play.py | 2 ++ lib/ansible/playbook/playbook_include.py | 6 ++++++ 3 files changed, 11 insertions(+) diff --git a/lib/ansible/executor/play_iterator.py b/lib/ansible/executor/play_iterator.py index e2f3dcfd138..ddf8f5959fe 100644 --- a/lib/ansible/executor/play_iterator.py +++ b/lib/ansible/executor/play_iterator.py @@ -184,6 +184,9 @@ class PlayIterator: if fact_path: setup_task.args['fact_path'] = fact_path setup_task.set_loader(self._play._loader) + # short circuit fact gathering if the entire playbook is conditional + if self._play._included_conditional is not None: + setup_task.when = self._play._included_conditional[:] setup_block.block = [setup_task] setup_block = setup_block.filter_tagged_tasks(play_context, all_vars) diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index 1f9c0bc4448..2b24dbcce9a 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -97,6 +97,7 @@ class Play(Base, Taggable, Become): def __init__(self): super(Play, self).__init__() + self._included_conditional = None self._included_path = None self._removed_hosts = [] self.ROLE_CACHE = {} @@ -330,5 +331,6 @@ class Play(Base, Taggable, Become): def copy(self): new_me = super(Play, self).copy() new_me.ROLE_CACHE = self.ROLE_CACHE.copy() + new_me._included_conditional = self._included_conditional new_me._included_path = self._included_path return new_me diff --git a/lib/ansible/playbook/playbook_include.py b/lib/ansible/playbook/playbook_include.py index dc0ee076d4e..1b13efe0eb4 100644 --- a/lib/ansible/playbook/playbook_include.py +++ b/lib/ansible/playbook/playbook_include.py @@ -49,6 +49,7 @@ class PlaybookInclude(Base, Conditional, Taggable): # import here to avoid a dependency loop from ansible.playbook import Playbook + from ansible.playbook.play import Play # first, we use the original parent method to correctly load the object # via the load_data/preprocess_data system we normally use for other @@ -73,6 +74,11 @@ class PlaybookInclude(Base, Conditional, Taggable): # finally, update each loaded playbook entry with any variables specified # on the included playbook and/or any tags which may have been set for entry in pb._entries: + + # conditional includes on a playbook need a marker to skip gathering + if new_obj.when and isinstance(entry, Play): + entry._included_conditional = new_obj.when[:] + temp_vars = entry.vars.copy() temp_vars.update(new_obj.vars) param_tags = temp_vars.pop('tags', None)