diff --git a/ansible_rulebook/action/run_playbook.py b/ansible_rulebook/action/run_playbook.py index fbab1c74..2d5b18d3 100644 --- a/ansible_rulebook/action/run_playbook.py +++ b/ansible_rulebook/action/run_playbook.py @@ -149,9 +149,12 @@ 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, + copy_files=self.action_args.get( + "copy_files", self.default_copy_files + ), ) os.mkdir(project_dir) @@ -270,7 +273,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..847e6850 100644 --- a/ansible_rulebook/util.py +++ b/ansible_rulebook/util.py @@ -270,17 +270,33 @@ async def send_session_stats(event_log: asyncio.Queue, stats: Dict): ) -def create_inventory(runner_inventory_dir: str, inventory: str) -> None: - if os.path.isfile(inventory): - shutil.copy(os.path.abspath(inventory), runner_inventory_dir) - elif os.path.exists(inventory): +def create_inventory( + runner_inventory_dir: str, + inventory: str, + copy_files: bool = False, +) -> str: + inventory_src_basedir = inventory + inventory_dest_path = "" + absolute_inventory_path = os.path.abspath(inventory) + + if not os.path.exists(absolute_inventory_path): + raise InventoryNotFound(f"Inventory {inventory} not found") + + if os.path.isfile(absolute_inventory_path): + inventory_src_basedir = os.path.dirname(absolute_inventory_path) + inventory_dest_path = os.path.join( + runner_inventory_dir, os.path.basename(inventory) + ) + if not copy_files: + shutil.copy(absolute_inventory_path, runner_inventory_dir) + + if copy_files or not os.path.isfile(absolute_inventory_path): shutil.copytree( - os.path.abspath(inventory), + inventory_src_basedir, runner_inventory_dir, dirs_exist_ok=True, ) - else: - raise InventoryNotFound(f"Inventory {inventory} not found") + return os.path.join(runner_inventory_dir, inventory_dest_path) def _builtin_filter_path(name: str) -> Tuple[bool, str]: diff --git a/tests/test_engine.py b/tests/test_engine.py index d5459ebf..14fce7b1 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -234,7 +234,6 @@ async def test_start_source_with_args( @pytest.mark.asyncio async def test_run_rulesets(): - ruleset_queues, event_log = load_rulebook("rules/test_rules.yml") queue = ruleset_queues[0][1] @@ -495,33 +494,36 @@ async def test_run_assert_facts(): inventory = dict( all=dict(hosts=dict(localhost=dict(ansible_connection="local"))) ) - with tempfile.NamedTemporaryFile(mode="w+") as temp: - temp.write(yaml.dump(inventory)) - temp.flush() - queue = ruleset_queues[0][1] - queue.put_nowait(dict()) - queue.put_nowait(dict(i=1, meta=dict(hosts="localhost"))) - queue.put_nowait(Shutdown()) - await run_rulesets( - event_log, - ruleset_queues, - dict(Naboo="naboo"), - temp.name, - ) + with tempfile.TemporaryDirectory() as tempdir: + with tempfile.NamedTemporaryFile(mode="w+", dir=tempdir) as temp: + temp.write(yaml.dump(inventory)) + temp.flush() + queue = ruleset_queues[0][1] + queue.put_nowait(dict()) + queue.put_nowait(dict(i=1, meta=dict(hosts="localhost"))) + queue.put_nowait(Shutdown()) + await run_rulesets( + event_log, + ruleset_queues, + dict(Naboo="naboo"), + temp.name, + ) - assert event_log.get_nowait()["type"] == "EmptyEvent", "0" - assert event_log.get_nowait()["type"] == "Action", "0.1" - assert event_log.get_nowait()["type"] == "Job", "1.0" - for i in range(47): - assert event_log.get_nowait()["type"] == "AnsibleEvent", f"1.{i}" - - event = event_log.get_nowait() - assert event["type"] == "Action", "2.1" - assert event["action"] == "run_playbook", "2.2" - assert event["rc"] == 0, "2.3" - assert event["status"] == "successful", "2.4" - assert event_log.get_nowait()["type"] == "Shutdown", "4" - assert event_log.empty() + assert event_log.get_nowait()["type"] == "EmptyEvent", "0" + assert event_log.get_nowait()["type"] == "Action", "0.1" + assert event_log.get_nowait()["type"] == "Job", "1.0" + for i in range(47): + assert ( + event_log.get_nowait()["type"] == "AnsibleEvent" + ), f"1.{i}" + + event = event_log.get_nowait() + assert event["type"] == "Action", "2.1" + assert event["action"] == "run_playbook", "2.2" + assert event["rc"] == 0, "2.3" + assert event["status"] == "successful", "2.4" + assert event_log.get_nowait()["type"] == "Shutdown", "4" + assert event_log.empty() @pytest.mark.asyncio