This is in continuation of the articles of the series “Learning Ansible.
In the previous article Getting started with Ansible – Part 4, we learned about setting up local ansible config. We also wrote our first playbook and got to know about the setup module and fact variables. So, in this article, we will take it forward and will be covering the below topics,
- Printing fact variables using a debug module.
- Different levels of verbosity.
- Conditions in Playbook.
- Deploying sample file on different web servers using playbook.
Let’s get started.
Printing Fact Variables using debug module
We have created a sample playbook named print_fact.yml and here, we are printing the value of the OS family. We have seen that there are lots of built-in fact variables that we get by default when setup module executes.
Here, we are using the debug module which will help us to print fact variable. Ansible uses Jinga2 templating and thus we have enclosed ansible_os_family in {{}}.
Below is the code for our playbook.
- ---
- - hosts: websrv01
- tasks:
- - name: Printing Fact Variables
- debug:
- msg: "The value of OS Family is {{ansible_os_family}}"
Let’s run this playbook with the below command.
ansible-playbook print_fact.yml
OUTPUT

We can see from the output that the OS family has been printed successfully and for our web server 01, the OS family is RedHat as it is a CentOS-based web server.
Note
If you know that you are not going to use the values of fact variables inside the plays in your playbook, then it is recommended to disable the gather_Facts task which is a part of setup module and runs by default. This can be done by setting gather_facts: False.
If you know that you are not going to use the values of fact variables inside the plays in your playbook, then it is recommended to disable the gather_Facts task which is a part of setup module and runs by default. This can be done by setting gather_facts: False.
Now, let’s see if we are still going to get the value of ansible_os_family variable. The updated playbook will be like.
- ---
- - hosts: websrv01
- gather_facts: False
- tasks:
- - name: Printing Fact Variables
- debug:
- msg: "The value of OS Family is {{ansible_os_family}}"
OUTPUT

It clearly says that the variable is undefined as we have disabled the task. Let’s enable it again and see the values of OS family for all web servers.
Updated playbook code
- ---
- - hosts: all
- tasks:
- - name: Printing Fact Variables
- debug:
- msg: "The value of OS Family is {{ansible_os_family}}"
OUTPUT

And, we can see that Web Server 03 has Debian family as it is an Ubuntu server.
Now, we have seen the debug module, and this can be a real help if we want to know and print some information about the target machines and many times, we need detailed information on the output in case of errors. So, to get even more detailed output, we have different levels of verbosity which will help us to get to the exact error easily.
Levels of verbosity
Sometimes, even looking at the output doesn’t make us understand what exactly the issue is. So, there are 4 levels of verbosity and if we want to know more in the output, then we can add those levels during the execution of the playbook.
Level1
ansible-playbook -v web_db.yml
ansible-playbook -v web_db.yml
Level2
ansible-playbook -vv web_db.yml
ansible-playbook -vv web_db.yml
Level3
ansible-playbook -vvv web_db.yml
Level4
ansible-playbook -vvvv web_db.yml
You can run all these commands to see the output. The number of occurrence of -v denotes the levels of verbosity.
Conditions in Playbook
In our previous article Getting started with Ansible – Part 4, we got an error when we tried to install package httpd on web server 03 and we mentioned that we will resolve the error in the next article. So, let’s see what happened which caused installation failure on web server 03.
Httpd package could not be installed on web server 03 as web server 03 has an Ubuntu OS and this OS doesn’t have httpd instead it has apache2 to handle HTTP requests and yum is also not available there, so we will use apt to install the package in Ubuntu.
- ---
- - hosts: WebServersGroup
- become: yes
- tasks:
- - name: Install httpd service
- yum:
- name: httpd
- state: present
- when: ansible_os_family == "RedHat"
- - name: Install apache2 service
- apt:
- name: apache2
- state: present
- when: ansible_os_family == "Debian"
- - name: Start and Enable httpd service
- service:
- name: httpd
- state: started
- enabled: yes
- when: ansible_os_family == "RedHat"
- - name: Start and Enable apache2 service
- service:
- name: apache2
- state: started
- enabled: yes
- when: ansible_os_family == "Debian"





Join the conversation! Your thoughts help the community grow.