From ba3466af956ac865b99857dc69092a13d6db0a53 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Fri, 20 Jul 2012 09:43:45 -0400 Subject: [PATCH] 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. --- CHANGELOG.md | 1 + lib/ansible/inventory/__init__.py | 19 +++++++++++++++++++ lib/ansible/playbook/play.py | 15 +++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77bd6aea1f..ad8645e7ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ Ansible Changes By Release * mount module * apt module now passes DEBIAN_FRONTEND=noninteractive * 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 diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index c7d6057ea7..60a3523aba 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -80,6 +80,7 @@ class Inventory(object): def _match(self, 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"): """ Get all host objects matching the pattern """ hosts = {} @@ -107,6 +108,8 @@ class Inventory(object): def get_groups(self): 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): for group in self.groups: for host in group.get_hosts(): @@ -120,12 +123,16 @@ class Inventory(object): return group 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): group = self.get_group(groupname) if group is None: raise Exception("group not found: %s" % groupname) 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): if self._is_script: @@ -169,3 +176,15 @@ class Inventory(object): """ Do not restrict list operations """ 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) diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index 3e5ad39253..3306c80c80 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -203,6 +203,21 @@ class Play(object): if type(self.vars_files) != list: 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: if type(filename) == list: