mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-26 22:51:23 -07:00
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
|
__metaclass__ = type
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import re
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from ansible import constants as C
|
from ansible import constants as C
|
||||||
|
@ -28,7 +27,6 @@ from ansible.errors import AnsibleError
|
||||||
from ansible.inventory.group import Group
|
from ansible.inventory.group import Group
|
||||||
from ansible.inventory.host import Host
|
from ansible.inventory.host import Host
|
||||||
from ansible.module_utils.six import iteritems
|
from ansible.module_utils.six import iteritems
|
||||||
from ansible.module_utils._text import to_bytes
|
|
||||||
from ansible.plugins.cache import FactCache
|
from ansible.plugins.cache import FactCache
|
||||||
from ansible.utils.vars import combine_vars
|
from ansible.utils.vars import combine_vars
|
||||||
from ansible.utils.path import basedir
|
from ansible.utils.path import basedir
|
||||||
|
@ -104,23 +102,6 @@ class InventoryData(object):
|
||||||
|
|
||||||
return new_host
|
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):
|
def reconcile_inventory(self):
|
||||||
''' Ensure inventory basic rules, run after updates '''
|
''' Ensure inventory basic rules, run after updates '''
|
||||||
|
|
||||||
|
@ -170,27 +151,15 @@ class InventoryData(object):
|
||||||
self._groups_dict_cache = {}
|
self._groups_dict_cache = {}
|
||||||
|
|
||||||
def get_host(self, hostname):
|
def get_host(self, hostname):
|
||||||
''' fetch host object using name
|
''' fetch host object using name deal with implicit localhost '''
|
||||||
deal with implicit localhost
|
|
||||||
and possible inconsistent inventory '''
|
|
||||||
|
|
||||||
matching_host = self.hosts.get(hostname, None)
|
matching_host = self.hosts.get(hostname, None)
|
||||||
|
|
||||||
# if host is not in hosts dict
|
# 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
|
# might need to create implicit localhost
|
||||||
if hostname in C.LOCALHOST:
|
|
||||||
matching_host = self._create_implicit_localhost(hostname)
|
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
|
return matching_host
|
||||||
|
|
||||||
def add_group(self, group):
|
def add_group(self, group):
|
||||||
|
|
|
@ -69,6 +69,7 @@ class HostVars(collections.Mapping):
|
||||||
self._inventory = inventory
|
self._inventory = inventory
|
||||||
|
|
||||||
def _find_host(self, host_name):
|
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)
|
return self._inventory.get_host(host_name)
|
||||||
|
|
||||||
def raw_get(self, host_name):
|
def raw_get(self, host_name):
|
||||||
|
@ -100,18 +101,19 @@ class HostVars(collections.Mapping):
|
||||||
self._variable_manager.set_host_facts(host, facts)
|
self._variable_manager.set_host_facts(host, facts)
|
||||||
|
|
||||||
def __contains__(self, host_name):
|
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
|
return self._find_host(host_name) is not None
|
||||||
|
|
||||||
def __iter__(self):
|
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
|
yield host.name
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self._inventory.get_hosts(ignore_limits=True, ignore_restrictions=True))
|
return len(self._inventory.hosts)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
out = {}
|
out = {}
|
||||||
for host in self._inventory.get_hosts(ignore_limits=True, ignore_restrictions=True):
|
for host in self._inventory.hosts:
|
||||||
name = host.name
|
name = host.name
|
||||||
out[name] = self.get(name)
|
out[name] = self.get(name)
|
||||||
return repr(out)
|
return repr(out)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue