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 of Ansible that allows users to automate tasks and manage configurations.

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 of playbook without making any actual changes. This article will explain what a Dry Run is and how to use it with Ansible Playbooks.

What is a Dry Run in Ansible?

A Dry Run is a simulation of playbook execution that checks if playbook is valid and if it would have intended effect if it were to be executed. It is a way to validate your Playbook without making any actual changes to systems.

When running a Playbook in Dry Run mode, Ansible performs same checks it would perform during a regular execution, but it does not make any changes to systems. Instead, it displays what changes would have been made if playbook was executed.

The Dry Run feature is particularly useful for making sure your Playbooks are error-free and do not cause any unintended changes to your systems.

How to Run a Playbook in Check Mode

Running a playbook in check mode is quite simple. All you need to do is use --check flag when running ansible-playbook command. --check flag tells Ansible to run playbook in Dry Run mode.

Here is an example of how to run a playbook in check mode −

$ ansible-playbook playbook.yml --check

In this example, we are running playbook.yml playbook in check mode.

If there are no errors in playbook, Ansible will display a message indicating that playbook ran successfully in check mode. If there are errors in playbook, Ansible will display an error message indicating what went wrong.

Using --diff flag

In addition to --check flag, you can also use --diff flag to display differences between current state of systems and state that would be achieved if playbook were to be executed.

Here is an example of how to use --diff flag −

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

In this example, we are running playbook.yml playbook in check mode and displaying differences between current state of systems and state that would be achieved if playbook were to be executed.

The --diff flag can be quite helpful when debugging Playbooks, as it can help identify any unintended changes that might occur if playbook were to be executed.

Using --list-tasks flag

Another useful flag when running a playbook in check mode is --list-tasks flag. This flag displays tasks that would be executed if playbook were to be executed, without actually executing them.

Here is an example of how to use --list-tasks flag −

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

In this example, we are running playbook.yml playbook in check mode and displaying tasks that would be executed if playbook were to be executed.

The --list-tasks flag can be helpful when trying to understand what tasks a playbook will perform without actually executing them.

Using --limit flag

The --limit flag is used to limit execution of a playbook to a specific set of systems. This can be useful when testing Playbooks on a subset of your systems.

Here is an example of how to use --limit flag −

$ ansible-playbook playbook.yml --check --limit server1

In this example, we are running playbook.yml playbook in check mode, but limiting execution to only server1 system.

The --limit flag can be quite helpful when testing Playbooks on a specific set of systems, without affecting other systems.

Using --tags flag

The --tags flag is used to limit execution of a playbook to specific tasks that are tagged with specified tag. This can be useful when testing specific tasks in a Playbook.

Here is an example of how to use --tags flag −

$ ansible-playbook playbook.yml --check --tags web

In this example, we are running playbook.yml playbook in check mode, but limiting execution to only tasks that are tagged with web tag.

The --tags flag can be helpful when testing specific tasks in a playbook, without executing all tasks in playbook.

Using --skip-tags flag

The --skip-tags flag is used to skip execution of specific tasks that are tagged with specified tag. This can be useful when testing Playbooks that have specific tasks that you want to skip.

Here is an example of how to use --skip-tags flag −

$ ansible-playbook playbook.yml --check --skip-tags db

In this example, we are running playbook.yml playbook in check mode, but skipping execution of tasks that are tagged with db tag.

The --skip-tags flag can be helpful when testing Playbooks that have specific tasks that you want to skip, without affecting other tasks in playbook.

Additional Benefits

Additional benefits of Ansible Playbook Dry Run mode include −

  • Safely Testing Playbooks − Running Playbooks in check mode allows you to validate tasks to be executed and confirm that they are safe to run before actually applying changes to your systems. This helps to avoid unexpected issues and downtime.

  • Debugging Playbooks − Dry run mode is also useful when debugging Playbooks as it allows you to identify and troubleshoot issues without actually making any changes to your systems.

  • Compliance Validation − If your organization has strict compliance rules, it's important to test your Playbooks in check mode to ensure that they adhere to compliance requirements before deploying them in production.

  • Confidence in Changes − Ansible Playbook Dry Run mode provides an additional layer of confidence in changes being made to your systems. It ensures that changes will not have any adverse effects on your systems.

Overall, Ansible Playbook Dry Run mode is a valuable feature that every Ansible user should use before deploying Playbooks in production. It allows you to safely test and validate Playbooks, debug issues, ensure compliance, and have confidence in changes being made to your systems.

In addition to benefits mentioned above, Ansible Playbook Dry Run mode also provides following advantages −

  • Time-saving − Running Playbooks in check mode can save time by avoiding unnecessary changes to your systems. It allows you to test your Playbooks without making any actual changes, so you can catch errors early on and make corrections before deploying playbook to your systems.

  • Improved Accuracy − Dry run mode helps ensure that your Playbooks are accurate and will not cause unintended changes to your systems. By validating changes before they are applied, you can avoid mistakes that could lead to downtime or other negative impacts on your systems.

  • Easy to Use − Ansible Playbook Dry Run mode is easy to use, and it requires minimal setup. All you need to do is add --check flag to your ansible-playbook command, and Ansible will run your playbook in check mode.

  • Compatibility − Ansible Playbook Dry Run mode is compatible with a wide range of systems, including Linux, macOS, and Windows. This makes it a versatile tool that can be used to manage various types of systems.

  • Scalability − Ansible Playbook Dry Run mode can be used to manage hundreds or even thousands of systems at once. This makes it ideal for large-scale IT operations that require automation and management of a large number of systems.

Conclusion

Ansible Playbook Dry Run or Check mode is an essential feature that enables users to test and validate their Playbooks before executing them on production systems. By using this feature, users can make sure their Playbooks are error-free, do not cause any unintended changes, and work as intended. In this article, we have explained what a Dry Run is and how to use it with Ansible Playbooks. We have also covered several useful flags that can be used when running a playbook in check mode. By using these flags, users can limit execution of playbook to specific systems, tasks, or tags, and display differences between current state and state that would be achieved if playbook were to be executed. Overall, Dry Run feature is a powerful tool that can help users save time and avoid costly mistakes.

Updated on: 02-May-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements