From f955d9db6a01c6d5c2cd761b3bdfd76ae20de86b Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Thu, 30 Jan 2014 12:45:19 -0600 Subject: [PATCH] Handle implicit localhost when using '-i host,' syntax Fixes #5820 --- lib/ansible/inventory/__init__.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index b1755e1d75f..ed945ce50c2 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -249,6 +249,17 @@ class Inventory(object): else: return [ hosts[left] ] + def _create_implicit_localhost(self, pattern): + new_host = Host(pattern) + new_host.set_variable("ansible_python_interpreter", sys.executable) + new_host.set_variable("ansible_connection", "local") + ungrouped = self.get_group("ungrouped") + if ungrouped is None: + self.add_group(Group('ungrouped')) + ungrouped = self.get_group('ungrouped') + ungrouped.add_host(new_host) + return new_host + def _hosts_in_unenumerated_pattern(self, pattern): """ Get all host names matching the pattern """ @@ -268,15 +279,7 @@ class Inventory(object): hostnames.add(host.name) if pattern in ["localhost", "127.0.0.1"] and len(results) == 0: - new_host = Host(pattern) - new_host.set_variable("ansible_python_interpreter", sys.executable) - new_host.set_variable("ansible_connection", "local") - ungrouped = self.get_group("ungrouped") - if ungrouped is None: - self.add_group(Group('ungrouped')) - ungrouped = self.get_group('ungrouped') - - ungrouped.add_host(new_host) + new_host = self._create_implicit_localhost(pattern) results.append(new_host) return results @@ -319,6 +322,7 @@ class Inventory(object): for host in self.get_group('all').get_hosts(): if host.name in ['localhost', '127.0.0.1']: return host + return self._create_implicit_localhost(hostname) else: for group in self.groups: for host in group.get_hosts():