Ansible Playbook Dry Run Run Playbook in Check Mode

Ansible is an open-source IT automation tool used for configuration management, application deployment, and task automation. It allows system administrators to manage a large number of servers with ease. Ansible Playbook is a powerful feature that allows users to automate tasks and manage configurations through YAML files.

When creating a playbook, it's important to test and validate it before executing it on production systems. Ansible offers a Dry Run feature to run a playbook in check mode, which enables users to simulate execution without making any actual changes. This validation step is crucial for maintaining system stability and avoiding unintended modifications.

What is a Dry Run in Ansible?

A Dry Run is a simulation of playbook execution that checks if the playbook is valid and would have the intended effect if executed. It validates your playbook without making any actual changes to target systems.

When running a playbook in dry run mode, Ansible performs the same checks it would during regular execution, but displays what changes would have been made instead of applying them. This feature is particularly useful for ensuring playbooks are error-free and won't cause unintended system modifications.

How to Run a Playbook in Check Mode

Running a playbook in check mode is straightforward. Use the --check flag with the ansible-playbook command to enable dry run mode

ansible-playbook playbook.yml --check

If the playbook contains no errors, Ansible displays a success message. If errors exist, Ansible shows detailed error messages indicating what went wrong.

Essential Flags for Check Mode

Using --diff Flag

The --diff flag displays differences between the current system state and the state that would be achieved after playbook execution

ansible-playbook playbook.yml --check --diff

This flag is invaluable for debugging playbooks and identifying potential unintended changes.

Using --list-tasks Flag

The --list-tasks flag shows all tasks that would be executed without actually running them

ansible-playbook playbook.yml --check --list-tasks

Using --limit Flag

The --limit flag restricts playbook execution to specific systems, useful for testing on a subset of infrastructure

ansible-playbook playbook.yml --check --limit server1,server2

Using --tags and --skip-tags Flags

Execute only specific tagged tasks with --tags

ansible-playbook playbook.yml --check --tags web,database

Skip specific tagged tasks with --skip-tags

ansible-playbook playbook.yml --check --skip-tags maintenance

Key Benefits

Benefit Description
Safe Testing Validate playbooks without risking system changes
Early Error Detection Catch syntax and logic errors before production deployment
Compliance Validation Ensure playbooks meet organizational compliance requirements
Change Preview See exactly what modifications would be applied
Time Efficiency Avoid rollbacks and system repairs from failed deployments

Best Practices

  • Always dry run first Make it a standard practice before any production deployment.

  • Combine flags effectively Use --check --diff --limit for comprehensive testing on specific hosts.

  • Test incrementally Use --tags to test individual components of complex playbooks.

  • Verify output carefully Review all proposed changes, especially file modifications and service restarts.

Conclusion

Ansible's dry run feature through check mode is essential for safe automation practices. It allows administrators to validate playbooks, preview changes, and catch errors before affecting production systems. Combined with flags like --diff, --limit, and --tags, it provides comprehensive testing capabilities that ensure reliable and predictable infrastructure automation.

Updated on: 2026-03-17T09:01:38+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements