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-13
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Design

## Decisions

- `_PLAN_ENV` holds `ANSIBLE_NOCOLOR=True` and `ANSIBLE_FORCE_COLOR=False`. `ansible/utils/color.py` applies `ANSIBLE_FORCE_COLOR` after `ANSIBLE_NOCOLOR`, so `ANSIBLE_NOCOLOR` alone does not turn off the color of a repo whose `ansible.cfg` sets `force_color`. The environment variable replaces that setting of `ansible.cfg`, as `ANSIBLE_CALLBACK_RESULT_FORMAT` replaces `result_format`. `runner_env` adds `_PLAN_ENV` after the allowlist of the process runner, so every run of `Playbook.run` gets it: the plan, the fresh check and the run of an apply. The status ping (`ping_env`) parses no skip result and does not change.
- `_is_check_mode_skip` does not change. A skip result that does not read as JSON stays unchecked: the safe side.
- The tests use a real recording. `tests/fixtures/infra/runner_events/site_color.jsonl` holds the job events of the run `site` of the probe again, with `TERM=xterm-256color` and no `ANSIBLE_NOCOLOR`, as a color terminal gave them before this change. The answer of the fake `ansible-runner` (`terminal_events` in `tests/support/infra.py`) gives that recording to a run whose environment is the environment of the recording, and the recording `site` to every other run. It reads the environment of the run with `build_subprocess_env`, the function that the process runner uses, so the `TERM` of the person reaches the decision as it reaches the real run.
- Acceptance criterion 1 of the issue asks for the same plan "with and without the color codes in `stdout`". The proposed fix of the issue removes the color codes and keeps the safe side for a result that does not read. Thus the test of the plan reader shows that a skip in color stays unchecked, and the test of the apply shows that a plan with no `TERM` and its apply with `TERM=xterm-256color` get the same plan (criteria 1 and 3). The tests of the plan run and of the apply runs find `ANSIBLE_NOCOLOR=True` and `ANSIBLE_FORCE_COLOR=False` in the environment of each `ansible-runner` run (criterion 2).

## Rejected alternatives

- **Remove the color codes in the plan reader.** The cause is the color of the event text, and Ansible has a setting that turns it off. The recorded run with `TERM=xterm-256color` and `ANSIBLE_NOCOLOR=True` holds no escape code and gives the same plan as the run with no `TERM`. A reader of terminal codes is a second fix for the same cause and more code to keep, and the issue asks to keep the safe side for a result that does not read.
- **Set `ANSIBLE_NOCOLOR` in the environment of the caller.** The process runner drops it: `DEFAULT_ALLOWED_ENV_VARS` holds `TERM` and `COLORTERM`, and no allowed name or prefix matches an `ANSIBLE_` variable. The second apply of the owner with `ANSIBLE_NOCOLOR=1` stopped the same way.
- **Remove `TERM` and `COLORTERM` from the environment of the run.** The allowlist of the process runner is shared by every child. A removal for one caller needs a new option of the process runner, and Ansible has its own setting for this.
- **Read the skip result from the event data.** ansible-runner 2.4.3 puts no result in the event data of `runner_on_skipped` (`v2_runner_on_skipped` in `awx_display.py`), so the event `stdout` is the only source.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Proposal: #5352

## Why

`devops infra apply` stops at the fresh check when the plan came from a run with no terminal and the apply runs in a color terminal (#5352). ansible-runner starts `ansible-playbook` in a pseudo-terminal, and the process runner passes `TERM` and `COLORTERM` from the caller. Thus with `TERM=xterm-256color` the default callback puts color codes around the text of each event. The plan reads the skip result after ` => ` in the event `stdout` as JSON. A result in color does not read, so the plan counts the skip as unchecked (the safe side). A skip by a false `when` is then unchecked in a color terminal and not unchecked with no terminal, and the fresh check differs from the plan. Closes #5352.

## What Changes

- Every `ansible-runner` run of a playbook (the plan, the fresh check and the run of an apply) gets `ANSIBLE_NOCOLOR=True` and `ANSIBLE_FORCE_COLOR=False` in its environment. `ANSIBLE_FORCE_COLOR=False` replaces a `force_color` of the repo's `ansible.cfg`, as `ANSIBLE_CALLBACK_RESULT_FORMAT` replaces its `result_format`. The callback then writes the same event text in a color terminal and with no terminal, and the plan of a host is the same in both.
- A skip result that does not read as JSON stays unchecked, as before.
- The change is observable: the plan of a host in a color terminal changes, and `devops infra apply` passes the fresh check that stopped it. Thus it carries a delta of the capability `infra-plan`.

## Capabilities

- `infra-plan`: ADDED requirement "The run output has no color".

## Impact

- Code: `src/devops_cli/infra/plan.py` (`_PLAN_ENV` and its comment, the docstrings of the module and of `runner_env`).
- Tests: `tests/infra/test_plan.py`, `tests/commands/test_infra_plan.py`, `tests/commands/test_infra_apply.py`, `tests/support/infra.py`, and the new recorded run `tests/fixtures/infra/runner_events/site_color.jsonl`.
- A plan record made in a color terminal before this change can show more unchecked tasks than a fresh check after it. Such a plan stops at the fresh check. Make the plan again.
- No setting, command, option or message changes. No manifest changes. The change fits one pull request.

## Feasibility

The premise was confirmed against `origin/main` and the pinned tools (ansible-core 2.22.0, ansible-runner 2.4.3 in the tool environment of `[python.ansible-core]`):

- `_is_check_mode_skip` (`src/devops_cli/infra/plan.py`) returns `True` when `json.loads` of the text after ` => ` fails. `_PLAN_ENV` holds no `ANSIBLE_NOCOLOR`.
- `DEFAULT_ALLOWED_ENV_VARS` (`src/devops_cli/core/process.py`) holds `TERM` and `COLORTERM`, and no allowed name or prefix matches `ANSIBLE_NOCOLOR`. Thus a caller `ANSIBLE_NOCOLOR=1` does not reach the run, as the comment of the owner on the issue says.
- `ansible_runner/runner.py` starts the command with `pexpect`, which gives it a pseudo-terminal. `ansible_runner/display_callback/callback/awx_display.py` (`v2_runner_on_skipped`) puts no result in the event data of a skip, so the event `stdout` is the only source of the skip result.
- `ansible/utils/color.py` turns color off when `ANSIBLE_NOCOLOR` is true. Else it turns color on when `stdout` is a terminal and `curses.setupterm()` finds a terminal with colors. `ANSIBLE_NOCOLOR` is the setting `nocolor` of `ansible.cfg`, also read from `NO_COLOR` (`ansible/config/base.yml`).
- Three check-mode runs of the probe `tests/fixtures/infra/runner_events/probe/` on all hosts, as `tests/support/infra.py` says:
- With no `TERM`: 0 of 65 events hold an escape code. node-1 has 1 unchecked task and node-3 has none.
- With `TERM=xterm-256color`: 21 events hold escape codes, and a skip has the `stdout` `\x1b[0;36mskipping: [node-3.example.com] => {"changed": false, "false_condition": ...}\x1b[0m`. node-1 has 2 unchecked tasks (`Remove the old demo unit` too) and node-3 has 1 (`Download and execute K3s agent installation script`).
- With `TERM=xterm-256color` and `ANSIBLE_NOCOLOR=True`: 0 events hold an escape code, and the plan of each host is equal to the plan of the run with no `TERM`.
- `ansible/utils/color.py` applies `ANSIBLE_FORCE_COLOR` after `ANSIBLE_NOCOLOR`, and `force_color` turns color on also with no terminal. An environment variable replaces the same setting of `ansible.cfg`: with `force_color = True` in `ansible.cfg` and `ANSIBLE_FORCE_COLOR=False` in the environment, `ansible-config dump --only-changed` shows `ANSIBLE_FORCE_COLOR(env: ANSIBLE_FORCE_COLOR) = False`. A check-mode run of a playbook with one skip by a false `when`, with `TERM=xterm-256color` and that `ansible.cfg`, gives the skip `stdout` `\x1b[0;36mskipping: [localhost] => {"false_condition": false}\x1b[0m` with `ANSIBLE_NOCOLOR=True` only, and `skipping: [localhost] => {"false_condition": false}` with `ANSIBLE_FORCE_COLOR=False` too.

## Existing Tool Considered

Ansible has the setting `ANSIBLE_NOCOLOR` (`nocolor` in `ansible.cfg`). The change sets it and adds no new tool or library. It deletes no internal code.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## ADDED Requirements

### Requirement: The run output has no color
Every `ansible-runner` run of a playbook, the plan, the fresh check and the run of an apply, SHALL get `ANSIBLE_NOCOLOR=True` and `ANSIBLE_FORCE_COLOR=False` in its environment, also when the repo's `ansible.cfg` sets `force_color`, so the event text and the plan of a host are the same in a color terminal and with no terminal. A skip result that does not read as JSON, such as a result with color codes, SHALL stay unchecked.

#### Scenario: The environment of the plan run
- **WHEN** a person plans `ansible/playbooks/03_k3s.yaml`
- **THEN** `ansible-runner` gets `ANSIBLE_NOCOLOR=True` and `ANSIBLE_FORCE_COLOR=False`

#### Scenario: A plan with no terminal and its apply in a color terminal
- **WHEN** a person plans node-3 with no `TERM`, and applies the plan with `TERM=xterm-256color`
- **THEN** each `ansible-runner` run gets `ANSIBLE_NOCOLOR=True` and `ANSIBLE_FORCE_COLOR=False`, the fresh check of node-3 equals the plan, and the apply exits 0

#### Scenario: A skip result in color
- **WHEN** the recorded run in a color terminal skips a task of node-1 and a task of node-3 by a false `when`, with the color codes around the text of each skip
- **THEN** each of the two tasks is unchecked, and the rest of the plan of each host equals the plan of the run with no terminal
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Tasks

## 1. Turn off the color of every run of a playbook

Kind: behavior. Reads: `src/devops_cli/infra/plan.py`, `src/devops_cli/core/process.py`, `tests/support/infra.py`. Changes: `src/devops_cli/infra/plan.py`, `tests/support/infra.py`, `tests/fixtures/infra/runner_events/site_color.jsonl`, `tests/commands/test_infra_plan.py`, `tests/commands/test_infra_apply.py`, `tests/infra/test_plan.py`.

- [x] 1.1 Record the run `site` of the probe again with `TERM=xterm-256color` and no `ANSIBLE_NOCOLOR` as `tests/fixtures/infra/runner_events/site_color.jsonl`, and say how in `tests/support/infra.py`. Add `terminal_events` to `tests/support/infra.py`: it answers a run with the recording `site_color` when the environment of the run is the environment of that recording, else with the recording `site`. Add `ANSIBLE_NOCOLOR=True` to the environment that `test_the_run_is_check_mode_on_the_limit_without_offline_hosts_with_the_key` expects
- Verification: `$ uv run devops ci test tests/commands/test_infra_plan.py` fails on the base
- [x] 1.2 Add `test_a_plan_with_no_terminal_passes_the_fresh_check_of_its_apply_in_a_color_terminal` to `tests/commands/test_infra_apply.py`: the plan of node-3 runs with no `TERM`, the apply runs with `TERM=xterm-256color`, the apply exits 0 with each step of node-3, and each `ansible-runner` run gets `ANSIBLE_NOCOLOR=True`
- Verification: `$ uv run devops ci test tests/commands/test_infra_apply.py` fails on the base
- [x] 1.3 Add `test_a_skip_result_in_color_stays_unchecked` to `tests/infra/test_plan.py`: in the recording `site_color`, the two skips by a false `when` are unchecked, and the rest of each host plan equals the plan of the recording `site`
- Verification: `$ uv run devops ci test tests/infra/test_plan.py` passes on the base and after the change
- [x] 1.4 Add `ANSIBLE_NOCOLOR=True` to `_PLAN_ENV` in `src/devops_cli/infra/plan.py`, and say why in the docstrings of the module and of `runner_env`
- Verification: `$ uv run devops ci test tests/commands/test_infra_plan.py tests/commands/test_infra_apply.py tests/infra/test_plan.py` passes
- [x] 1.5 Expect `ANSIBLE_FORCE_COLOR=False` beside `ANSIBLE_NOCOLOR=True` in the tests of 1.1 and 1.2, then add it to `_PLAN_ENV`, so a `force_color` of the repo's `ansible.cfg` does not turn the color on again
- Verification: `$ uv run devops ci test tests/commands/test_infra_plan.py tests/commands/test_infra_apply.py` fails before the change of `_PLAN_ENV` and passes after it

- Pending a person: review the diff in tuicr (tuicr -r origin/main..HEAD).
15 changes: 15 additions & 0 deletions openspec/specs/infra-plan/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,18 @@ The command SHALL print a title, the commit that it read, a table of hosts, a ta
#### Scenario: A dry run of a branch
- **WHEN** an agent runs the command with `--ref fix/k3s --dry-run` on a repo at `none`
- **THEN** the command exits 0, starts no process, and lists the requests in order

### Requirement: The run output has no color
Every `ansible-runner` run of a playbook, the plan, the fresh check and the run of an apply, SHALL get `ANSIBLE_NOCOLOR=True` and `ANSIBLE_FORCE_COLOR=False` in its environment, also when the repo's `ansible.cfg` sets `force_color`, so the event text and the plan of a host are the same in a color terminal and with no terminal. A skip result that does not read as JSON, such as a result with color codes, SHALL stay unchecked.

#### Scenario: The environment of the plan run
- **WHEN** a person plans `ansible/playbooks/03_k3s.yaml`
- **THEN** `ansible-runner` gets `ANSIBLE_NOCOLOR=True` and `ANSIBLE_FORCE_COLOR=False`

#### Scenario: A plan with no terminal and its apply in a color terminal
- **WHEN** a person plans node-3 with no `TERM`, and applies the plan with `TERM=xterm-256color`
- **THEN** each `ansible-runner` run gets `ANSIBLE_NOCOLOR=True` and `ANSIBLE_FORCE_COLOR=False`, the fresh check of node-3 equals the plan, and the apply exits 0

#### Scenario: A skip result in color
- **WHEN** the recorded run in a color terminal skips a task of node-1 and a task of node-3 by a false `when`, with the color codes around the text of each skip
- **THEN** each of the two tasks is unchecked, and the rest of the plan of each host equals the plan of the run with no terminal
14 changes: 11 additions & 3 deletions src/devops_cli/infra/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
once for each version of the file, and runs `ansible-runner run <folder> --playbook <playbook>`
with `--check --diff -v` and the limit `<limit>:!<offline group>`. The run gets the repo's
`ansible.cfg`, the key of `devops ssh` as `ansible_ssh_private_key_file`, host key checks on, and
the environment of the process runner, so no `ANSIBLE_` variable of the person reaches it.
the environment of the process runner, so no `ANSIBLE_` variable of the person reaches it. The
`TERM` of the person reaches it, and ansible-runner gives the run a pseudo-terminal, so in a color
terminal the default callback puts color codes around the text of each event. Every run sets
`ANSIBLE_NOCOLOR`, and `ANSIBLE_FORCE_COLOR=False` over a `force_color` of the repo's `ansible.cfg`
(#5352): the event text, and so the plan, is the same in a color terminal and with no terminal.

The job events (`artifacts/<ident>/job_events/*.json`) give the plan of each host, with no read of
the repo's YAML:
Expand Down Expand Up @@ -115,11 +119,15 @@
# that changes a file with a secret do not hold its text.
_RUN_OPTIONS: Final[tuple[str, ...]] = ("-v",)
# What the run adds to the environment of `ping_env`: the rendered arguments in each result, the
# results as JSON, and a line for each skipped host.
# results as JSON, a line for each skipped host, and no color, also when the repo's `ansible.cfg`
# sets `force_color`. A skip result in color does not read as JSON, and the plan counts it as
# unchecked (#5352).
_PLAN_ENV: Final[dict[str, str]] = {
"ANSIBLE_INJECT_INVOCATION": "True",
"ANSIBLE_CALLBACK_RESULT_FORMAT": "json",
"ANSIBLE_DISPLAY_SKIPPED_HOSTS": "True",
"ANSIBLE_NOCOLOR": "True",
"ANSIBLE_FORCE_COLOR": "False",
}

_EVENT_OK: Final[str] = "runner_on_ok"
Expand Down Expand Up @@ -414,7 +422,7 @@ def _config_env(project: Path) -> dict[str, str]:
def runner_env(tool: AnsibleTool, project: Path, collections: Path | None) -> dict[str, str]:
"""The environment that the run adds: the tool environment first on PATH, host key checks on,
the repo's `ansible.cfg`, the rendered arguments in each result, the results as JSON, a line
for each skipped host, and the pinned collections."""
for each skipped host, no color, and the pinned collections."""
env = ping_env(tool) | _PLAN_ENV | _config_env(project)
if collections is not None:
env["ANSIBLE_COLLECTIONS_PATH"] = str(collections)
Expand Down
Loading