mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Revert to using local file/dir tests in inventory instead of loader's
Fixes #12719
This commit is contained in:
parent
c637d60a8d
commit
e6d3c6745f
2 changed files with 22 additions and 7 deletions
|
@ -104,7 +104,7 @@ class Inventory(object):
|
||||||
all.add_host(Host(host, port))
|
all.add_host(Host(host, port))
|
||||||
elif self._loader.path_exists(host_list):
|
elif self._loader.path_exists(host_list):
|
||||||
#TODO: switch this to a plugin loader and a 'condition' per plugin on which it should be tried, restoring 'inventory pllugins'
|
#TODO: switch this to a plugin loader and a 'condition' per plugin on which it should be tried, restoring 'inventory pllugins'
|
||||||
if self._loader.is_directory(host_list):
|
if self.is_directory(host_list):
|
||||||
# Ensure basedir is inside the directory
|
# Ensure basedir is inside the directory
|
||||||
host_list = os.path.join(self.host_list, "")
|
host_list = os.path.join(self.host_list, "")
|
||||||
self.parser = InventoryDirectory(loader=self._loader, groups=self.groups, filename=host_list)
|
self.parser = InventoryDirectory(loader=self._loader, groups=self.groups, filename=host_list)
|
||||||
|
@ -592,23 +592,36 @@ class Inventory(object):
|
||||||
self._restriction = None
|
self._restriction = None
|
||||||
|
|
||||||
def is_file(self):
|
def is_file(self):
|
||||||
""" did inventory come from a file? """
|
"""
|
||||||
|
Did inventory come from a file? We don't use the equivalent loader
|
||||||
|
methods in inventory, due to the fact that the loader does an implict
|
||||||
|
DWIM on the path, which may be incorrect for inventory paths relative
|
||||||
|
to the playbook basedir.
|
||||||
|
"""
|
||||||
if not isinstance(self.host_list, string_types):
|
if not isinstance(self.host_list, string_types):
|
||||||
return False
|
return False
|
||||||
return self._loader.path_exists(self.host_list)
|
return os.path.isfile(self.host_list) or self.host_list == os.devnull
|
||||||
|
|
||||||
|
def is_directory(self, path):
|
||||||
|
"""
|
||||||
|
Is the inventory host list a directory? Same caveat for here as with
|
||||||
|
the is_file() method above.
|
||||||
|
"""
|
||||||
|
if not isinstance(self.host_list, string_types):
|
||||||
|
return False
|
||||||
|
return os.path.isdir(path)
|
||||||
|
|
||||||
def basedir(self):
|
def basedir(self):
|
||||||
""" if inventory came from a file, what's the directory? """
|
""" if inventory came from a file, what's the directory? """
|
||||||
dname = self.host_list
|
dname = self.host_list
|
||||||
if not self.is_file():
|
if not self.is_file():
|
||||||
dname = None
|
dname = None
|
||||||
elif self._loader.is_directory(self.host_list):
|
elif self.is_directory(self.host_list):
|
||||||
dname = self.host_list
|
dname = self.host_list
|
||||||
else:
|
else:
|
||||||
dname = os.path.dirname(self.host_list)
|
dname = os.path.dirname(self.host_list)
|
||||||
if dname is None or dname == '' or dname == '.':
|
if dname is None or dname == '' or dname == '.':
|
||||||
cwd = os.getcwd()
|
dname = os.getcwd()
|
||||||
dname = cwd
|
|
||||||
if dname:
|
if dname:
|
||||||
dname = os.path.abspath(dname)
|
dname = os.path.abspath(dname)
|
||||||
return dname
|
return dname
|
||||||
|
|
|
@ -177,7 +177,8 @@ class TestVariableManager(unittest.TestCase):
|
||||||
v = VariableManager()
|
v = VariableManager()
|
||||||
self.assertEqual(v.get_vars(loader=fake_loader, task=mock_task, use_cache=False).get("foo"), "bar")
|
self.assertEqual(v.get_vars(loader=fake_loader, task=mock_task, use_cache=False).get("foo"), "bar")
|
||||||
|
|
||||||
def test_variable_manager_precedence(self):
|
@patch.object(Inventory, 'basedir')
|
||||||
|
def test_variable_manager_precedence(self, mock_basedir):
|
||||||
'''
|
'''
|
||||||
Tests complex variations and combinations of get_vars() with different
|
Tests complex variations and combinations of get_vars() with different
|
||||||
objects to modify the context under which variables are merged.
|
objects to modify the context under which variables are merged.
|
||||||
|
@ -224,6 +225,7 @@ class TestVariableManager(unittest.TestCase):
|
||||||
""",
|
""",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
mock_basedir.return_value = './'
|
||||||
inv1 = Inventory(loader=fake_loader, variable_manager=v, host_list='/etc/ansible/inventory1')
|
inv1 = Inventory(loader=fake_loader, variable_manager=v, host_list='/etc/ansible/inventory1')
|
||||||
inv1.set_playbook_basedir('./')
|
inv1.set_playbook_basedir('./')
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue