From ccecb2a9c19bda2b520fced07328070549b3f5cb Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 22 Nov 2024 18:47:59 +0100 Subject: [PATCH] fix: set correct inventory path when it is a dir (#684) Fixes https://github.com/ansible/ansible-rulebook/issues/681 --------- Signed-off-by: Alex --- ansible_rulebook/action/run_playbook.py | 6 ++-- ansible_rulebook/util.py | 7 +++- .../group_vars/customgroup.yml | 1 + .../inventory_as_dir/host_vars/localhost.yml | 1 + .../inventories/inventory_as_dir/hosts.yml | 4 +++ .../e2e/files/playbooks/print_group_vars.yml | 8 +++++ .../files/rulebooks/test_inventory_as_dir.yml | 14 ++++++++ tests/e2e/test_actions.py | 33 +++++++++++++++++++ 8 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 tests/e2e/files/inventories/inventory_as_dir/group_vars/customgroup.yml create mode 100644 tests/e2e/files/inventories/inventory_as_dir/host_vars/localhost.yml create mode 100644 tests/e2e/files/inventories/inventory_as_dir/hosts.yml create mode 100644 tests/e2e/files/playbooks/print_group_vars.yml create mode 100644 tests/e2e/files/rulebooks/test_inventory_as_dir.yml diff --git a/ansible_rulebook/action/run_playbook.py b/ansible_rulebook/action/run_playbook.py index fbab1c74..73030059 100644 --- a/ansible_rulebook/action/run_playbook.py +++ b/ansible_rulebook/action/run_playbook.py @@ -149,9 +149,8 @@ async def _pre_process(self) -> None: os.mkdir(inventory_dir) if self.helper.control.inventory: - create_inventory(inventory_dir, self.helper.control.inventory) - self.inventory = os.path.join( - inventory_dir, os.path.basename(self.helper.control.inventory) + self.inventory = create_inventory( + inventory_dir, self.helper.control.inventory ) os.mkdir(project_dir) @@ -270,7 +269,6 @@ def _get_latest_artifact(self, component: str, content: bool = True): return files[0] async def _untar_project(self, output_dir, project_data_file): - cmd = [tar, "zxvf", project_data_file] proc = await asyncio.create_subprocess_exec( *cmd, diff --git a/ansible_rulebook/util.py b/ansible_rulebook/util.py index 15df1333..7acfe1cf 100644 --- a/ansible_rulebook/util.py +++ b/ansible_rulebook/util.py @@ -270,17 +270,22 @@ async def send_session_stats(event_log: asyncio.Queue, stats: Dict): ) -def create_inventory(runner_inventory_dir: str, inventory: str) -> None: +def create_inventory(runner_inventory_dir: str, inventory: str) -> str: if os.path.isfile(inventory): shutil.copy(os.path.abspath(inventory), runner_inventory_dir) + inventory_path = os.path.join( + runner_inventory_dir, os.path.basename(inventory) + ) elif os.path.exists(inventory): shutil.copytree( os.path.abspath(inventory), runner_inventory_dir, dirs_exist_ok=True, ) + inventory_path = runner_inventory_dir else: raise InventoryNotFound(f"Inventory {inventory} not found") + return inventory_path def _builtin_filter_path(name: str) -> Tuple[bool, str]: diff --git a/tests/e2e/files/inventories/inventory_as_dir/group_vars/customgroup.yml b/tests/e2e/files/inventories/inventory_as_dir/group_vars/customgroup.yml new file mode 100644 index 00000000..c8e3085f --- /dev/null +++ b/tests/e2e/files/inventories/inventory_as_dir/group_vars/customgroup.yml @@ -0,0 +1 @@ +groupvar: groupvar_value diff --git a/tests/e2e/files/inventories/inventory_as_dir/host_vars/localhost.yml b/tests/e2e/files/inventories/inventory_as_dir/host_vars/localhost.yml new file mode 100644 index 00000000..613c670e --- /dev/null +++ b/tests/e2e/files/inventories/inventory_as_dir/host_vars/localhost.yml @@ -0,0 +1 @@ +hostvar: hostvar_value diff --git a/tests/e2e/files/inventories/inventory_as_dir/hosts.yml b/tests/e2e/files/inventories/inventory_as_dir/hosts.yml new file mode 100644 index 00000000..27265db9 --- /dev/null +++ b/tests/e2e/files/inventories/inventory_as_dir/hosts.yml @@ -0,0 +1,4 @@ +customgroup: + hosts: + localhost: + ansible_connection: local diff --git a/tests/e2e/files/playbooks/print_group_vars.yml b/tests/e2e/files/playbooks/print_group_vars.yml new file mode 100644 index 00000000..3049dbd5 --- /dev/null +++ b/tests/e2e/files/playbooks/print_group_vars.yml @@ -0,0 +1,8 @@ +- hosts: customgroup + gather_facts: false + tasks: + - name: Show vars + ansible.builtin.debug: + msg: + - "groupvar: {{ groupvar }}" + - "hostvar: {{ hostvar }}" diff --git a/tests/e2e/files/rulebooks/test_inventory_as_dir.yml b/tests/e2e/files/rulebooks/test_inventory_as_dir.yml new file mode 100644 index 00000000..c28a8a4a --- /dev/null +++ b/tests/e2e/files/rulebooks/test_inventory_as_dir.yml @@ -0,0 +1,14 @@ +- name: Test inventory as dir + hosts: all + sources: + - ansible.eda.generic: + shutdown_after: 2 + payload: + motto: winter is coming + rules: + - name: Test rule + condition: event.motto == "winter is coming" + action: + run_playbook: + name: ./playbooks/print_group_vars.yml + copy_files: true diff --git a/tests/e2e/test_actions.py b/tests/e2e/test_actions.py index a1f734a3..c7ac44a5 100644 --- a/tests/e2e/test_actions.py +++ b/tests/e2e/test_actions.py @@ -384,3 +384,36 @@ def test_shutdown_action_now(update_environment): assert ( "This condition should not fire" not in result.stdout ), "a post-shutdown condition fired when it should not have" + + +@pytest.mark.e2e +def test_inventory_as_dir(): + """ + Execute a rulebook that contains a run_playbook action with an inventory + directory instead of a file. + """ + + rulebook = utils.BASE_DATA_PATH / "rulebooks/test_inventory_as_dir.yml" + inventory = utils.BASE_DATA_PATH / "inventories/inventory_as_dir" + cmd = utils.Command( + rulebook=rulebook, + inventory=inventory, + ) + + LOGGER.info(f"Running command: {cmd}") + result = subprocess.run( + cmd, + timeout=DEFAULT_CMD_TIMEOUT, + capture_output=True, + cwd=utils.BASE_DATA_PATH, + text=True, + ) + + assert result.returncode == 0 + assert not result.stderr + assert ( + "hostvar_value" in result.stdout + ), "hostvar_value not found in stdout" + assert ( + "groupvar_value" in result.stdout + ), "groupvar_value not found in stdout"