diff --git a/ansible_runner/config/_base.py b/ansible_runner/config/_base.py index 1c43da075..2208c36b4 100644 --- a/ansible_runner/config/_base.py +++ b/ansible_runner/config/_base.py @@ -120,16 +120,17 @@ def __init__(self, self.artifact_dir = os.path.join(artifact_dir, "{}".format(self.ident)) + self.loader = ArtifactLoader(self.private_data_dir) + if not project_dir: self.project_dir = os.path.join(self.private_data_dir, 'project') else: - self.project_dir = project_dir + self.project_dir = self.loader.abspath(project_dir) self.rotate_artifacts = rotate_artifacts self.fact_cache_type = fact_cache_type self.fact_cache = os.path.join(self.artifact_dir, fact_cache or 'fact_cache') if self.fact_cache_type == 'jsonfile' else None - self.loader = ArtifactLoader(self.private_data_dir) if self.host_cwd: self.host_cwd = os.path.abspath(self.host_cwd) @@ -473,6 +474,9 @@ def wrap_args_for_containerization(self, args, execution_mode, cmdline_args): self._ensure_path_safe_to_mount(self.host_cwd) self._update_volume_mount_paths(new_args, self.host_cwd) workdir = self.host_cwd + elif self.project_dir: + workdir = "/runner/project" + self._update_volume_mount_paths(new_args, self.project_dir, workdir) else: workdir = "/runner/project" diff --git a/test/fixtures/projects/project_dir_test/env/envvars b/test/fixtures/projects/project_dir_test/env/envvars new file mode 100644 index 000000000..c85123e2e --- /dev/null +++ b/test/fixtures/projects/project_dir_test/env/envvars @@ -0,0 +1 @@ +ANSIBLE_DEVEL_WARNING: no diff --git a/test/fixtures/projects/project_dir_test/inventory/inv_1 b/test/fixtures/projects/project_dir_test/inventory/inv_1 new file mode 100644 index 000000000..093f403c6 --- /dev/null +++ b/test/fixtures/projects/project_dir_test/inventory/inv_1 @@ -0,0 +1 @@ +host_1 ansible_connection=local ansible_python_interpreter="{{ ansible_playbook_python }}" diff --git a/test/fixtures/projects/project_dir_test/my_project/debug.yml b/test/fixtures/projects/project_dir_test/my_project/debug.yml new file mode 100644 index 000000000..9f75f2217 --- /dev/null +++ b/test/fixtures/projects/project_dir_test/my_project/debug.yml @@ -0,0 +1,6 @@ +- hosts: all + gather_facts: no + + tasks: + - debug: + msg: "In my_project" diff --git a/test/fixtures/projects/project_dir_test/project/debug.yml b/test/fixtures/projects/project_dir_test/project/debug.yml new file mode 100644 index 000000000..e19899a9f --- /dev/null +++ b/test/fixtures/projects/project_dir_test/project/debug.yml @@ -0,0 +1,6 @@ +- hosts: all + gather_facts: no + + tasks: + - debug: + msg: "In project" diff --git a/test/integration/test_interface.py b/test/integration/test_interface.py index 42a0b53ef..9da23faf1 100644 --- a/test/integration/test_interface.py +++ b/test/integration/test_interface.py @@ -190,6 +190,68 @@ def test_multiple_inventories(project_fixtures): assert 'host_2' in stdout +def test_default_project_dir(project_fixtures): + private_data_dir = project_fixtures / 'project_dir_test' + + res = run( + private_data_dir=private_data_dir, + playbook='debug.yml', + ) + stdout = res.stdout.read() + + assert res.rc == 0, stdout + assert 'In project' in stdout + + +def test_project_dir(project_fixtures): + private_data_dir = project_fixtures / 'project_dir_test' + + res = run( + private_data_dir=private_data_dir, + project_dir='my_project', + playbook='debug.yml', + ) + stdout = res.stdout.read() + + assert res.rc == 0, stdout + assert 'In my_project' in stdout + + +@pytest.mark.test_all_runtimes +def test_default_project_dir_inside_container(project_fixtures, runtime): + private_data_dir = project_fixtures / 'project_dir_test' + + res = run( + private_data_dir=private_data_dir, + playbook='debug.yml', + process_isolation_executable=runtime, + process_isolation=True, + container_image=defaults.default_container_image, + ) + stdout = res.stdout.read() + + assert res.rc == 0, stdout + assert 'In project' in stdout + + +@pytest.mark.test_all_runtimes +def test_project_dir_inside_container(project_fixtures, runtime): + private_data_dir = project_fixtures / 'project_dir_test' + + res = run( + private_data_dir=private_data_dir, + project_dir='my_project', + playbook='debug.yml', + process_isolation_executable=runtime, + process_isolation=True, + container_image=defaults.default_container_image, + ) + stdout = res.stdout.read() + + assert res.rc == 0, stdout + assert 'In my_project' in stdout + + def test_inventory_absolute_path(project_fixtures): private_data_dir = project_fixtures / 'debug'