bypass get_hosts and use dictionary directly
removed redundant consistency fallback inventory reconciliation already takes care of this a priori removed unused imports
This commit is contained in:
parent
1e785d4117
commit
0571014f2f
2 changed files with 8 additions and 37 deletions
|
@ -20,7 +20,6 @@ from __future__ import (absolute_import, division, print_function)
|
|||
__metaclass__ = type
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
from ansible import constants as C
|
||||
|
@ -28,7 +27,6 @@ from ansible.errors import AnsibleError
|
|||
from ansible.inventory.group import Group
|
||||
from ansible.inventory.host import Host
|
||||
from ansible.module_utils.six import iteritems
|
||||
from ansible.module_utils._text import to_bytes
|
||||
from ansible.plugins.cache import FactCache
|
||||
from ansible.utils.vars import combine_vars
|
||||
from ansible.utils.path import basedir
|
||||
|
@ -104,23 +102,6 @@ class InventoryData(object):
|
|||
|
||||
return new_host
|
||||
|
||||
def _scan_groups_for_host(self, hostname, localhost=False):
|
||||
''' in case something did not update inventory correctly, fallback to group scan '''
|
||||
|
||||
found = None
|
||||
for group in self.groups.values():
|
||||
for host in group.get_hosts():
|
||||
if hostname == host.name:
|
||||
found = host
|
||||
break
|
||||
if found:
|
||||
break
|
||||
|
||||
if found:
|
||||
display.debug('Found host (%s) in groups but it was missing from main inventory' % hostname)
|
||||
|
||||
return found
|
||||
|
||||
def reconcile_inventory(self):
|
||||
''' Ensure inventory basic rules, run after updates '''
|
||||
|
||||
|
@ -170,27 +151,15 @@ class InventoryData(object):
|
|||
self._groups_dict_cache = {}
|
||||
|
||||
def get_host(self, hostname):
|
||||
''' fetch host object using name
|
||||
deal with implicit localhost
|
||||
and possible inconsistent inventory '''
|
||||
''' fetch host object using name deal with implicit localhost '''
|
||||
|
||||
matching_host = self.hosts.get(hostname, None)
|
||||
|
||||
# if host is not in hosts dict
|
||||
if matching_host is None:
|
||||
|
||||
if matching_host is None and hostname in C.LOCALHOST:
|
||||
# might need to create implicit localhost
|
||||
if hostname in C.LOCALHOST:
|
||||
matching_host = self._create_implicit_localhost(hostname)
|
||||
|
||||
# might be inconsistent inventory, search groups
|
||||
if matching_host is None:
|
||||
matching_host = self._scan_groups_for_host(hostname)
|
||||
|
||||
# if found/created update hosts dict
|
||||
if matching_host:
|
||||
self.hosts[hostname] = matching_host
|
||||
|
||||
return matching_host
|
||||
|
||||
def add_group(self, group):
|
||||
|
|
|
@ -69,6 +69,7 @@ class HostVars(collections.Mapping):
|
|||
self._inventory = inventory
|
||||
|
||||
def _find_host(self, host_name):
|
||||
# does not use inventory.hosts so it can create localhost on demand
|
||||
return self._inventory.get_host(host_name)
|
||||
|
||||
def raw_get(self, host_name):
|
||||
|
@ -100,18 +101,19 @@ class HostVars(collections.Mapping):
|
|||
self._variable_manager.set_host_facts(host, facts)
|
||||
|
||||
def __contains__(self, host_name):
|
||||
# does not use inventory.hosts so it can create localhost on demand
|
||||
return self._find_host(host_name) is not None
|
||||
|
||||
def __iter__(self):
|
||||
for host in self._inventory.get_hosts(ignore_limits=True, ignore_restrictions=True):
|
||||
for host in self._inventory.hosts:
|
||||
yield host.name
|
||||
|
||||
def __len__(self):
|
||||
return len(self._inventory.get_hosts(ignore_limits=True, ignore_restrictions=True))
|
||||
return len(self._inventory.hosts)
|
||||
|
||||
def __repr__(self):
|
||||
out = {}
|
||||
for host in self._inventory.get_hosts(ignore_limits=True, ignore_restrictions=True):
|
||||
for host in self._inventory.hosts:
|
||||
name = host.name
|
||||
out[name] = self.get(name)
|
||||
return repr(out)
|
||||
|
|
Loading…
Reference in a new issue