Ansible Dry Run: How to Use Check Mode and Diff Mode
By Luca Berton · Published 2024-01-01 · Category: troubleshooting
Learn how to dry run an Ansible playbook using check and diff modes. Discover how to simulate changes and view differences before applying them.
How to Dry Run an Ansible Playbook? The check and diff modes are extremely useful to have a clear vision of the changes that are going to be performed on the target node. I'm going to show you a live Playbook with some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible Playbook Dry Run How to Dry Run the Ansible Playbook: • check • diff
command-line interface parameters • --check • --diff
Ansible Task statements • check_mode: true • diff: true
How to Dry Run an Ansible Playbook Sometimes you need to deep-dive your Ansible Playbook to validate any changes on the target node. It is useful to validate the code and have a clear vision of the single Ansible Task or Ansible Playbook outcome. Let's explore the two modes: check and diff that you could enable via the ansible-playbook command or the Ansible Task statements check_mode: true and diff: trueinside the Playbook code. These modes can be used separately or together. The check mode is just a simulation, it's great to validate the Ansible Playbook without performing any action on the target machine. The diff mode reports the changes made for any module that supports the diff mode. It's common to combine together the two modes --check --diff in order to simulate the execution and have the full reports of changes and increase the execution verbosity.
Links • Validating tasks: check mode and diff mode
Playbook How to Dry Run the Ansible Playbook with the check and diff modes. I'm going to show you the outcome of the check and diff modes on an Ansible Playbook with a simple task to enable the PermitRootLogin parameter in the SSH configuration file /etc/ssh/sshd_config.
code
before execution
Before the execution of the Ansible Playbook the PermitRootLogin is disabled in the SSH configuration file - no value.
check execution
After the execution of the Ansible Playbook with check mode, the SSH configuration file was NOT modified.
check diff execution
After the execution of the Ansible Playbook with check and diff mode, the SSH configuration file was NOT modified.
diff execution
After the execution of the Ansible Playbook with diff mode, the SSH configuration file was modified.
Conclusion
Now you know how to Dry Run an Ansible Playbook using accordingly the check and diff Ansible modes. You know how to use it based on your use case.
Related Articles • Ansible Check Mode Guide • Ansible Become Guide • Ansible Inventory Guide • Ansible Handlers Guide
Category: troubleshooting
Watch the video: Ansible Dry Run: How to Use Check Mode and Diff Mode — Video Tutorial