Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set correct inventory path when it is a dir #684

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions ansible_rulebook/action/run_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion ansible_rulebook/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
groupvar: groupvar_value
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hostvar: hostvar_value
4 changes: 4 additions & 0 deletions tests/e2e/files/inventories/inventory_as_dir/hosts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
customgroup:
hosts:
localhost:
ansible_connection: local
8 changes: 8 additions & 0 deletions tests/e2e/files/playbooks/print_group_vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- hosts: customgroup
gather_facts: false
tasks:
- name: Show vars
ansible.builtin.debug:
msg:
- "groupvar: {{ groupvar }}"
- "hostvar: {{ hostvar }}"
14 changes: 14 additions & 0 deletions tests/e2e/files/rulebooks/test_inventory_as_dir.yml
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions tests/e2e/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Alex-Izquierdo marked this conversation as resolved.
Show resolved Hide resolved
"""
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"
ttuffin marked this conversation as resolved.
Show resolved Hide resolved
Loading