From a108f0fcd3585ef39f0382a0108bee902d829ba4 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 24 Apr 2017 15:19:52 -0400 Subject: [PATCH] avoid hosts 'pre templated warnings' (#23878) * avoid hosts 'pre templated warnings' fixed is_template failing on undefined fixes #23057 * py3 compat --- lib/ansible/template/__init__.py | 4 ++-- lib/ansible/vars/__init__.py | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index ea2c59c1684..0c6ff1c9fe5 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -503,8 +503,8 @@ class Templar: ''' lets us know if data has a template''' if isinstance(data, string_types): try: - new = self.do_template(data) - except UndefinedError: + new = self.do_template(data, fail_on_undefined=True) + except (AnsibleUndefinedVariable, UndefinedError): return True except: return False diff --git a/lib/ansible/vars/__init__.py b/lib/ansible/vars/__init__.py index 74f41d9cafa..57c8341559b 100644 --- a/lib/ansible/vars/__init__.py +++ b/lib/ansible/vars/__init__.py @@ -429,8 +429,13 @@ class VariableManager: variables['inventory_dir'] = self._inventory.basedir() variables['inventory_file'] = self._inventory.src() if play: + templar = Templar(loader=loader) + if templar.is_template(play.hosts): + pattern = 'all' + else: + pattern = play.hosts or 'all' # add the list of hosts in the play, as adjusted for limit/filters - variables['ansible_play_hosts_all'] = [x.name for x in self._inventory.get_hosts(pattern=play.hosts or 'all', ignore_restrictions=True)] + variables['ansible_play_hosts_all'] = [x.name for x in self._inventory.get_hosts(pattern=pattern, ignore_restrictions=True)] variables['ansible_play_hosts'] = [x for x in variables['ansible_play_hosts_all'] if x not in play._removed_hosts] variables['ansible_play_batch'] = [x.name for x in self._inventory.get_hosts() if x.name not in play._removed_hosts]