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.
Subscribe for updates
Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.
By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.
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.
---
- name: Mark a command task as changed only when stdout contains "created"
hosts: all
gather_facts: true
tasks:
- name: Execute command and mark as changed if stdout contains "created"
ansible.builtin.command: echo "This is a test command"
register: command_result
changed_when: "'created' in command_result.stdout"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
- Execution passed
- 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.
Reference
- Defining changed (Ansible docs)
Built for every Ansible use case in every organization
BacklogZero can generate this kind of automation in your environment, just like it did here.