MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Ansible Tutorials: Ansible Workflow

Ansible playbooks can be run in two ways: check mode (--check), which reports what would change without changing anything, and run mode, which applies the changes. This page covers both and adds what you need to use them well: a safe step-by-step workflow, diff mode, per-task check mode control and the common reasons a dry run differs from a real run.

Ansible Check Mode

Ansible workflow cycle: update playbook or role, run Ansible in check mode, then run Ansible

Ansible Check Mode: When you run an Ansible playbook or command in check mode, it’s also known as “dry-run” mode. In this mode, Ansible performs a simulation of the tasks specified in the playbook without making any actual changes to the target systems. This allows you to preview what changes would be made if the playbook were run normally.

To run Ansible in check mode, you use the --check or -C flag with the ansible-playbook command or any other Ansible command. For example:

This is useful to verify the impact of a playbook before applying changes to production systems. It helps prevent unintended consequences.

ansible-playbook my_playbook.yml --check
Code language: CSS (css)
Flowchart that runs the task while list items remain or the condition is met, otherwise stops

Ansible Run Mode:

Ansible run mode refers to the standard way of executing Ansible playbooks or commands, where tasks specified in the playbook are executed against the target systems. When you run a playbook without the --check flag, Ansible applies the defined tasks to the target hosts, bringing their state in line with what’s specified in the playbook.

This mode is used to apply actual changes, configurations, installations, and other tasks to the target systems based on the playbook’s instructions.

ansible-playbook my_playbook.yml
Code language: CSS (css)

A safe Ansible workflow from edit to apply

Check mode and run mode are the two main stages, but a reliable workflow has a few cheap checks before them. Each step catches a different kind of mistake before it reaches a real server.

  1. Syntax check: ansible-playbook -i inventory.ini site.yml --syntax-check parses the playbook and stops on YAML or structure errors. It does not connect to any host.
  2. Lint (optional, separate tool): ansible-lint site.yml flags deprecated syntax, missing names, risky patterns and similar problems.
  3. Confirm the targets: --list-hosts shows which hosts the play matches and --list-tasks shows which tasks will run. Use these to catch a wrong inventory group or tag before anything executes.
  4. Dry run: --check --diff connects to the hosts, gathers facts and reports what would change, with line-by-line differences for files.
  5. Limited real run: apply to one host or a small group first with --limit.
  6. Full run: run without --check, then run again to confirm the playbook is idempotent. The second run should report changed=0.
ansible-playbook -i inventory.ini site.yml --syntax-check
ansible-playbook -i inventory.ini site.yml --list-hosts --list-tasks
ansible-playbook -i inventory.ini site.yml --check --diff
ansible-playbook -i inventory.ini site.yml --limit web1
ansible-playbook -i inventory.ini site.ymlCode language: CSS (css)

Check, diff and run modes compared

ModeFlagChanges hosts?What it shows
Check mode--check / -CNoWhich tasks would report changed
Diff mode--diff / -DYes, unless combined with --checkBefore/after content of changed files and templates
Check + diff--check --diffNoWhich tasks would change and exactly what would change
Run modenoneYesThe actual result of each task

--diff on its own does not make a run safe. It only adds detail to the output. Combine it with --check for a dry run. Both flags also work with ad hoc commands, for example:

ansible web -i inventory.ini -b -m ansible.builtin.copy \
  -a "src=motd dest=/etc/motd mode=0644" --check --diffCode language: JavaScript (javascript)

What check mode actually does

In check mode Ansible still connects to every host, gathers facts and evaluates each task. Modules that support check mode work out whether a change is needed and report changed or ok without applying it. Modules that do not support check mode report the task as skipped.

  • ansible.builtin.command, ansible.builtin.shell and ansible.builtin.raw do not run in check mode, because Ansible cannot predict what an arbitrary command would do. With command, the creates and removes options let Ansible report whether the command would run.
  • Handlers that a task notifies also run in check mode, so they only report changes too.
  • Facts are real, because they come from the actual hosts, so when conditions based on facts are evaluated correctly.

Typical recap output from a dry run against a host that needs changes:

PLAY RECAP *********************************************************
web1 : ok=4  changed=2  unreachable=0  failed=0  skipped=1  rescued=0  ignored=0

Here changed=2 means two tasks would modify the host in a real run, and skipped=1 is often a command or shell task that check mode could not simulate.

Controlling check mode per task

The check_mode keyword overrides the global behaviour for a single task, block or play, and the ansible_check_mode variable tells a task whether the run is a dry run. The playbook below uses both.

---
- name: Configure web servers
  hosts: web
  become: true
  tasks:
    - name: Read current app version (read-only, safe to run in check mode)
      ansible.builtin.command: cat /opt/app/VERSION
      register: app_version
      changed_when: false
      check_mode: false        # always runs, even with --check

    - name: Install nginx
      ansible.builtin.package:
        name: nginx
        state: present

    - name: Deploy nginx config
      ansible.builtin.template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
        mode: "0644"
      notify: Restart nginx

    - name: Ensure nginx is running
      ansible.builtin.service:
        name: nginx
        state: started
        enabled: true
      # nginx is not really installed during a dry run on a new host
      ignore_errors: "{{ ansible_check_mode }}"

    - name: Show version only in real runs
      ansible.builtin.debug:
        msg: "App version {{ app_version.stdout }}"
      when: not ansible_check_mode

  handlers:
    - name: Restart nginx
      ansible.builtin.service:
        name: nginx
        state: restartedCode language: PHP (php)
  • check_mode: false runs the task for real even under --check. Use it only for read-only tasks whose output later tasks need.
  • check_mode: true forces a task to only simulate, even in a normal run. This is useful for tasks you want to report on but never apply.
  • ignore_errors: "{{ ansible_check_mode }}" stops a dry run from failing on a task that depends on an earlier change that was only simulated.
  • changed_when: false stops read-only commands from showing as changes.

Running for real without surprises

Run mode applies every task, so limit the impact of a mistake. These options are part of ansible-playbook and combine with each other and with --check.

OptionPurpose
--limit web1 or --limit 'web:!web3'Restrict the run to some hosts in the play
--tags nginx / --skip-tags rebootRun or skip only tagged tasks
--stepConfirm each task interactively before it runs
--start-at-task "Deploy nginx config"Resume from a specific task after a failure
serial: 1 (play keyword)Roll changes out one host (or batch) at a time
-v to -vvvvIncrease output detail when debugging

Note: --diff prints file contents to the console and to any CI logs. For templates containing passwords or keys, set diff: false on the task, or use no_log: true, so secrets are not exposed.

Common problems and fixes

Symptom in check modeCauseFix
'dict object' has no attribute 'stdout'A command/shell task was skipped, so its registered variable has no outputAdd check_mode: false if the command is read-only, or guard later tasks with when: not ansible_check_mode
Could not find the requested serviceThe package install was only simulated, so the service does not exist yetignore_errors: "{{ ansible_check_mode }}" on the service task
Dry run shows changed=0 but the real run changes thingsChanges are made by skipped command/shell tasksReplace commands with idempotent modules where possible, or add creates/removes
Every run reports changed for the same taskThe task is not idempotentUse the correct module, or set changed_when to a real condition

Corrections to the original

  • In the check mode section above, the sentence "For example:" is followed by another paragraph rather than the command. The example it refers to is ansible-playbook my_playbook.yml --check (or ansible-playbook my_playbook.yml -C).
  • The original describes check mode as a preview of the changes. It is a close approximation, not a guarantee: tasks whose modules do not support check mode (including command and shell) are skipped, and tasks that depend on simulated earlier changes can fail or report differently. Use --check --diff together, and treat a dry run as one safety step alongside --limit and staged rollouts.

Frequently asked questions

What is the difference between –check and –diff in Ansible?

--check stops Ansible from making changes and reports what would change. --diff shows the before and after content of changed files. Used alone, --diff still applies changes, so use --check --diff for a detailed dry run.

Do shell and command tasks run in Ansible check mode?

No. command, shell and raw tasks are skipped in check mode unless you set check_mode: false on the task. With command, the creates or removes options let Ansible report whether the command would run.

How can a playbook tell whether it is running in check mode?

Use the built-in ansible_check_mode variable, which is true during a --check run. It is commonly used in when: not ansible_check_mode or ignore_errors: "{{ ansible_check_mode }}".

Is Ansible check mode 100% accurate?

No. It is reliable for modules that support it, but skipped tasks and tasks that depend on simulated changes can make the dry run differ from the real run. Follow it with a limited real run on one host.

Can I make a task always run, even in check mode?

Yes. Set check_mode: false on the task. Do this only for read-only tasks, because the task will really execute during a dry run.

Related tutorials

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Ansible Training Flow

This page walks through the Ansible training flow step by step. It starts with ad hoc commands, moves on to playbooks and finishes with roles. At each…

Read More

Ansible Playbook for Zabbix Agent Deployment

This page shows an Ansible playbook that installs and configures Zabbix agent 6.0 on CentOS and RHEL hosts. Below the original playbook you will find a corrected,…

Read More

How Ansible Automates Cisco IOS and IOS XE Network Devices

Introduction Managing multiple Cisco routers and switches manually can become time-consuming as a network grows. Network engineers regularly configure interfaces, check device status, update settings, and troubleshoot…

Read More

Configuration Management in DevOps: Concepts, Tooling, and a Practical Workflow

This guide explains how configuration management works in modern DevOps teams, compares common tools, and shows a practical Ansible example. It also preserves the original SlideShare reference from the legacy post.

Read More

Configuration Management in DevOps: Workflow, Tool Comparison, and an Ansible Example

This guide explains configuration management for DevOps teams: what it solves, how it fits into delivery workflows, and how major tool options compare. It also includes a practical Ansible example you can adapt for real environments.

Read More

Infrastructure as Code: A Practical Guide for DevOps Professionals

Introduction Managing infrastructure has undergone a massive transformation over the past decade. If you started your career in the era of physical data centers, you likely remember…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x