Skip to content

Commit

Permalink
There can be only one localhost
Browse files Browse the repository at this point in the history
The changes to exclude implicit localhosts from group patterns exposed
the bug that we sometimes create multiple implicit localhosts, which
caused some bugs with things like includes, where the host was used as
an entry into a dict, so having multiple meant that the incorrect host
(with a different uuid) was found and includes were not executed for
implicit localhosts.
  • Loading branch information
jimi-c committed Jun 8, 2016
1 parent fbec2d9 commit 5f1bbb4
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions lib/ansible/inventory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(self, loader, variable_manager, host_list=C.DEFAULT_HOST_LIST):
self.host_list = host_list
self._loader = loader
self._variable_manager = variable_manager
self.localhost = None

# caching to avoid repeated calculations, particularly with
# external inventory scripts.
Expand Down Expand Up @@ -89,6 +90,8 @@ def __init__(self, loader, variable_manager, host_list=C.DEFAULT_HOST_LIST):
self.clear_pattern_cache()

self.parse_inventory(host_list)
if self.localhost is None:
self.localhost = self._create_implicit_localhost()

def serialize(self):
data = dict()
Expand Down Expand Up @@ -125,7 +128,13 @@ def parse_inventory(self, host_list):
display.vvv("Unable to parse address from hostname, leaving unchanged: %s" % to_unicode(e))
host = h
port = None
all.add_host(Host(host, port))
new_host = Host(host, port)
all.add_host(new_host)
if new_host.name in C.LOCALHOST:
if self.localhost is None:
self.localhost = new_host
else:
display.warning("A duplicate localhost-like entry was found (%s). First found localhost was %s" % (new_host.name, self.localhost.name))
elif self._loader.path_exists(host_list):
#TODO: switch this to a plugin loader and a 'condition' per plugin on which it should be tried, restoring 'inventory pllugins'
if self.is_directory(host_list):
Expand Down Expand Up @@ -461,12 +470,9 @@ def __append_host_to_results(host):
for host in matching_hosts:
__append_host_to_results(host)

if pattern in C.LOCALHOST and len(results) == 0:
new_host = self._create_implicit_localhost(pattern)
results.append(new_host)
return results

def _create_implicit_localhost(self, pattern):
def _create_implicit_localhost(self, pattern='localhost'):
new_host = Host(pattern)
new_host.address = "127.0.0.1"
new_host.implicit = True
Expand Down Expand Up @@ -495,17 +501,11 @@ def get_groups(self):
def get_host(self, hostname):
if hostname not in self._hosts_cache:
self._hosts_cache[hostname] = self._get_host(hostname)
if hostname in C.LOCALHOST:
for host in C.LOCALHOST.difference((hostname,)):
self._hosts_cache[host] = self._hosts_cache[hostname]
return self._hosts_cache[hostname]

def _get_host(self, hostname):
if hostname in C.LOCALHOST:
for host in self.get_group('all').get_hosts():
if host.name in C.LOCALHOST:
return host
return self._create_implicit_localhost(hostname)
if hostname in C.LOCALHOST and self.localhost:
self.localhost
matching_host = None
for group in self.groups.values():
for host in group.get_hosts():
Expand Down

0 comments on commit 5f1bbb4

Please sign in to comment.