2014-11-01 19:34:14 +00:00
|
|
|
# (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/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from collections import defaultdict
|
2015-05-08 20:18:19 +00:00
|
|
|
from collections import MutableMapping
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2015-07-21 18:51:53 +00:00
|
|
|
from jinja2.exceptions import UndefinedError
|
|
|
|
|
2015-01-23 03:45:25 +00:00
|
|
|
try:
|
|
|
|
from hashlib import sha1
|
|
|
|
except ImportError:
|
|
|
|
from sha import sha as sha1
|
|
|
|
|
2015-01-27 05:41:02 +00:00
|
|
|
from ansible import constants as C
|
2015-07-10 07:22:37 +00:00
|
|
|
from ansible.cli import CLI
|
2015-09-01 18:20:16 +00:00
|
|
|
from ansible.errors import AnsibleError
|
2014-11-14 22:14:08 +00:00
|
|
|
from ansible.parsing import DataLoader
|
2014-11-01 19:34:14 +00:00
|
|
|
from ansible.plugins.cache import FactCache
|
2014-11-14 22:14:08 +00:00
|
|
|
from ansible.template import Templar
|
|
|
|
from ansible.utils.debug import debug
|
2015-09-01 18:20:16 +00:00
|
|
|
from ansible.utils.vars import combine_vars
|
2015-01-23 03:45:25 +00:00
|
|
|
from ansible.vars.hostvars import HostVars
|
2015-09-03 22:08:34 +00:00
|
|
|
from ansible.vars.unsafe_proxy import UnsafeProxy
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
CACHED_VARS = dict()
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2015-09-09 21:52:44 +00:00
|
|
|
def preprocess_vars(a):
|
|
|
|
'''
|
|
|
|
Ensures that vars contained in the parameter passed in are
|
|
|
|
returned as a list of dictionaries, to ensure for instance
|
|
|
|
that vars loaded from a file conform to an expected state.
|
|
|
|
'''
|
|
|
|
|
|
|
|
if a is None:
|
|
|
|
return None
|
|
|
|
elif not isinstance(a, list):
|
|
|
|
data = [ a ]
|
|
|
|
else:
|
|
|
|
data = a
|
|
|
|
|
|
|
|
for item in data:
|
|
|
|
if not isinstance(item, MutableMapping):
|
|
|
|
raise AnsibleError("variable files must contain either a dictionary of variables, or a list of dictionaries. Got: %s (%s)" % (a, type(a)))
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2014-11-01 19:34:14 +00:00
|
|
|
class VariableManager:
|
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
def __init__(self):
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2015-09-09 16:21:07 +00:00
|
|
|
self._fact_cache = FactCache()
|
|
|
|
self._nonpersistent_fact_cache = defaultdict(dict)
|
|
|
|
self._vars_cache = defaultdict(dict)
|
|
|
|
self._extra_vars = defaultdict(dict)
|
|
|
|
self._host_vars_files = defaultdict(dict)
|
2014-11-01 19:34:14 +00:00
|
|
|
self._group_vars_files = defaultdict(dict)
|
2015-09-09 16:21:07 +00:00
|
|
|
self._inventory = None
|
|
|
|
self._omit_token = '__omit_place_holder__%s' % sha1(os.urandom(64)).hexdigest()
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
def _get_cache_entry(self, play=None, host=None, task=None):
|
|
|
|
play_id = "NONE"
|
|
|
|
if play:
|
|
|
|
play_id = play._uuid
|
|
|
|
|
|
|
|
host_id = "NONE"
|
|
|
|
if host:
|
|
|
|
host_id = host.get_name()
|
|
|
|
|
|
|
|
task_id = "NONE"
|
|
|
|
if task:
|
|
|
|
task_id = task._uuid
|
|
|
|
|
|
|
|
return "PLAY:%s;HOST:%s;TASK:%s" % (play_id, host_id, task_id)
|
2014-11-01 19:34:14 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def extra_vars(self):
|
|
|
|
''' ensures a clean copy of the extra_vars are made '''
|
|
|
|
return self._extra_vars.copy()
|
|
|
|
|
2015-05-15 01:10:31 +00:00
|
|
|
@extra_vars.setter
|
|
|
|
def extra_vars(self, value):
|
2014-11-01 19:34:14 +00:00
|
|
|
''' ensures a clean copy of the extra_vars are used to set the value '''
|
2015-05-08 20:18:19 +00:00
|
|
|
assert isinstance(value, MutableMapping)
|
2014-11-01 19:34:14 +00:00
|
|
|
self._extra_vars = value.copy()
|
|
|
|
|
2015-01-23 03:45:25 +00:00
|
|
|
def set_inventory(self, inventory):
|
|
|
|
self._inventory = inventory
|
|
|
|
|
2015-08-21 14:59:32 +00:00
|
|
|
def _preprocess_vars(self, a):
|
|
|
|
'''
|
|
|
|
Ensures that vars contained in the parameter passed in are
|
|
|
|
returned as a list of dictionaries, to ensure for instance
|
|
|
|
that vars loaded from a file conform to an expected state.
|
|
|
|
'''
|
|
|
|
|
|
|
|
if a is None:
|
|
|
|
return None
|
|
|
|
elif not isinstance(a, list):
|
|
|
|
data = [ a ]
|
|
|
|
else:
|
|
|
|
data = a
|
|
|
|
|
|
|
|
for item in data:
|
|
|
|
if not isinstance(item, MutableMapping):
|
|
|
|
raise AnsibleError("variable files must contain either a dictionary of variables, or a list of dictionaries. Got: %s (%s)" % (a, type(a)))
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2015-08-21 14:59:32 +00:00
|
|
|
def get_vars(self, loader, play=None, host=None, task=None, include_hostvars=True, use_cache=True):
|
2014-11-01 19:34:14 +00:00
|
|
|
'''
|
|
|
|
Returns the variables, with optional "context" given via the parameters
|
|
|
|
for the play, host, and task (which could possibly result in different
|
|
|
|
sets of variables being returned due to the additional context).
|
|
|
|
|
|
|
|
The order of precedence is:
|
|
|
|
- play->roles->get_default_vars (if there is a play context)
|
|
|
|
- group_vars_files[host] (if there is a host context)
|
|
|
|
- host_vars_files[host] (if there is a host context)
|
|
|
|
- host->get_vars (if there is a host context)
|
|
|
|
- fact_cache[host] (if there is a host context)
|
|
|
|
- play vars (if there is a play context)
|
|
|
|
- play vars_files (if there's no host context, ignore
|
|
|
|
file names that cannot be templated)
|
|
|
|
- task->get_vars (if there is a task context)
|
2015-09-04 20:41:38 +00:00
|
|
|
- vars_cache[host] (if there is a host context)
|
2014-11-01 19:34:14 +00:00
|
|
|
- extra vars
|
|
|
|
'''
|
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
debug("in VariableManager get_vars()")
|
|
|
|
cache_entry = self._get_cache_entry(play=play, host=host, task=task)
|
2015-05-15 01:10:31 +00:00
|
|
|
if cache_entry in CACHED_VARS and use_cache:
|
2014-11-14 22:14:08 +00:00
|
|
|
debug("vars are cached, returning them now")
|
|
|
|
return CACHED_VARS[cache_entry]
|
|
|
|
|
|
|
|
all_vars = defaultdict(dict)
|
2014-11-01 19:34:14 +00:00
|
|
|
|
|
|
|
if play:
|
|
|
|
# first we compile any vars specified in defaults/main.yml
|
|
|
|
# for all roles within the specified play
|
|
|
|
for role in play.get_roles():
|
2015-09-01 18:20:16 +00:00
|
|
|
all_vars = combine_vars(all_vars, role.get_default_vars())
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2015-09-01 15:27:00 +00:00
|
|
|
# if we have a task in this context, and that task has a role, make
|
|
|
|
# sure it sees its defaults above any other roles, as we previously
|
|
|
|
# (v1) made sure each task had a copy of its roles default vars
|
|
|
|
if task and task._role is not None:
|
2015-09-01 18:20:16 +00:00
|
|
|
all_vars = combine_vars(all_vars, task._role.get_default_vars())
|
2015-09-01 15:27:00 +00:00
|
|
|
|
2014-11-01 19:34:14 +00:00
|
|
|
if host:
|
|
|
|
# next, if a host is specified, we load any vars from group_vars
|
|
|
|
# files and then any vars from host_vars files which may apply to
|
|
|
|
# this host or the groups it belongs to
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-09-04 20:41:38 +00:00
|
|
|
# we merge in vars from groups specified in the inventory (INI or script)
|
|
|
|
all_vars = combine_vars(all_vars, host.get_group_vars())
|
|
|
|
|
|
|
|
# then we merge in the special 'all' group_vars first, if they exist
|
2014-11-14 22:14:08 +00:00
|
|
|
if 'all' in self._group_vars_files:
|
2015-09-09 21:52:44 +00:00
|
|
|
data = preprocess_vars(self._group_vars_files['all'])
|
2015-08-21 14:59:32 +00:00
|
|
|
for item in data:
|
2015-09-01 18:20:16 +00:00
|
|
|
all_vars = combine_vars(all_vars, item)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2014-11-01 19:34:14 +00:00
|
|
|
for group in host.get_groups():
|
2015-03-25 18:51:40 +00:00
|
|
|
if group.name in self._group_vars_files and group.name != 'all':
|
2015-09-04 20:41:38 +00:00
|
|
|
for data in self._group_vars_files[group.name]:
|
2015-09-09 21:52:44 +00:00
|
|
|
data = preprocess_vars(data)
|
2015-09-04 20:41:38 +00:00
|
|
|
for item in data:
|
|
|
|
all_vars = combine_vars(all_vars, item)
|
|
|
|
|
|
|
|
# then we merge in vars from the host specified in the inventory (INI or script)
|
|
|
|
all_vars = combine_vars(all_vars, host.get_vars())
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2015-09-04 20:41:38 +00:00
|
|
|
# then we merge in the host_vars/<hostname> file, if it exists
|
2014-11-01 19:34:14 +00:00
|
|
|
host_name = host.get_name()
|
|
|
|
if host_name in self._host_vars_files:
|
2015-09-04 20:41:38 +00:00
|
|
|
for data in self._host_vars_files[host_name]:
|
2015-09-09 21:52:44 +00:00
|
|
|
data = preprocess_vars(data)
|
2015-09-04 20:41:38 +00:00
|
|
|
for item in data:
|
|
|
|
all_vars = combine_vars(all_vars, item)
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2015-09-09 16:21:07 +00:00
|
|
|
# finally, the facts caches for this host, if it exists
|
2015-07-13 15:06:03 +00:00
|
|
|
try:
|
2015-09-03 22:08:34 +00:00
|
|
|
host_facts = self._fact_cache.get(host.name, dict())
|
|
|
|
for k in host_facts.keys():
|
2015-09-05 21:30:43 +00:00
|
|
|
if host_facts[k] is not None and not isinstance(host_facts[k], UnsafeProxy):
|
2015-09-08 06:50:07 +00:00
|
|
|
host_facts[k] = UnsafeProxy(host_facts[k])
|
2015-09-03 22:08:34 +00:00
|
|
|
all_vars = combine_vars(all_vars, host_facts)
|
2015-07-13 15:06:03 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2014-11-01 19:34:14 +00:00
|
|
|
|
|
|
|
if play:
|
2015-09-01 18:20:16 +00:00
|
|
|
all_vars = combine_vars(all_vars, play.get_vars())
|
2015-08-13 02:39:06 +00:00
|
|
|
|
2015-07-13 19:18:05 +00:00
|
|
|
for vars_file_item in play.get_vars_files():
|
2014-11-14 22:14:08 +00:00
|
|
|
try:
|
2015-07-21 18:51:53 +00:00
|
|
|
# create a set of temporary vars here, which incorporate the
|
|
|
|
# extra vars so we can properly template the vars_files entries
|
2015-09-01 18:20:16 +00:00
|
|
|
temp_vars = combine_vars(all_vars, self._extra_vars)
|
2015-07-21 18:51:53 +00:00
|
|
|
templar = Templar(loader=loader, variables=temp_vars)
|
|
|
|
|
2015-07-13 19:18:05 +00:00
|
|
|
# we assume each item in the list is itself a list, as we
|
|
|
|
# support "conditional includes" for vars_files, which mimics
|
|
|
|
# the with_first_found mechanism.
|
|
|
|
vars_file_list = templar.template(vars_file_item)
|
|
|
|
if not isinstance(vars_file_list, list):
|
|
|
|
vars_file_list = [ vars_file_list ]
|
|
|
|
|
|
|
|
# now we iterate through the (potential) files, and break out
|
|
|
|
# as soon as we read one from the list. If none are found, we
|
|
|
|
# raise an error, which is silently ignored at this point.
|
|
|
|
for vars_file in vars_file_list:
|
2015-09-09 21:52:44 +00:00
|
|
|
data = preprocess_vars(loader.load_from_file(vars_file))
|
2015-07-13 19:18:05 +00:00
|
|
|
if data is not None:
|
2015-08-21 14:59:32 +00:00
|
|
|
for item in data:
|
2015-09-01 18:20:16 +00:00
|
|
|
all_vars = combine_vars(all_vars, item)
|
2015-07-13 19:18:05 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise AnsibleError("vars file %s was not found" % vars_file_item)
|
2015-09-01 18:20:16 +00:00
|
|
|
except UndefinedError:
|
2015-07-21 18:51:53 +00:00
|
|
|
continue
|
2015-07-01 15:32:44 +00:00
|
|
|
|
|
|
|
if not C.DEFAULT_PRIVATE_ROLE_VARS:
|
|
|
|
for role in play.get_roles():
|
2015-09-01 18:20:16 +00:00
|
|
|
all_vars = combine_vars(all_vars, role.get_vars())
|
2014-11-01 19:34:14 +00:00
|
|
|
|
|
|
|
if task:
|
2014-11-14 22:14:08 +00:00
|
|
|
if task._role:
|
2015-09-01 18:20:16 +00:00
|
|
|
all_vars = combine_vars(all_vars, task._role.get_vars())
|
|
|
|
all_vars = combine_vars(all_vars, task.get_vars())
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2015-08-21 14:59:32 +00:00
|
|
|
if host:
|
2015-09-01 18:20:16 +00:00
|
|
|
all_vars = combine_vars(all_vars, self._vars_cache.get(host.get_name(), dict()))
|
2015-09-11 16:24:44 +00:00
|
|
|
all_vars = combine_vars(all_vars, self._nonpersistent_fact_cache.get(host.name, dict()))
|
2015-08-21 14:59:32 +00:00
|
|
|
|
2015-09-01 18:20:16 +00:00
|
|
|
all_vars = combine_vars(all_vars, self._extra_vars)
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2015-01-23 03:45:25 +00:00
|
|
|
# FIXME: make sure all special vars are here
|
|
|
|
# Finally, we create special vars
|
|
|
|
|
2015-06-23 18:29:39 +00:00
|
|
|
all_vars['playbook_dir'] = loader.get_basedir()
|
|
|
|
|
2015-05-11 16:22:41 +00:00
|
|
|
if host:
|
|
|
|
all_vars['groups'] = [group.name for group in host.get_groups()]
|
|
|
|
|
|
|
|
if self._inventory is not None:
|
2015-06-17 13:48:19 +00:00
|
|
|
all_vars['groups'] = self._inventory.groups_list()
|
2015-08-21 14:59:32 +00:00
|
|
|
if include_hostvars:
|
|
|
|
hostvars = HostVars(vars_manager=self, play=play, inventory=self._inventory, loader=loader)
|
|
|
|
all_vars['hostvars'] = hostvars
|
2015-01-23 03:45:25 +00:00
|
|
|
|
2015-05-11 19:04:17 +00:00
|
|
|
if task:
|
|
|
|
if task._role:
|
|
|
|
all_vars['role_path'] = task._role._role_path
|
|
|
|
|
2015-01-29 22:43:50 +00:00
|
|
|
if self._inventory is not None:
|
|
|
|
all_vars['inventory_dir'] = self._inventory.basedir()
|
2015-06-23 01:03:55 +00:00
|
|
|
if play:
|
|
|
|
# add the list of hosts in the play, as adjusted for limit/filters
|
2015-08-19 04:10:31 +00:00
|
|
|
# DEPRECATED: play_hosts should be deprecated in favor of ansible_play_hosts,
|
|
|
|
# however this would take work in the templating engine, so for now
|
|
|
|
# we'll add both so we can give users something transitional to use
|
2015-06-23 01:03:55 +00:00
|
|
|
host_list = [x.name for x in self._inventory.get_hosts()]
|
|
|
|
all_vars['play_hosts'] = host_list
|
|
|
|
all_vars['ansible_play_hosts'] = host_list
|
|
|
|
|
2015-01-29 22:43:50 +00:00
|
|
|
|
2015-01-23 03:45:25 +00:00
|
|
|
# the 'omit' value alows params to be left out if the variable they are based on is undefined
|
|
|
|
all_vars['omit'] = self._omit_token
|
2015-07-07 20:48:19 +00:00
|
|
|
|
2015-07-10 07:22:37 +00:00
|
|
|
all_vars['ansible_version'] = CLI.version_info(gitinfo=False)
|
|
|
|
|
2015-08-23 16:50:15 +00:00
|
|
|
if 'hostvars' in all_vars and host:
|
|
|
|
all_vars['vars'] = all_vars['hostvars'][host.get_name()]
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-05-15 01:10:31 +00:00
|
|
|
#CACHED_VARS[cache_entry] = all_vars
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
debug("done with get_vars()")
|
|
|
|
return all_vars
|
2014-11-01 19:34:14 +00:00
|
|
|
|
|
|
|
def _get_inventory_basename(self, path):
|
|
|
|
'''
|
2015-09-09 15:55:40 +00:00
|
|
|
Returns the basename minus the extension of the given path, so the
|
2014-11-01 19:34:14 +00:00
|
|
|
bare filename can be matched against host/group names later
|
|
|
|
'''
|
|
|
|
|
|
|
|
(name, ext) = os.path.splitext(os.path.basename(path))
|
2015-03-25 18:51:40 +00:00
|
|
|
if ext not in ('.yml', '.yaml'):
|
2014-11-14 22:14:08 +00:00
|
|
|
return os.path.basename(path)
|
|
|
|
else:
|
|
|
|
return name
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
def _load_inventory_file(self, path, loader):
|
2014-11-01 19:34:14 +00:00
|
|
|
'''
|
|
|
|
helper function, which loads the file and gets the
|
|
|
|
basename of the file without the extension
|
|
|
|
'''
|
|
|
|
|
2015-03-25 18:51:40 +00:00
|
|
|
if loader.is_directory(path):
|
2015-01-28 20:06:10 +00:00
|
|
|
data = dict()
|
|
|
|
|
|
|
|
try:
|
2015-03-25 18:51:40 +00:00
|
|
|
names = loader.list_directory(path)
|
2015-04-13 16:35:20 +00:00
|
|
|
except os.error as err:
|
2015-01-28 20:06:10 +00:00
|
|
|
raise AnsibleError("This folder cannot be listed: %s: %s." % (path, err.strerror))
|
|
|
|
|
|
|
|
# evaluate files in a stable order rather than whatever
|
|
|
|
# order the filesystem lists them.
|
|
|
|
names.sort()
|
|
|
|
|
|
|
|
# do not parse hidden files or dirs, e.g. .svn/
|
|
|
|
paths = [os.path.join(path, name) for name in names if not name.startswith('.')]
|
|
|
|
for p in paths:
|
|
|
|
_found, results = self._load_inventory_file(path=p, loader=loader)
|
2015-07-13 22:14:13 +00:00
|
|
|
if results is not None:
|
2015-09-01 18:20:16 +00:00
|
|
|
data = combine_vars(data, results)
|
2015-01-28 20:06:10 +00:00
|
|
|
|
|
|
|
else:
|
2015-06-16 15:55:26 +00:00
|
|
|
file_name, ext = os.path.splitext(path)
|
|
|
|
data = None
|
2015-09-08 07:20:09 +00:00
|
|
|
if not ext or ext not in C.YAML_FILENAME_EXTENSIONS:
|
|
|
|
for test_ext in C.YAML_FILENAME_EXTENSIONS:
|
|
|
|
new_path = path + test_ext
|
2015-06-16 15:55:26 +00:00
|
|
|
if loader.path_exists(new_path):
|
2015-07-13 22:20:04 +00:00
|
|
|
data = loader.load_from_file(new_path)
|
|
|
|
break
|
2015-06-16 15:55:26 +00:00
|
|
|
else:
|
|
|
|
if loader.path_exists(path):
|
|
|
|
data = loader.load_from_file(path)
|
2015-01-28 20:06:10 +00:00
|
|
|
|
2014-11-01 19:34:14 +00:00
|
|
|
name = self._get_inventory_basename(path)
|
|
|
|
return (name, data)
|
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
def add_host_vars_file(self, path, loader):
|
2014-11-01 19:34:14 +00:00
|
|
|
'''
|
|
|
|
Loads and caches a host_vars file in the _host_vars_files dict,
|
|
|
|
where the key to that dictionary is the basename of the file, minus
|
|
|
|
the extension, for matching against a given inventory host name
|
|
|
|
'''
|
|
|
|
|
2015-06-16 15:55:26 +00:00
|
|
|
(name, data) = self._load_inventory_file(path, loader)
|
|
|
|
if data:
|
2015-09-04 20:41:38 +00:00
|
|
|
if name not in self._host_vars_files:
|
|
|
|
self._host_vars_files[name] = []
|
|
|
|
self._host_vars_files[name].append(data)
|
2015-06-16 15:55:26 +00:00
|
|
|
return data
|
|
|
|
else:
|
|
|
|
return dict()
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
def add_group_vars_file(self, path, loader):
|
2014-11-01 19:34:14 +00:00
|
|
|
'''
|
|
|
|
Loads and caches a host_vars file in the _host_vars_files dict,
|
|
|
|
where the key to that dictionary is the basename of the file, minus
|
|
|
|
the extension, for matching against a given inventory host name
|
|
|
|
'''
|
|
|
|
|
2015-06-16 15:55:26 +00:00
|
|
|
(name, data) = self._load_inventory_file(path, loader)
|
|
|
|
if data:
|
2015-09-04 20:41:38 +00:00
|
|
|
if name not in self._group_vars_files:
|
|
|
|
self._group_vars_files[name] = []
|
|
|
|
self._group_vars_files[name].append(data)
|
2015-06-16 15:55:26 +00:00
|
|
|
return data
|
|
|
|
else:
|
|
|
|
return dict()
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
def set_host_facts(self, host, facts):
|
|
|
|
'''
|
|
|
|
Sets or updates the given facts for a host in the fact cache.
|
|
|
|
'''
|
|
|
|
|
|
|
|
assert isinstance(facts, dict)
|
|
|
|
|
2015-09-10 21:36:06 +00:00
|
|
|
if host.name not in self._fact_cache:
|
|
|
|
self._fact_cache[host.name] = facts
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
self._fact_cache[host.name].update(facts)
|
|
|
|
except KeyError:
|
|
|
|
self._fact_cache[host.name] = facts
|
|
|
|
|
|
|
|
def set_nonpersistent_facts(self, host, facts):
|
|
|
|
'''
|
|
|
|
Sets or updates the given facts for a host in the fact cache.
|
|
|
|
'''
|
|
|
|
|
|
|
|
assert isinstance(facts, dict)
|
|
|
|
|
2015-09-09 22:28:39 +00:00
|
|
|
if host.name not in self._nonpersistent_fact_cache:
|
2015-09-09 16:21:07 +00:00
|
|
|
self._nonpersistent_fact_cache[host.name] = facts
|
2014-11-14 22:14:08 +00:00
|
|
|
else:
|
2015-07-13 15:06:03 +00:00
|
|
|
try:
|
2015-09-09 16:21:07 +00:00
|
|
|
self._nonpersistent_fact_cache[host.name].update(facts)
|
2015-07-13 15:06:03 +00:00
|
|
|
except KeyError:
|
2015-09-09 16:21:07 +00:00
|
|
|
self._nonpersistent_fact_cache[host.name] = facts
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
def set_host_variable(self, host, varname, value):
|
|
|
|
'''
|
|
|
|
Sets a value in the vars_cache for a host.
|
|
|
|
'''
|
|
|
|
|
|
|
|
host_name = host.get_name()
|
|
|
|
if host_name not in self._vars_cache:
|
|
|
|
self._vars_cache[host_name] = dict()
|
|
|
|
self._vars_cache[host_name][varname] = value
|
2014-11-01 19:34:14 +00:00
|
|
|
|