From 647cd0141cf1fff6817ed0de5c22f15c6179d3d2 Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Wed, 27 Feb 2013 20:35:08 +0100 Subject: [PATCH 1/5] Move all inventory script code into the script parser --- lib/ansible/inventory/__init__.py | 17 ++--------------- lib/ansible/inventory/ini.py | 3 +++ lib/ansible/inventory/script.py | 13 ++++++++++++- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 32c757d282..0d5dc853cc 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -35,7 +35,7 @@ class Inventory(object): Host inventory for ansible. """ - __slots__ = [ 'host_list', 'groups', '_restriction', '_also_restriction', '_subset', '_is_script', + __slots__ = [ 'host_list', 'groups', '_restriction', '_also_restriction', '_subset', 'parser', '_vars_per_host', '_vars_per_group', '_hosts_cache', '_groups_list'] def __init__(self, host_list=C.DEFAULT_HOST_LIST): @@ -60,9 +60,6 @@ class Inventory(object): self._also_restriction = None self._subset = None - # whether the inventory file is a script - self._is_script = False - if type(host_list) in [ str, unicode ]: if host_list.find(",") != -1: host_list = host_list.split(",") @@ -82,7 +79,6 @@ class Inventory(object): all.add_host(Host(x)) elif os.path.exists(host_list): if utils.is_executable(host_list): - self._is_script = True self.parser = InventoryScript(filename=host_list) self.groups = self.parser.groups.values() else: @@ -280,16 +276,7 @@ class Inventory(object): vars.update(updated) vars.update(host.get_variables()) - if self._is_script: - cmd = [self.host_list,"--host",hostname] - try: - sp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - except OSError, e: - raise errors.AnsibleError("problem running %s (%s)" % (' '.join(cmd), e)) - (out, err) = sp.communicate() - results = utils.parse_json(out) - - vars.update(results) + vars.update(self.parser.get_host_variables(host)) return vars def add_group(self, group): diff --git a/lib/ansible/inventory/ini.py b/lib/ansible/inventory/ini.py index d79a148894..d05037bc0b 100644 --- a/lib/ansible/inventory/ini.py +++ b/lib/ansible/inventory/ini.py @@ -173,3 +173,6 @@ class InventoryParser(object): group.set_variable(k, re.sub(r"^['\"]|['\"]$", '', v)) else: group.set_variable(k, v) + + def get_host_variables(self, host): + return {} diff --git a/lib/ansible/inventory/script.py b/lib/ansible/inventory/script.py index 44d3f37b2b..628e802934 100644 --- a/lib/ansible/inventory/script.py +++ b/lib/ansible/inventory/script.py @@ -29,7 +29,8 @@ class InventoryScript(object): def __init__(self, filename=C.DEFAULT_HOST_LIST): - cmd = [ filename, "--list" ] + self.filename = filename + cmd = [ self.filename, "--list" ] try: sp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except OSError, e: @@ -77,3 +78,13 @@ class InventoryScript(object): if child_name in groups: groups[group_name].add_child_group(groups[child_name]) return groups + + def get_host_variables(self, host): + """ Runs