From 3740d7b028b23a1630f544e01cabf1fa3a50257c Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 19 Apr 2021 12:03:40 -0500 Subject: [PATCH] Only check if play.hosts is a template when the play hasn't been finalized (#73941) * Extend finalized logic to strategy * Add changelog fragment --- changelogs/fragments/73926-is-template-play-hosts.yml | 3 +++ lib/ansible/playbook/base.py | 4 ++++ lib/ansible/plugins/strategy/__init__.py | 2 +- lib/ansible/vars/manager.py | 2 +- 4 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/73926-is-template-play-hosts.yml diff --git a/changelogs/fragments/73926-is-template-play-hosts.yml b/changelogs/fragments/73926-is-template-play-hosts.yml new file mode 100644 index 00000000000..59c8e967785 --- /dev/null +++ b/changelogs/fragments/73926-is-template-play-hosts.yml @@ -0,0 +1,3 @@ +bugfixes: +- Variable Manager - Only check if ``play.hosts`` is a template when the play + hasn't been finalized (https://github.com/ansible/ansible/issues/73926) diff --git a/lib/ansible/playbook/base.py b/lib/ansible/playbook/base.py index 9fa84d14730..f32053a4daa 100644 --- a/lib/ansible/playbook/base.py +++ b/lib/ansible/playbook/base.py @@ -180,6 +180,10 @@ class FieldAttributeBase(with_metaclass(BaseMeta, object)): # and init vars, avoid using defaults in field declaration as it lives across plays self.vars = dict() + @property + def finalized(self): + return self._finalized + def dump_me(self, depth=0): ''' this is never called from production code, it is here to be used when debugging as a 'complex print' ''' if depth == 0: diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py index 46b3885c4b8..ae20aea53c7 100644 --- a/lib/ansible/plugins/strategy/__init__.py +++ b/lib/ansible/plugins/strategy/__init__.py @@ -251,7 +251,7 @@ class StrategyBase: if not refresh and all((self._hosts_cache, self._hosts_cache_all)): return - if Templar(None).is_template(play.hosts): + if not play.finalized and Templar(None).is_template(play.hosts): _pattern = 'all' else: _pattern = play.hosts or 'all' diff --git a/lib/ansible/vars/manager.py b/lib/ansible/vars/manager.py index 8b2a151dde9..0715f0af94a 100644 --- a/lib/ansible/vars/manager.py +++ b/lib/ansible/vars/manager.py @@ -487,7 +487,7 @@ class VariableManager: variables['groups'] = self._inventory.get_groups_dict() if play: templar = Templar(loader=self._loader) - if templar.is_template(play.hosts): + if not play.finalized and templar.is_template(play.hosts): pattern = 'all' else: pattern = play.hosts or 'all'