Ansible Pilot

Ansible Playbook Dry Run - check and diff mode

How to use the check and diff mode to debug and deeply understand the action performed by your Ansible Playbook.

May 23, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

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 demo 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:

command-line interface parameters

Ansible Task statements

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.

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

demo

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

---
- name: root login enabled
  hosts: all
  become: true
  gather_facts: false
  tasks:
    - name: ssh PermitRootLogin
      ansible.builtin.lineinfile:
        dest: /etc/ssh/sshd_config
        regexp: '^PermitRootLogin'
        line: "PermitRootLogin yes"
        state: present
      notify: ssh restart
  handlers:
    - name: ssh restart
      ansible.builtin.service:
        name: sshd
        state: restarted

before execution

Before the execution of the Ansible Playbook the PermitRootLogin is disabled in the SSH configuration file - no value.

$ ssh [email protected]
[devops@demo ~]$ sudo grep ^PermitRootLogin /etc/ssh/sshd_config 
PermitRootLogin no

check execution

$ ansible-playbook --check -i virtualmachines/demo/inventory edit\ single-line\ text/enable_root_login.yml
PLAY [root login enabled] *************************************************************************
TASK [ssh PermitRootLogin] ************************************************************************
changed: [demo.example.com]
RUNNING HANDLER [ssh restart] *********************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

After the execution of the Ansible Playbook with check mode, the SSH configuration file was NOT modified.

$ ssh [email protected]
[devops@demo ~]$ sudo grep ^PermitRootLogin /etc/ssh/sshd_config 
PermitRootLogin no

check diff execution

$ ansible-playbook --check --diff -i virtualmachines/demo/inventory edit\ single-line\ text/enable_root_login.yml
PLAY [root login enabled] *************************************************************************
TASK [ssh PermitRootLogin] ************************************************************************
--- before: /etc/ssh/sshd_config (content)
+++ after: /etc/ssh/sshd_config (content)
@@ -41,7 +41,7 @@
 
 #LoginGraceTime 2m
 #PermitRootLogin yes
-PermitRootLogin no
+PermitRootLogin yes
 #StrictModes yes
 #MaxAuthTries 6
 #MaxSessions 10
changed: [demo.example.com]
RUNNING HANDLER [ssh restart] *********************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

After the execution of the Ansible Playbook with check and diff mode, the SSH configuration file was NOT modified.

ansible-pilot $ ssh [email protected]
[devops@demo ~]$ sudo grep ^PermitRootLogin /etc/ssh/sshd_config 
PermitRootLogin no

diff execution

$ ansible-playbook --diff -i virtualmachines/demo/inventory edit\ single-line\ text/enable_root_login.yml
PLAY [root login enabled] *************************************************************************
TASK [ssh PermitRootLogin] ************************************************************************
--- before: /etc/ssh/sshd_config (content)
+++ after: /etc/ssh/sshd_config (content)
@@ -41,7 +41,7 @@
 
 #LoginGraceTime 2m
 #PermitRootLogin yes
-PermitRootLogin no
+PermitRootLogin yes
 #StrictModes yes
 #MaxAuthTries 6
 #MaxSessions 10
changed: [demo.example.com]
RUNNING HANDLER [ssh restart] *********************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

After the execution of the Ansible Playbook with diff mode, the SSH configuration file was modified.

ansible-pilot $ ssh [email protected]
[devops@demo ~]$ sudo grep ^PermitRootLogin /etc/ssh/sshd_config 
PermitRootLogin yes

Recap

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.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases