Skip to content

fix(infra): apply runs disruptive hosts in name order #7366

Done

Description

@dan-petty

Problem or Motivation

devops infra apply runs each disruptive host of a plan alone, "in plan order", and the plan lists its hosts in name order. So a play whose host order matters does not run in the order of the play.

The case that shows it: the K3s upgrade play of dan-petty/homelab (ansible/playbooks/07_cluster_upgrade.yaml, hosts: k3s_server:k3s_agent, serial: 1) upgrades the server node first and then each agent. The server host of that inventory sorts after four agent hosts by name. An apply of that play then changes the agents before the server, and each agent gets a newer kubelet than the API server, which the version skew policy of Kubernetes forbids. make upgrade (plain ansible-playbook) keeps the play order.

Evidence

  • src/devops_cli/infra/plan.py: the host plans are built with for host in sorted(hosts).
  • src/devops_cli/infra/apply.py, _apply_hosts: alone = [host for host in plan.hosts if host.disruptive], then one run for each of them in that order. Its docstring says "each disruptive host alone in plan order".
  • Read from the code on 2026-11-27. Not run: no apply of the upgrade play ran for this record.
  • dan-petty/homelab pull request 439 adds a stop to the play: an agent does not change while the server node does not report k3s_version. With it, an apply in name order stops at the first agent and changes nothing. It does not make the apply work.

Proposed Solution & Architectural Design

Run the disruptive hosts in the order in which the play runs them: the order that ansible-playbook --list-hosts gives for the play and its limit. Keep name order only as the order of the report.

Acceptance Criteria

  • For a play with hosts: <group a>:<group b>, the apply runs a disruptive host of group a before a disruptive host of group b, whatever their names are.
  • A test with a fake inventory holds the order: a server host whose name sorts last runs first.
  • The plan report says in which order an apply runs the hosts.

Target Subsystem

Infrastructure repositories (devops infra)

Existing Tool Considered

ansible-playbook --list-hosts and ansible-inventory give the host order of a play. The change needs no new tool.

Alternatives check of 2026-11-27: in-use.
The repository already uses this tool.
Tool: ansible/ansible (https://github.com/ansible/ansible), GPL-3.0, last activity 2026-11-23, maintained.
Reason: Searches surfaced no tool that orders a per-host disruptive apply; Ansible's own --list-hosts already yields a play's host order, but the apply loop, the serial sequencing and the report are the project's code.

Covers:

  • --list-hosts reports a play's hosts in play and group order
  • Group order such as k3s_server:k3s_agent is preserved
  • A plain run applies hosts in that order with serial

Lacks:

  • No API to feed that order into a per-host apply loop
  • No ordering of disruptive hosts run one at a time
  • No plan report line naming the apply order

Proposed design

Problem statement

devops infra apply runs disruptive hosts in the name order of the saved plan. A play that needs a group order, such as a K3s upgrade with servers before agents, runs agents first and can break version skew. The fix makes the plan read the play host order and list disruptive hosts in that order. The report table stays name sorted and the report states the apply order.

Acceptance criteria

Requirement: Host order source

The plan SHALL read the host order from ansible-playbook --list-hosts with the playbook, each inventory source, the limit and the tags.

Scenario: Limit and tags reach list-hosts

  • WHEN The saved plan has limit k3s_server:k3s_agent and tags upgrade.
  • THEN The captured --list-hosts command has the playbook path, each inventory source, --limit k3s_server:k3s_agent and --tags upgrade.

Requirement: Play order in plan

The plan SHALL list each disruptive host in the play host order, so a host of an earlier group comes before a host of a later group.

Scenario: Server host sorts last but runs first

  • WHEN A fake inventory has group a with host zeta and group b with host alpha, the play uses hosts: a:b, and both hosts are disruptive.
  • THEN The plan lists zeta before alpha as disruptive hosts, although alpha sorts first by name.

Requirement: Apply uses plan order

The apply SHALL run each disruptive host alone in the order of plan.hosts.

Scenario: Disruptive host order

  • WHEN A saved plan lists disruptive hosts zeta then alpha and non-disruptive host beta.
  • THEN The apply runs zeta alone, then alpha alone, and runs beta with the non-disruptive hosts.

Requirement: Report table name order

The plan report's host table SHALL list hosts in name order.

Scenario: Table stays name sorted

  • WHEN The plan host list is zeta then alpha.
  • THEN The host table rows are alpha then zeta.

Requirement: Report apply order

The plan report SHALL state the order in which an apply runs the disruptive hosts.

Scenario: Report line names apply order

  • WHEN The plan host list is zeta then alpha and both hosts are disruptive.
  • THEN The rendered report has one line that names zeta before alpha as the apply order.

Requirement: Host-order failure

The plan MUST stop with an error when ansible-playbook --list-hosts fails, so no plan with no host order can apply.

Scenario: List-hosts failure

  • WHEN The --list-hosts command exits nonzero.
  • THEN The plan command exits 1 with an error that names the host-order read and saves no plan.

Existing Tool Considered

The alternatives check of 2026-11-27 in the issue body gives the verdict in-use for ansible/ansible (https://github.com/ansible/ansible). The repository already uses the tool. The check in the issue body lists what the tool covers and lacks. The proposal adds a small parser for the --list-hosts output, orders the plan hosts with it, and adds the report line. It builds no new host-order engine.

Key questions

  • Which source gives the host order of a play?: Use ansible-playbook --list-hosts with the plan's playbook, inventory sources, limit and tags. Do not parse the playbook YAML or keep name order. (best practice) [sources: src/devops_cli/infra/plan.py, src/devops_cli/infra/apply.py]
    • Chosen: ansible-playbook --list-hosts. The tool reports the play and group order, and the repository already uses Ansible.
    • Rejected: Parse playbook YAML. It duplicates Ansible's pattern and group resolution and fails on dynamic inventory.
    • Rejected: Keep name order. It causes the version-skew bug that the issue records.
  • Does the plan record keep name order or play order?: Keep play order in plan.hosts, because the apply uses that list. Sort the report host table by name. (best practice) [sources: src/devops_cli/infra/plan.py, src/devops_cli/infra/apply.py]
    • Chosen: Play order in plan.hosts. The apply already runs plan.hosts in order, and the report can sort its own table by name.
    • Rejected: Name order in plan.hosts with a separate apply-order field. It adds a second order to keep consistent and lets a plan hold two different host orders.
    • Rejected: Name order in plan.hosts and re-read order in apply. It makes the apply read the repo again and can disagree with the plan report.
  • How does the plan report show both name order and apply order?: Keep the host table in name order and add one line to the report footer that lists the apply order. (best practice) [sources: src/devops_cli/infra/plan.py]
    • Chosen: Footer line for apply order. It keeps the existing table stable and shows the order in one place.
    • Rejected: Apply-order column in the host table. It repeats the order on each row and makes the table wider.
    • Rejected: No report change. The issue requires the report to state the apply order.
  • What does the plan do when --list-hosts fails?: The plan ends failed with an error that names the host-order read. It saves no plan. (best practice) [sources: src/devops_cli/infra/plan.py, src/devops_cli/infra/apply.py]
    • Chosen: Fail the plan. A plan with no play order can apply disruptive hosts in the unsafe name order.
    • Rejected: Fall back to name order. It keeps the bug and hides the failed read.
    • Rejected: Ask the operator to give the order. It adds a manual step that the tool can do.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/bug
    on Nov 27, 2026
  2. dan-petty commented on Nov 27, 2026

    @dan-petty
    OwnerAuthor
  3. added
    scope/infra
    source/agent
    status/new
    value/medium
    effort/medium
    priority/p2-medium
    on Nov 27, 2026
  4. devops-cli-service commented on Nov 27, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (6)

    Tools considered:

    Searches run:

    • ansible-inventory --list host order group order playbook, results: 5
    • ansible-playbook --list-hosts output host order of a play, results: 5
    • ansible-core pypi package, results: 5
    • ansible-runner run playbook from python preserve play host order, results: 5
    • ansible playbook command line list hosts docs.ansible.com, results: 5
    • ansible inventory order of hosts groups pattern ansible documentation, results: 5

    Sources:

  5. added
    status/ready
    and removed
    status/new
    on Nov 28, 2026
  6. devops-cli-service commented on Nov 28, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See the section Proposed design of the body.

  7. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 28, 2026
  8. removed
    status/in-review
    owner/agent
    on Nov 28, 2026
  9. devops-cli-service commented on Nov 29, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-29 00:41 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/infrasource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions