Ansible Dry Run – How to Run Playbook in Ansible Check mode

By Linux Guru 01.01.0001

Excerpt

Introduction Ansible Dry Run or Ansible Check mode feature is to check your playbook before execution like Ansible’s –syntax-check feature.

With Ansible Dry Run feature you can execute the playbook without having to actually make changes on the server. With Ansible Dry Run you can see if the host is getting changed or not.

Introduction

Ansible Dry Run or Ansible Check mode feature is to check your playbook before execution like Ansible’s --syntax-check feature.

With Ansible Dry Run feature you can execute the playbook without having to actually make changes on the server. With Ansible Dry Run you can see if the host is getting changed or not.

Here is a sample playbook is written to install and start the Apache HTTPD web server.

---
  - 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 Apache web server and start it  but if we want to see if it would affect/change the host or throw error . We can Dry Run this Playbook

with no further ado, Let me tell you how to Run the Ansible Playbook in Check mode or dry run mode ASCII Video

Where to use Ansible Dry Run

Consider you want to run the aforementioned playbook to install Apache HTTPD server against a list of hosts which already has Apache installed in them. You could simply run this playbook with Check mode or perform a Dry Run to verify it..

let us do it practically.

Ansible Dry Run – How to execute the playbook on check mode

This can be done with the help of -C or --check flag of the ansible-playbook command

As said 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 will attempt to run your playbook as before. It’s called Dry Run, right? This is what it was designed for.

But when you make the task conditional or based on the results in the notebook, the test run will fail.

For example, consider the following playbook in which you try to install a Weblogic application server based on the installation and availability of the Java JDK. If you run the following playbook in verification mode, that is, a test run. It will fail halfway because it cannot meet the requirements of the install weblogic task because Java is not installed. (as it cannot install anything during a 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"

I hope you can understand how the Dry run function works and where it can be used and where it cannot be used.

While still exploring the possibilities of using this Checkmod and running it dry. I would be happy if you could tell us where this test run is useful in your case. Use the comments field.

comments powered by Disqus