# (c) 2012, Michael DeHaan # # 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 . import os import glob from ansible import errors from ansible import utils import ansible.constants as C class VarsModule(object): def __init__(self, inventory): self.inventory = inventory def run(self, host): # return the inventory variables for the host inventory = self.inventory #hostrec = inventory.get_host(host) groupz = sorted(inventory.groups_for_host(host.name), key=lambda g: g.depth) groups = [ g.name for g in groupz ] basedir = inventory.basedir() if basedir is None: # could happen when inventory is passed in via the API return results = {} # load vars in inventory_dir/group_vars/name_of_group for x in groups: p = os.path.join(basedir, "group_vars/%s" % x) 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) # load vars in inventory_dir/hosts_vars/name_of_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