Skip to content

Commit

Permalink
Fix invalid inventory with relative pvt data dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Shrews committed Oct 4, 2023
1 parent 60cfaa1 commit 47b62b8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/ansible_runner/config/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,20 @@ def prepare(self):
def prepare_inventory(self):
"""
Prepares the inventory default under ``private_data_dir`` if it's not overridden by the constructor.
We make sure that if inventory is a path, that it is an absolute path.
"""
if self.containerized:
self.inventory = '/runner/inventory'
return

if self.inventory is None:
# At this point we expect self.private_data_dir to be an absolute path
# since that is expanded in the base class.
if os.path.exists(os.path.join(self.private_data_dir, "inventory")):
self.inventory = os.path.join(self.private_data_dir, "inventory")
elif os.path.exists(self.inventory):
self.inventory = os.path.abspath(self.inventory)

def prepare_env(self):
"""
Expand Down
46 changes: 46 additions & 0 deletions test/integration/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,49 @@ def test_get_role_argspec_within_container(project_fixtures, runtime, skipif_pre
assert isinstance(resp, dict)
assert 'Into_The_Mystic' in resp
assert resp['Into_The_Mystic']['entry_points'] == expected_epoint


class TestRelativePvtDataDirPaths:
"""
Class to handle test setup/teardown of tests that need to change working
directory to test relative paths.
"""

def setup_method(self):
self._old_workdir = os.getcwd() # pylint: disable=W0201

def teardown_method(self):
os.chdir(self._old_workdir)

def test_inventory_as_string(self, project_fixtures):
"""
Test of bug fix for GH issue #1216: https://github.com/ansible/ansible-runner/issues/1216
A relative private data directory combined with an inventory specified as a string
would produce an invalid inventory path being passed along to ansible.
"""
os.chdir(str(project_fixtures))

inventory = 'hostA ansible_connection=local ansible_python_interpreter="{{ ansible_playbook_python }}"'

r = run(private_data_dir='debug',
inventory=inventory,
playbook='debug.yml')

with r.stdout as output:
text = output.read()

assert "No inventory was parsed" not in text

def test_default_inventory(self, project_fixtures):
"""
Test relative pvt data dir with the default inventory.
"""
os.chdir(str(project_fixtures))

r = run(private_data_dir='debug', playbook='debug.yml')

with r.stdout as output:
text = output.read()

assert "No inventory was parsed" not in text

0 comments on commit 47b62b8

Please sign in to comment.