|
|
@ -1,4 +1,4 @@
|
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
# (c) 2012-2013, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
#
|
|
|
@ -23,45 +23,90 @@ import ansible.constants as C
|
|
|
|
|
|
|
|
|
|
|
|
class VarsModule(object):
|
|
|
|
class VarsModule(object):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Loads variables from group_vars/<groupname> and host_vars/<hostname> in directories parallel
|
|
|
|
|
|
|
|
to the inventory base directory or in the same directory as the playbook. Variables in the playbook
|
|
|
|
|
|
|
|
dir will win over the inventory dir if files are in both.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, inventory):
|
|
|
|
def __init__(self, inventory):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" constructor """
|
|
|
|
|
|
|
|
|
|
|
|
self.inventory = inventory
|
|
|
|
self.inventory = inventory
|
|
|
|
|
|
|
|
|
|
|
|
def run(self, host):
|
|
|
|
def run(self, host):
|
|
|
|
# return the inventory variables for the host
|
|
|
|
|
|
|
|
|
|
|
|
""" main body of the plugin, does actual loading """
|
|
|
|
|
|
|
|
|
|
|
|
inventory = self.inventory
|
|
|
|
inventory = self.inventory
|
|
|
|
#hostrec = inventory.get_host(host)
|
|
|
|
self.pb_basedir = inventory.playbook_basedir()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# sort groups by depth so deepest groups can override the less deep ones
|
|
|
|
groupz = sorted(inventory.groups_for_host(host.name), key=lambda g: g.depth)
|
|
|
|
groupz = sorted(inventory.groups_for_host(host.name), key=lambda g: g.depth)
|
|
|
|
groups = [ g.name for g in groupz ]
|
|
|
|
groups = [ g.name for g in groupz ]
|
|
|
|
basedir = inventory.basedir()
|
|
|
|
inventory_basedir = inventory.basedir()
|
|
|
|
|
|
|
|
|
|
|
|
if basedir is None:
|
|
|
|
|
|
|
|
# could happen when inventory is passed in via the API
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
results = {}
|
|
|
|
results = {}
|
|
|
|
|
|
|
|
scan_pass = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# look in both the inventory base directory and the playbook base directory
|
|
|
|
|
|
|
|
for basedir in [ inventory_basedir, self.pb_basedir ]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# this can happen from particular API usages, particularly if not run
|
|
|
|
|
|
|
|
# from /usr/bin/ansible-playbook
|
|
|
|
|
|
|
|
if basedir is None:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scan_pass = scan_pass + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# it's not an eror if the directory does not exist, keep moving
|
|
|
|
|
|
|
|
if not os.path.exists(basedir):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# save work of second scan if the directories are the same
|
|
|
|
|
|
|
|
if inventory_basedir == self.pb_basedir and scan_pass != 1:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# load vars in dir/group_vars/name_of_group
|
|
|
|
|
|
|
|
for x in groups:
|
|
|
|
|
|
|
|
|
|
|
|
# load vars in inventory_dir/group_vars/name_of_group
|
|
|
|
p = os.path.join(basedir, "group_vars/%s" % x)
|
|
|
|
for x in groups:
|
|
|
|
|
|
|
|
p = os.path.join(basedir, "group_vars/%s" % x)
|
|
|
|
# the file can be <groupname> or end in .yml or .yaml
|
|
|
|
|
|
|
|
# currently ALL will be loaded, even if more than one
|
|
|
|
|
|
|
|
paths = [p, '.'.join([p, 'yml']), '.'.join([p, 'yaml'])]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for path in paths:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if os.path.exists(path) and not os.path.isdir(path):
|
|
|
|
|
|
|
|
data = utils.parse_yaml_from_file(path)
|
|
|
|
|
|
|
|
if type(data) != dict:
|
|
|
|
|
|
|
|
raise errors.AnsibleError("%s must be stored as a dictionary/hash" % path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# combine vars overrides by default but can be configured to do a hash
|
|
|
|
|
|
|
|
# merge in settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
results = utils.combine_vars(results, data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# group vars have been loaded
|
|
|
|
|
|
|
|
# load vars in inventory_dir/hosts_vars/name_of_host
|
|
|
|
|
|
|
|
# these have greater precedence than group variables
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
p = os.path.join(basedir, "host_vars/%s" % host.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# again allow the file to be named filename or end in .yml or .yaml
|
|
|
|
paths = [p, '.'.join([p, 'yml']), '.'.join([p, 'yaml'])]
|
|
|
|
paths = [p, '.'.join([p, 'yml']), '.'.join([p, 'yaml'])]
|
|
|
|
|
|
|
|
|
|
|
|
for path in paths:
|
|
|
|
for path in paths:
|
|
|
|
|
|
|
|
|
|
|
|
if os.path.exists(path) and not os.path.isdir(path):
|
|
|
|
if os.path.exists(path) and not os.path.isdir(path):
|
|
|
|
data = utils.parse_yaml_from_file(path)
|
|
|
|
data = utils.parse_yaml_from_file(path)
|
|
|
|
if type(data) != dict:
|
|
|
|
if type(data) != dict:
|
|
|
|
raise errors.AnsibleError("%s must be stored as a dictionary/hash" % path)
|
|
|
|
raise errors.AnsibleError("%s must be stored as a dictionary/hash" % path)
|
|
|
|
results = utils.combine_vars(results, data)
|
|
|
|
results = utils.combine_vars(results, data)
|
|
|
|
|
|
|
|
|
|
|
|
# load vars in inventory_dir/hosts_vars/name_of_host
|
|
|
|
# all done, results is a dictionary of variables for this particular host.
|
|
|
|
p = os.path.join(basedir, "host_vars/%s" % host.name)
|
|
|
|
|
|
|
|
paths = [p, '.'.join([p, 'yml']), '.'.join([p, 'yaml'])]
|
|
|
|
|
|
|
|
for path in paths:
|
|
|
|
|
|
|
|
if os.path.exists(path) and not os.path.isdir(path):
|
|
|
|
|
|
|
|
data = utils.parse_yaml_from_file(path)
|
|
|
|
|
|
|
|
if type(data) != dict:
|
|
|
|
|
|
|
|
raise errors.AnsibleError("%s must be stored as a dictionary/hash" % path)
|
|
|
|
|
|
|
|
results = utils.combine_vars(results, data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return results
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|