mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 19:31:26 -07: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.
|
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']
|
'parser', '_vars_per_host', '_vars_per_group', '_hosts_cache', '_groups_list']
|
||||||
|
|
||||||
def __init__(self, host_list=C.DEFAULT_HOST_LIST):
|
def __init__(self, host_list=C.DEFAULT_HOST_LIST):
|
||||||
|
@ -60,9 +60,6 @@ class Inventory(object):
|
||||||
self._also_restriction = None
|
self._also_restriction = None
|
||||||
self._subset = None
|
self._subset = None
|
||||||
|
|
||||||
# whether the inventory file is a script
|
|
||||||
self._is_script = False
|
|
||||||
|
|
||||||
if type(host_list) in [ str, unicode ]:
|
if type(host_list) in [ str, unicode ]:
|
||||||
if host_list.find(",") != -1:
|
if host_list.find(",") != -1:
|
||||||
host_list = host_list.split(",")
|
host_list = host_list.split(",")
|
||||||
|
@ -82,7 +79,6 @@ class Inventory(object):
|
||||||
all.add_host(Host(x))
|
all.add_host(Host(x))
|
||||||
elif os.path.exists(host_list):
|
elif os.path.exists(host_list):
|
||||||
if utils.is_executable(host_list):
|
if utils.is_executable(host_list):
|
||||||
self._is_script = True
|
|
||||||
self.parser = InventoryScript(filename=host_list)
|
self.parser = InventoryScript(filename=host_list)
|
||||||
self.groups = self.parser.groups.values()
|
self.groups = self.parser.groups.values()
|
||||||
else:
|
else:
|
||||||
|
@ -280,16 +276,7 @@ class Inventory(object):
|
||||||
vars.update(updated)
|
vars.update(updated)
|
||||||
|
|
||||||
vars.update(host.get_variables())
|
vars.update(host.get_variables())
|
||||||
if self._is_script:
|
vars.update(self.parser.get_host_variables(host))
|
||||||
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)
|
|
||||||
return vars
|
return vars
|
||||||
|
|
||||||
def add_group(self, group):
|
def add_group(self, group):
|
||||||
|
|
|
@ -173,3 +173,6 @@ class InventoryParser(object):
|
||||||
group.set_variable(k, re.sub(r"^['\"]|['\"]$", '', v))
|
group.set_variable(k, re.sub(r"^['\"]|['\"]$", '', v))
|
||||||
else:
|
else:
|
||||||
group.set_variable(k, v)
|
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):
|
def __init__(self, filename=C.DEFAULT_HOST_LIST):
|
||||||
|
|
||||||
cmd = [ filename, "--list" ]
|
self.filename = filename
|
||||||
|
cmd = [ self.filename, "--list" ]
|
||||||
try:
|
try:
|
||||||
sp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
sp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
except OSError, e:
|
except OSError, e:
|
||||||
|
@ -77,3 +78,13 @@ class InventoryScript(object):
|
||||||
if child_name in groups:
|
if child_name in groups:
|
||||||
groups[group_name].add_child_group(groups[child_name])
|
groups[group_name].add_child_group(groups[child_name])
|
||||||
return groups
|
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…
Add table
Add a link
Reference in a new issue