Coverage for lib/ansible/inventory/data.py : 56%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com> # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#############################################
except ImportError: from ansible.utils.display import Display display = Display()
""" Holds inventory data (host and group objects). Using it's methods should guarantee expected relationships and data. """
# the inventory object holds a list of groups
# provides 'groups' magic var, host object has group_names
# current localhost, implicit or explicit
# Always create the 'all' and 'ungrouped' groups,
self._groups_dict_cache = None data = { 'groups': self.groups, 'hosts': self.hosts, 'local': self.localhost, 'source': self.current_source, } return data
self._groups_dict_cache = {} self.hosts = data.get('hosts') self.groups = data.get('groups') self.localhost = data.get('local') self.current_source = data.get('source')
if self.localhost: new_host = self.localhost else: new_host = Host(pattern)
new_host.address = "127.0.0.1" new_host.implicit = True
# set localhost defaults py_interp = sys.executable if not py_interp: # sys.executable is not set in some cornercases. see issue #13585 py_interp = '/usr/bin/python' display.warning('Unable to determine python interpreter from sys.executable. Using /usr/bin/python default. ' 'You can correct this by setting ansible_python_interpreter for localhost') new_host.set_variable("ansible_python_interpreter", py_interp) new_host.set_variable("ansible_connection", 'local')
self.localhost = new_host
return new_host
''' Ensure inventory basic rules, run after updates '''
# set group vars from group_vars/ files and vars plugins
# ensure all groups inherit from 'all'
# get host vars from host_vars/ files and vars plugins
# clear ungrouped of any incorrectly stored by parser if set(mygroups).difference(set([self.groups['all'], self.groups['ungrouped']])): host.remove_group(self.groups['ungrouped'])
# add ungrouped hosts to ungrouped, except implicit self.add_child('ungrouped', host.name)
# special case for implicit hosts host.vars = combine_vars(self.groups['all'].get_vars(), host.vars)
# warn if overloading identifier as both group and host display.warning("Found both group and host with same name: %s" % conflict)
''' 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 and hostname in C.LOCALHOST: # might need to create implicit localhost matching_host = self._create_implicit_localhost(hostname)
return matching_host
''' adds a group to inventory if not there already '''
else: display.debug("group %s already in inventory" % group)
if group in self.groups: del self.groups[group] display.debug("Removed group %s from inventory" % group) self._groups_dict_cache = {}
for host in self.hosts: h = self.hosts[host] h.remove_group(group)
''' adds a host to inventory and possibly a group if not there already '''
else: raise AnsibleError("Could not find group %s in inventory" % group)
else: self.set_variable(host, 'inventory_file', None) self.set_variable(host, 'inventory_dir', None)
# set default localhost from inventory to avoid creating an implicit one. Last localhost defined 'wins'. if self.localhost is None: self.localhost = self.hosts[host] display.vvvv("Set default localhost to %s" % h) else: display.warning("A duplicate localhost-like entry was found (%s). First found localhost was %s" % (h, self.localhost.name)) else: h = self.hosts[host]
if host in self.hosts: del self.hosts[host]
for group in self.groups: g = self.groups[group] g.remove_host(host)
''' sets a varible for an inventory object '''
else: raise AnsibleError("Could not identify group or host named %s" % entity)
''' Add host or group to group '''
elif child in self.hosts: g.add_host(self.hosts[child]) else: raise AnsibleError("%s is not a known host nor group" % child) else: raise AnsibleError("%s is not a known group" % group)
""" We merge a 'magic' var 'groups' with group name keys and hostname list values into every host variable set. Cache for speed. """
|