mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Move all inventory script code into the script parser
This commit is contained in:
parent
7749b34546
commit
647cd0141c
3 changed files with 17 additions and 16 deletions
|
@ -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):
|
||||
|
|
|
@ -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 {}
|
||||
|
|
|
@ -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 <script> --host <hostname> to determine additional host variables """
|
||||
cmd = [self.filename, "--host", host.name]
|
||||
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()
|
||||
return utils.parse_json(out)
|
||||
|
|
Loading…
Reference in a new issue