Ansible Dry Run or Ansible Check mode feature is to validate your playbook before execution
With the Ansible Dry Run feature, you can execute the playbook without having to actually make changes on the server. this enables us to see what changes are going to be made when the playbook is run.
Here is a playbook to install and start the Apache HTTPD web server. But I want to dry run it.
---
- name: Playbook
hosts: webservers
become: yes
become_user: root
tasks:
- name: ensure apache is at the latest version
yum:
name: httpd
state: latest
- name: ensure apache is running
service:
name: httpd
state: started
Our objective is to install the Apache web server but I want to dry run it first and see what changes it makes on the remote host.
In a way, I can use this to see what machines are already having the httpd/apache
installed.
We can Dry Run this Playbook to validate.
Ansible Dry Run - How to execute the playbook on check mode
To execute the ansible playbook in check mode (dry run) you need to start the playbook with -C
or --check
option of the ansible-playbook
command
So here is the command to run ansible-playbook in check mode ( dry run)
$ ansible-playbook <playbookname> – check
As mentioned earlier, we are going to run the playbook against the list of hosts which already has Apache installed in them. So we are going to verify it by doing a DRY RUN
Where Ansible Dry run would fail / Cannot be used
Ansible dry run is to validate and see what changes are being made before they are actually applied.
But when you have conditional or result-based task execution in the playbook. The dry run would fail.
For example, let us consider the following playbook where you are trying to install the Weblogic Application server based on the installation and availability of Java JDK.
If you are running the following playbook in check mode aka dry run. It would fail in the middle cause it cannot satisfy the requirement for install weblogic
task as the Java is not yet installed. ( as it could not install anything on the dry run)
- name : Install java
become: yes
become_user: weblogic
tags: installjava, app
shell: "tar -xzf server-jre*.tar.gz"
args:
chdir: "{{ oracle_home }}"
environment:
JAVA_HOME: "/opt/oracle/jdk1.8.0_191"
PATH: "{{JAVA_HOME}}/bin"
- name: Crate a Soft link to java
become: yes
become_user: root
tags: linkjava
shell: "ln -s {{oracle_home}}/jdk1.8.0_191/bin/java /usr/bin/java"
ignore_errors: true
- name : Validate Java
become: yes
become_user: weblogic
tags: app,vjava
command: "java -version"
register: javaver
- debug:
msg: " Java Version Found {{ javaver.stderr }}"
- name: Install Weblogic
become: yes
become_user: weblogic
tags: installweblogic,app
register: wlsinstall
shell: "java -jar {{ oracle_home}}/fmw*.jar -silent -invPtrLoc {{oracle_home}}/oraInst.loc -responseFile {{oracle_home}}/install.file -ignoreSysPrereqs -force -novalidation ORACLE_HOME={{oracle_home}} INSTALL_TYPE='WebLogic Server'"
args:
chdir: "{{ oracle_home }}"
when: "'java version \"1.8.0_191\"' in javaver.stderr"
failed_when: "'failed' in wlsinstall.stderr"
changed_when: "'already installed' not in wlsinstall.stdout"
Hope you are able to understand how Dry run is working and where it can be used and where it cannot.
Cheers,
Sarav AK
Follow me on Linkedin My Profile Follow DevopsJunction onFacebook orTwitter For more practical videos and tutorials. Subscribe to our channel
Signup for Exclusive "Subscriber-only" Content