mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
There can be only one localhost
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.
This commit is contained in:
parent
fbec2d9692
commit
5f1bbb4fcd
1 changed files with 13 additions and 13 deletions
|
@ -58,6 +58,7 @@ class Inventory(object):
|
||||||
self.host_list = host_list
|
self.host_list = host_list
|
||||||
self._loader = loader
|
self._loader = loader
|
||||||
self._variable_manager = variable_manager
|
self._variable_manager = variable_manager
|
||||||
|
self.localhost = None
|
||||||
|
|
||||||
# caching to avoid repeated calculations, particularly with
|
# caching to avoid repeated calculations, particularly with
|
||||||
# external inventory scripts.
|
# external inventory scripts.
|
||||||
|
@ -89,6 +90,8 @@ class Inventory(object):
|
||||||
self.clear_pattern_cache()
|
self.clear_pattern_cache()
|
||||||
|
|
||||||
self.parse_inventory(host_list)
|
self.parse_inventory(host_list)
|
||||||
|
if self.localhost is None:
|
||||||
|
self.localhost = self._create_implicit_localhost()
|
||||||
|
|
||||||
def serialize(self):
|
def serialize(self):
|
||||||
data = dict()
|
data = dict()
|
||||||
|
@ -125,7 +128,13 @@ class Inventory(object):
|
||||||
display.vvv("Unable to parse address from hostname, leaving unchanged: %s" % to_unicode(e))
|
display.vvv("Unable to parse address from hostname, leaving unchanged: %s" % to_unicode(e))
|
||||||
host = h
|
host = h
|
||||||
port = None
|
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):
|
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'
|
#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):
|
if self.is_directory(host_list):
|
||||||
|
@ -461,12 +470,9 @@ class Inventory(object):
|
||||||
for host in matching_hosts:
|
for host in matching_hosts:
|
||||||
__append_host_to_results(host)
|
__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
|
return results
|
||||||
|
|
||||||
def _create_implicit_localhost(self, pattern):
|
def _create_implicit_localhost(self, pattern='localhost'):
|
||||||
new_host = Host(pattern)
|
new_host = Host(pattern)
|
||||||
new_host.address = "127.0.0.1"
|
new_host.address = "127.0.0.1"
|
||||||
new_host.implicit = True
|
new_host.implicit = True
|
||||||
|
@ -495,17 +501,11 @@ class Inventory(object):
|
||||||
def get_host(self, hostname):
|
def get_host(self, hostname):
|
||||||
if hostname not in self._hosts_cache:
|
if hostname not in self._hosts_cache:
|
||||||
self._hosts_cache[hostname] = self._get_host(hostname)
|
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]
|
return self._hosts_cache[hostname]
|
||||||
|
|
||||||
def _get_host(self, hostname):
|
def _get_host(self, hostname):
|
||||||
if hostname in C.LOCALHOST:
|
if hostname in C.LOCALHOST and self.localhost:
|
||||||
for host in self.get_group('all').get_hosts():
|
self.localhost
|
||||||
if host.name in C.LOCALHOST:
|
|
||||||
return host
|
|
||||||
return self._create_implicit_localhost(hostname)
|
|
||||||
matching_host = None
|
matching_host = None
|
||||||
for group in self.groups.values():
|
for group in self.groups.values():
|
||||||
for host in group.get_hosts():
|
for host in group.get_hosts():
|
||||||
|
|
Loading…
Add table
Reference in a new issue