BacklogZero®

Ansible in a Minute · Ansible Core ·

Use changed_when to mark when a task changes the system

Short answer

Use changed_when to report changed only when the task actually changed the system.

Request

mark a command task as changed only when stdout contains created

Example

A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.

YAML
---
- name: Demonstrate changed_when
  hosts: localhost
  gather_facts: false
  connection: local

  tasks:
    - name: Run a command and register output
      ansible.builtin.command:
        cmd: echo "status=created"
      register: cmd_result
      changed_when: "'created' in cmd_result.stdout"
      failed_when: false

How it works

Many command modules report changed on every run. Use changed_when so handlers and reports reflect real change.

When to use it

  • Preventing unnecessary handler runs
  • Accurate change reporting for CLI tools

Requirements

  • Ansible: >=2.14

Validation

  • Syntax validated
  • Not execution-tested
  • Last verified 2026-09-03

Notes

Set changed_when: false when a task is read-only. Set a condition when change depends on command output.

Try it with BacklogZero

Ask BacklogZero to generate this automation for your environment.

Get BacklogZero today

Related guides

Back to Ansible in a Minute

Use changed_when to mark when a task changes the system · Level Up Labs