From ad089959895a6255982446f34c192d4df08647c6 Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Thu, 28 Feb 2013 16:58:09 +0100 Subject: [PATCH] Make inventory basedir for directory based inventory be the directory This puts host_vars and group_vars inside of the directory, allowing you to target a single inventory but still have access to the variables. --- lib/ansible/inventory/__init__.py | 7 ++++--- lib/ansible/inventory/dir.py | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 422134a7c5..49b7f1221e 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -66,9 +66,6 @@ class Inventory(object): host_list = host_list.split(",") host_list = [ h for h in host_list if h and h.strip() ] - else: - utils.plugins.vars_loader.add_directory(self.basedir()) - if type(host_list) == list: all = Group('all') self.groups = [ all ] @@ -80,6 +77,8 @@ class Inventory(object): all.add_host(Host(x)) elif os.path.exists(host_list): if os.path.isdir(host_list): + # Ensure basedir is inside the directory + self.host_list = os.path.join(self.host_list, "") self.parser = InventoryDirectory(filename=host_list) self.groups = self.parser.groups.values() elif utils.is_executable(host_list): @@ -92,6 +91,8 @@ class Inventory(object): self.groups = self.parser.groups.values() else: raise errors.AnsibleError("YAML inventory support is deprecated in 0.6 and removed in 0.7, see the migration script in examples/scripts in the git checkout") + + utils.plugins.vars_loader.add_directory(self.basedir()) else: raise errors.AnsibleError("Unable to find an inventory file, specify one with -i ?") diff --git a/lib/ansible/inventory/dir.py b/lib/ansible/inventory/dir.py index 389fd41805..b92ef4072d 100644 --- a/lib/ansible/inventory/dir.py +++ b/lib/ansible/inventory/dir.py @@ -39,6 +39,9 @@ class InventoryDirectory(object): for i in self.names: if i.endswith("~") or i.endswith(".orig") or i.endswith(".bak"): continue + # These are things inside of an inventory basedir + if i in ("host_vars", "group_vars", "vars_plugins"): + continue fullpath = os.path.join(self.directory, i) if os.path.isdir(fullpath): parser = InventoryDirectory(filename=fullpath)