1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

The following paths are now implict and optional in vars_files:

./group_vars/groupname.yml (for all groups the host is in)
./host_vars/hostname.yml (for the hostname given in the inventory)

This requires an actual inventory file, not script and the paths are relative
to the directory of the inventory file.
This commit is contained in:
Michael DeHaan 2012-07-20 09:43:45 -04:00
parent db1a4d8fac
commit ba3466af95
3 changed files with 35 additions and 0 deletions

View file

@ -31,6 +31,7 @@ Ansible Changes By Release
* mount module * mount module
* apt module now passes DEBIAN_FRONTEND=noninteractive * apt module now passes DEBIAN_FRONTEND=noninteractive
* to catch typos, like 'var' for 'vars', playbooks and tasks now yell on invalid parameters * to catch typos, like 'var' for 'vars', playbooks and tasks now yell on invalid parameters
* automatically load (directory_of_inventory_file)/group_vars/groupname and /host_vars/hostname in vars_files
0.5 "Amsterdam" ------- July 04, 2012 0.5 "Amsterdam" ------- July 04, 2012

View file

@ -80,6 +80,7 @@ class Inventory(object):
def _match(self, str, pattern_str): def _match(self, str, pattern_str):
return fnmatch.fnmatch(str, pattern_str) return fnmatch.fnmatch(str, pattern_str)
# TODO: cache this logic so if called a second time the result is not recalculated
def get_hosts(self, pattern="all"): def get_hosts(self, pattern="all"):
""" Get all host objects matching the pattern """ """ Get all host objects matching the pattern """
hosts = {} hosts = {}
@ -107,6 +108,8 @@ class Inventory(object):
def get_groups(self): def get_groups(self):
return self.groups return self.groups
# TODO: cache this logic so if called a second time the result is not recalculated
# if using inventory scripts
def get_host(self, hostname): def get_host(self, hostname):
for group in self.groups: for group in self.groups:
for host in group.get_hosts(): for host in group.get_hosts():
@ -120,12 +123,16 @@ class Inventory(object):
return group return group
return None return None
# TODO: cache this logic so if called a second time the result is not recalculated
# if using inventory scripts
def get_group_variables(self, groupname): def get_group_variables(self, groupname):
group = self.get_group(groupname) group = self.get_group(groupname)
if group is None: if group is None:
raise Exception("group not found: %s" % groupname) raise Exception("group not found: %s" % groupname)
return group.get_variables() return group.get_variables()
# TODO: cache this logic so if called a second time the result is not recalculated
# if using inventory scripts
def get_variables(self, hostname): def get_variables(self, hostname):
if self._is_script: if self._is_script:
@ -169,3 +176,15 @@ class Inventory(object):
""" Do not restrict list operations """ """ Do not restrict list operations """
self._restriction = None self._restriction = None
def is_file(self):
""" did inventory come from a file? """
if not isinstance(self.host_list, basestring):
return False
return os.path.exists(self.host_list)
def basedir(self):
""" if inventory came from a file, what's the directory? """
if not self.is_file():
return None
return os.path.dirname(self.host_list)

View file

@ -203,6 +203,21 @@ class Play(object):
if type(self.vars_files) != list: if type(self.vars_files) != list:
self.vars_files = [ self.vars_files ] self.vars_files = [ self.vars_files ]
if (host is not None):
inventory = self.playbook.inventory
hostrec = inventory.get_host(host)
groups = [ g.name for g in hostrec.groups ]
basedir = inventory.basedir()
if basedir is not None:
for x in groups:
path = os.path.join(basedir, "group_vars/%s" % x)
if os.path.exists(path):
self.vars_files.append(path)
path = os.path.join(basedir, "host_vars/%s" % hostrec.name)
if os.path.exists(path):
self.vars_files.append(path)
for filename in self.vars_files: for filename in self.vars_files:
if type(filename) == list: if type(filename) == list: