Use changed_when: false
changed_when in an ad-hoc command.✅ failed_when: false
Meaning
No matter what happens (even if the command returns a non-zero exit status), Ansible will NOT mark the task as failed.
Use case
Useful when:
-
A command normally returns a non-zero code but it should NOT be treated as an error.
-
You want to continue even if the command fails.
Example (Oracle user check)
🔍 If id oracle fails because user doesn’t exist, the play continues.
or
- name: 💾 Check Free Disk Space
hosts: webservers
tasks:
- name: Get /var filesystem usage
ansible.builtin.command: df -h /var
register: disk_usage_result # 1. Register the command output
changed_when: false # 2. Tell Ansible this task never changes the system
This makes sure Ansible does not treat the command as a change to the system.
Even if the command runs successfully, Ansible reports:
instead of:
✅ changed_when: false
Meaning
Even if the command makes changes or returns something unusual, Ansible will NOT mark the task as changed.
Use case
Used for:
-
Read-only tasks (checks, queries)
-
Idempotency — run every time but never show “CHANGED”
Example
🔍 This is a check-only task. It should NEVER show as “changed”.
5) Ansible facts
1. The Role of the Gathering Facts Task
Ansible automatically collects information about the remote host at the beginning of every play by default.
When you run your playbook, the first output you see is usually:
TASK [Gathering Facts] ******************************************** ok: [hostname]This task uses the
setupmodule internally to query the remote system for hundreds of pieces of information, including network interfaces, memory, disk space, and, most importantly, operating system details.
2. Fact Variables
The information collected during the "Gathering Facts" task is stored in a dictionary named ansible_facts. ansible_ (like ansible_os_family, ansible_distribution, ansible_default_ipv4) are readily available for use in any task that follows the fact-gathering step.
In your conditional example:
Ansible looks up the value of the ansible_os_family key inside the ansible_facts dictionary that was automatically created, and uses that value to evaluate the condition. You don't need to explicitly call the setup module because it runs automatically.
Controlling Fact Gathering
If you wanted to manually control when facts are gathered (e.g., to speed up a very short playbook), you could disable the automatic gathering at the play level and then run the setup module manually later:
---
- name: Conditional demo
hosts: all
gather_facts: false # Turns off automatic fact gathering
tasks:
- name: Explicitly gather facts
ansible.builtin.setup:
filter: ansible_os_family # Only gather this specific fact to save time
# ... your tasks can now use ansible_os_family ...6. The PLAY RECAP ok= counter includes every single task that executed and did not fail. It combines the tasks that were marked ok (green) AND the tasks that were marked changed (yellow).
No comments:
Post a Comment