Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-23
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Design

## Decisions

### 1. Shared helper in `repo_content.py`
Use the existing `_REQUIREMENTS_FILES` order and one helper `find_collection_requirements(root: Path) -> Path | None` in `repo_content.py`. The helper returns the first existing path. The collections row, `install_collections`, and the onboard roles row all use this helper.
- **Evidence:** `src/devops_cli/infra/repo_content.py:37` defines `_REQUIREMENTS_FILES = ("collections/requirements.yml", "collections/requirements.yaml")`.
- **Reason:** Reusing the existing two-name discovery removes the drift that caused the defect.

### 2. Pass discovered path to `install_requirements`
Update `install_requirements(tool: AnsibleTool, project: Path, target: Path, requirements: Path | None = None) -> None` in `ansible_facts.py` to accept the resolved requirements file.
- **Evidence:** `src/devops_cli/infra/ansible_facts.py:178` passes `project / REQUIREMENTS_FILE` to `galaxy_argv`.
- **Reason:** Passing the resolved path allows callers to supply the discovered file while preserving backwards compatibility.

## Rejected alternatives

### Accept only `collections/requirements.yml`
- **Evidence:** `src/devops_cli/infra/repo_content.py:37` has supported `.yaml` in onboard since inception.
- **Reason:** Rejecting `.yaml` breaks existing repositories that use `collections/requirements.yaml` and alters the existing `present` check in onboard.

### Repeat the two-name check in each caller
- **Evidence:** `src/devops_cli/infra/plan.py:473` and `src/devops_cli/infra/onboard.py:254` previously drifted from `repo_content.py:200`.
- **Reason:** Duplicated discovery logic causes future drift.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Proposal: #6698

## Why
The collections row of `devops infra onboard` reads `collections/requirements.yml` or `collections/requirements.yaml`, but `devops infra plan` and `devops infra apply` install only `collections/requirements.yml`. The roles row of onboard also installs only `collections/requirements.yml`. A repo that holds only `collections/requirements.yaml` gets a `present` collections row, but plan and apply run with no pinned collections installed. Closes #6698.

## What Changes
Use one shared discovery rule for the collection requirements file across onboard, plan and apply:
- Export a helper `find_collection_requirements(root: Path) -> Path | None` in `repo_content.py` that checks `_REQUIREMENTS_FILES` (`collections/requirements.yml`, `collections/requirements.yaml`) in order.
- In `plan.py`, `install_collections` uses the discovered requirements file rather than hardcoding `collections/requirements.yml`.
- In `ansible_facts.py`, `install_requirements` accepts an optional `requirements: Path | None = None` parameter, defaulting to `project / REQUIREMENTS_FILE` when omitted.
- In `onboard.py`, `_read_collection_roles` uses the discovered requirements file.
- Add unit tests verifying discovery and installation parity for repositories holding only `collections/requirements.yaml`.

## Capabilities
- `infra-onboard`: The roles row installs collections from `collections/requirements.yaml` when `.yml` is absent.
- `infra-plan`: The plan and apply install step installs collections from `collections/requirements.yaml` when `.yml` is absent.

## Impact
No breaking changes. Repositories with `collections/requirements.yaml` now have their pinned collections correctly installed during onboard, plan, and apply.

## Feasibility
Verified in `src/devops_cli/infra/repo_content.py:37` that `_REQUIREMENTS_FILES` already defines `collections/requirements.yml` and `collections/requirements.yaml`. `read_collection_requirements` uses this tuple, while `plan.py` and `onboard.py` hardcode `REQUIREMENTS_FILE` (`collections/requirements.yml`). Sharing the discovery rule makes behavior consistent across all infra commands.

The change fits one pull request.

## Existing Tool Considered
No open, maintained tool covers repository collection file discovery. The alternatives check in #6698 records `none`. The internal change uses standard Python `pathlib.Path` checks and adds no dependencies, deleting the duplicated hardcoded checks. Tokens to build: ~2,000, keep: 500, elapsed time: < 1 hour.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## MODIFIED Requirements

### Requirement: The roles row lists the roles of the pinned collections
When the export holds a collection requirements file (`collections/requirements.yml` or `collections/requirements.yaml`), the command SHALL install collections into a temporary folder with `ansible-galaxy collection install`, read `ansible-doc -t role -l -j` with that folder as collections path, and remove the folder. The roles row SHALL list role folders and each collection role by full name. The command MUST NOT read collection files directly.

#### Scenario: A role of a pinned collection
- **WHEN** `collections/requirements.yml` pins the collection `example.infra`, whose role `nvidia` has an argument spec
- **THEN** the roles row is `present` and lists the 5 roles of `ansible/roles`, then `example.infra.nvidia`, `ansible-doc` ran with `ANSIBLE_COLLECTIONS_PATH` set to the `-p` folder of the install and `ANSIBLE_COLLECTIONS_SCAN_SYS_PATH` set to `False`, and the folder no longer exists

#### Scenario: Onboard on a YAML-only repository
- **WHEN** `collections/requirements.yaml` pins the collection `example.infra` and no `collections/requirements.yml` exists
- **THEN** the roles row installs from `collections/requirements.yaml` and lists the roles of the pinned collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## MODIFIED Requirements

### Requirement: The plan runs the playbook in check mode
The command SHALL run `ansible-runner run` with `--check --diff -v`, the inventory of the profile, `--tags` when given, the key of `devops ssh` as `ansible_ssh_private_key_file`, the repo's `ansible.cfg` and host key checks on. Before the run it SHALL install the pinned collections of the discovered requirements file (`collections/requirements.yml` or `collections/requirements.yaml`) once for each version of that file.

#### Scenario: The run options
- **WHEN** a person plans `ansible/playbooks/03_k3s.yaml` with `--limit node-5.example.com --tags k3s`
- **THEN** `ansible-galaxy collection install` runs before `ansible-runner`, and `ansible-runner` gets `--playbook ansible/playbooks/03_k3s.yaml`, `--limit node-5.example.com:!offline`, the command line `--check --diff -v -i ansible/inventory/hosts.yaml --tags k3s -e ansible_ssh_private_key_file=<key>` and `ANSIBLE_HOST_KEY_CHECKING=True`

#### Scenario: The collections install once
- **WHEN** a person plans twice at the same commit
- **THEN** `ansible-galaxy` runs once and `ansible-runner` runs twice

#### Scenario: A playbook outside the repo
- **WHEN** a person plans `../outside.yaml`
- **THEN** the command exits 1, says that it is not a playbook file of the infra repo, and starts no `ansible-runner`

#### Scenario: Plan on a YAML-only repository
- **WHEN** the project has only `collections/requirements.yaml` and no `collections/requirements.yml`
- **THEN** the install step installs the pinned collections from `collections/requirements.yaml`
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Tasks

## 1. Behavior: discover and install collections from requirements.yaml or requirements.yml

- [x] 1.1 Write failing tests: a repository with only `collections/requirements.yaml` gets the same answer from the collections row and from the install of plan; `find_collection_requirements` returns `collections/requirements.yaml` when `.yml` is absent; the onboard roles row installs from `collections/requirements.yaml`
- Verification: `$ uv run devops ci test tests/commands/test_infra_onboard.py tests/infra/test_plan.py`
- [x] 1.2 Implement `find_collection_requirements` in `repo_content.py`, pass the resolved requirements path to `install_requirements` in `ansible_facts.py`, and update callers in `plan.py` and `onboard.py`
- Verification: `$ uv run devops ci test tests/commands/test_infra_onboard.py tests/infra/test_plan.py`

- Pending a person: review the diff in tuicr (tuicr -r origin/main..HEAD).
6 changes: 5 additions & 1 deletion openspec/specs/infra-onboard/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,16 @@ Only `ansible-runner` SHALL go on PATH. The command SHALL start the Ansible comm
- **THEN** the command finds no tool environment, and a dry run shows each Ansible command with no folder

### Requirement: The roles row lists the roles of the pinned collections
When the export holds `collections/requirements.yml`, the command SHALL install its collections into a new temporary folder with `ansible-galaxy collection install`, read `ansible-doc -t role -l -j` with only that folder as the collections path, and remove the folder. The roles row SHALL list the roles of the role folders, then each role of the listing that names a collection, by its full name. The command MUST NOT read the files of a collection itself.
When the export holds a collection requirements file (`collections/requirements.yml` or `collections/requirements.yaml`), the command SHALL install collections into a temporary folder with `ansible-galaxy collection install`, read `ansible-doc -t role -l -j` with that folder as collections path, and remove the folder. The roles row SHALL list role folders and each collection role by full name. The command MUST NOT read collection files directly.

#### Scenario: A role of a pinned collection
- **WHEN** `collections/requirements.yml` pins the collection `example.infra`, whose role `nvidia` has an argument spec
- **THEN** the roles row is `present` and lists the 5 roles of `ansible/roles`, then `example.infra.nvidia`, `ansible-doc` ran with `ANSIBLE_COLLECTIONS_PATH` set to the `-p` folder of the install and `ANSIBLE_COLLECTIONS_SCAN_SYS_PATH` set to `False`, and the folder no longer exists

#### Scenario: Onboard on a YAML-only repository
- **WHEN** `collections/requirements.yaml` pins the collection `example.infra` and no `collections/requirements.yml` exists
- **THEN** the roles row installs from `collections/requirements.yaml` and lists the roles of the pinned collection

### Requirement: The roles row keeps the role folders when the collections do not read
When no Ansible tool environment is installed, or its folder holds no `ansible-doc`, the command SHALL start no install, and the roles row SHALL list the roles of the role folders and say in its reason that it does not list the roles of the pinned collections. When the install or the listing fails, the row SHALL list the roles of the role folders and name the command, its exit code and its last line in its reason.

Expand Down
6 changes: 5 additions & 1 deletion openspec/specs/infra-plan/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ How `devops infra plan` runs a playbook of an infra repo in check mode at one co
- **THEN** the command exits 1, names the ref, and exports nothing

### Requirement: The plan runs the playbook in check mode
The command SHALL run `ansible-runner run` with `--check --diff -v`, the inventory of the profile, `--tags` when given, the key of `devops ssh` as `ansible_ssh_private_key_file`, the repo's `ansible.cfg` and host key checks on. Before the run it SHALL install the pinned collections of `collections/requirements.yml` once for each version of that file.
The command SHALL run `ansible-runner run` with `--check --diff -v`, the inventory of the profile, `--tags` when given, the key of `devops ssh` as `ansible_ssh_private_key_file`, the repo's `ansible.cfg` and host key checks on. Before the run it SHALL install the pinned collections of the discovered requirements file (`collections/requirements.yml` or `collections/requirements.yaml`) once for each version of that file.

#### Scenario: The run options
- **WHEN** a person plans `ansible/playbooks/03_k3s.yaml` with `--limit node-5.example.com --tags k3s`
Expand All @@ -31,6 +31,10 @@ The command SHALL run `ansible-runner run` with `--check --diff -v`, the invento
- **WHEN** a person plans `../outside.yaml`
- **THEN** the command exits 1, says that it is not a playbook file of the infra repo, and starts no `ansible-runner`

#### Scenario: Plan on a YAML-only repository
- **WHEN** the project has only `collections/requirements.yaml` and no `collections/requirements.yml`
- **THEN** the install step installs the pinned collections from `collections/requirements.yaml`

### Requirement: No host of the offline group is in the limit
The host pattern of the run SHALL be the `--limit`, else `all`, followed by `!<offline group>` as a term of its own when the profile names a group of hosts out of service: the term is joined with a comma when the limit holds a comma, and with a colon when it holds none.

Expand Down
9 changes: 6 additions & 3 deletions src/devops_cli/infra/ansible_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,18 @@ def galaxy_argv(tool: AnsibleTool, requirements: Path, target: Path) -> list[str
)


def install_requirements(tool: AnsibleTool, project: Path, target: Path) -> None:
"""Install the collections of `collections/requirements.yml` of the repo in `project` into the
def install_requirements(
tool: AnsibleTool, project: Path, target: Path, requirements: Path | None = None
) -> None:
"""Install the collections of the requirements file of the repo in `project` into the
folder `target`, with the repo's `ansible.cfg`.

Raises:
InfraReadError: the install fails.
"""
req_file = requirements if requirements is not None else project / REQUIREMENTS_FILE
result = run_subprocess(
galaxy_argv(tool, project / REQUIREMENTS_FILE, target),
galaxy_argv(tool, req_file, target),
cwd=project,
env=config_env(project),
quiet=True,
Expand Down
17 changes: 11 additions & 6 deletions src/devops_cli/infra/onboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
from devops_cli.infra.access import AgentAccess
from devops_cli.infra.ansible_facts import (
DOC_COMMAND,
REQUIREMENTS_FILE,
AnsibleConfig,
AnsibleInventory,
AnsibleTool,
Expand All @@ -55,6 +54,7 @@
from devops_cli.infra.repo_content import (
YamlContent,
find_ci_workflows,
find_collection_requirements,
find_node_targets,
find_roles,
read_collection_requirements,
Expand Down Expand Up @@ -249,19 +249,24 @@ def _read_facts(root: Path) -> _Facts:
def _read_collection_roles(tool: AnsibleTool, root: Path) -> tuple[str, ...] | str:
"""The roles of the pinned collections of the repo at `root`, which an install puts in a
temporary folder for their listing; or the reason that the roles row does not list them. A repo
with no `collections/requirements.yml` pins no collection."""
with no collection requirements file pins no collection."""
texts = MESSAGES.infra
if not (root / REQUIREMENTS_FILE).is_file():
requirements = find_collection_requirements(root)
if requirements is None:
return ()
if not (tool.installed and check_binary(tool.argv(DOC_COMMAND)[0])):
return texts.roles_no_doc
with tempfile.TemporaryDirectory(prefix=_COLLECTIONS_PREFIX) as folder:
roles = _attempt(lambda: _install_and_list(tool, root, Path(folder)))
roles = _attempt(
lambda: _install_and_list(tool, root, Path(folder), requirements=requirements)
)
return roles if isinstance(roles, tuple) else texts.roles_unread.format(reason=roles)


def _install_and_list(tool: AnsibleTool, root: Path, folder: Path) -> tuple[str, ...]:
install_requirements(tool, root, folder)
def _install_and_list(
tool: AnsibleTool, root: Path, folder: Path, requirements: Path | None = None
) -> tuple[str, ...]:
install_requirements(tool, root, folder, requirements=requirements)
return read_collection_roles(tool, root, folder)


Expand Down
10 changes: 5 additions & 5 deletions src/devops_cli/infra/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
from devops_cli.infra.access import AgentAccess, require_agent_level
from devops_cli.infra.ad_hoc import read_job_events, tool_env
from devops_cli.infra.ansible_facts import (
REQUIREMENTS_FILE,
RUNNER_COMMAND,
AnsibleInventory,
AnsibleTool,
Expand All @@ -84,6 +83,7 @@
read_inventory,
)
from devops_cli.infra.onboard import OnboardTarget
from devops_cli.infra.repo_content import find_collection_requirements
from devops_cli.infra.repo_tree import export_commit, render_read
from devops_cli.lang import ERRORS, MESSAGES
from devops_cli.models.infra import (
Expand Down Expand Up @@ -465,21 +465,21 @@ def collections_folder(requirements: Path) -> Path:

def install_collections(tool: AnsibleTool, project: Path) -> Path | None:
"""The folder of the pinned collections of the repo in `project`, installed once for each
version of `collections/requirements.yml`; none when the repo has no such file.
version of its collection requirements file; none when the repo has no such file.

Raises:
InfraReadError: the install fails.
"""
requirements = project / REQUIREMENTS_FILE
if not requirements.is_file():
requirements = find_collection_requirements(project)
if requirements is None:
return None
target = collections_folder(requirements)
if target.is_dir():
return target
target.parent.mkdir(parents=True, exist_ok=True, mode=_PRIVATE)
staging = Path(tempfile.mkdtemp(prefix=_INSTALL_PREFIX, dir=target.parent))
try:
install_requirements(tool, project, staging)
install_requirements(tool, project, staging, requirements=requirements)
staging.rename(target)
finally:
shutil.rmtree(staging, ignore_errors=True)
Expand Down
8 changes: 7 additions & 1 deletion src/devops_cli/infra/repo_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,15 @@ def find_roles(root: Path, roles_path: tuple[Path, ...]) -> tuple[str, ...]:
return tuple(sorted(roles))


def find_collection_requirements(root: Path) -> Path | None:
"""The collection requirements file of the repository (requirements.yml or requirements.yaml)."""
return next((root / name for name in _REQUIREMENTS_FILES if (root / name).is_file()), None)


def read_collection_requirements(root: Path) -> CollectionRequirements:
"""The collections file of the repository, and each collection it names with no exact
version."""
path = next((root / name for name in _REQUIREMENTS_FILES if (root / name).is_file()), None)
path = find_collection_requirements(root)
if path is None:
return CollectionRequirements(path=None)
relative = path.relative_to(root).as_posix()
Expand Down Expand Up @@ -275,6 +280,7 @@ def _is_node_target(name: str) -> bool:
"CollectionRequirements",
"YamlContent",
"find_ci_workflows",
"find_collection_requirements",
"find_node_targets",
"find_roles",
"read_collection_requirements",
Expand Down
19 changes: 19 additions & 0 deletions tests/commands/test_infra_onboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,22 @@ def test_a_relative_base_dir_resolves_under_the_main_worktree(
assert onboard.default_checkout(RepoRef("owner/name"), settings) == (
tmp_path / "repos" / "owner" / "name"
)


def test_collections_row_and_install_agree_on_requirements_yaml(tmp_path: Path) -> None:
"""The collections row and the requirements helper agree on `collections/requirements.yaml`."""
from devops_cli.infra.repo_content import (
find_collection_requirements,
read_collection_requirements,
)

colls = tmp_path / "collections"
colls.mkdir()
yaml_file = colls / "requirements.yaml"
yaml_file.write_text("collections:\n - name: community.general\n", encoding="utf-8")

reqs = read_collection_requirements(tmp_path)
found = find_collection_requirements(tmp_path)

assert reqs.path == "collections/requirements.yaml"
assert found == yaml_file
Loading