Getting Started With Ansible - Part Five

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.
  1. ---                                                                         
  2. - hosts: websrv01                                                          
  3.   tasks:                                                                    
  4.     - name: Printing Fact Variables                                         
  5.       debug:                                                                
  6.         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.
 
Now, let’s see if we are still going to get the value of ansible_os_family variable. The updated playbook will be like.
  1. ---                                                                         
  2. - hosts: websrv01                                                          
  3.   gather_facts: False                                                       
  4.   tasks:                                                                    
  5.     - name: Printing Fact Variables                                         
  6.       debug:                                                                
  7.         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
  1. ---                                                                                                               
  2. - hosts: all                                                                                                      
  3.   tasks:                                                                                                          
  4.     - name: Printing Fact Variables                                                                               
  5.       debug:                                                                                                      
  6.         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
 
Level2
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.
 
So, to do this, we can apply conditions in the playbook. Below is the code for the playbook which depending on the OS family decides which package to install  using yum or apt.
  1. ---    
  2. - hosts: WebServersGroup    
  3.   become: yes    
  4.   tasks:    
  5.           - name: Install httpd service    
  6.             yum:    
  7.               name: httpd    
  8.               state: present    
  9.             when: ansible_os_family == "RedHat"    
  10.     
  11.           - name: Install apache2 service    
  12.             apt:    
  13.               name: apache2    
  14.               state: present    
  15.             when: ansible_os_family == "Debian"    
  16.     
  17.           - name: Start and Enable httpd service    
  18.             service:    
  19.               name: httpd    
  20.               state: started    
  21.               enabled: yes    
  22.             when: ansible_os_family == "RedHat"    
  23.     
  24.           - name: Start and Enable apache2 service    
  25.             service:    
  26.               name: apache2     
  27.               state: started    
  28.               enabled: yes    
  29.             when: ansible_os_family == "Debian"   
OUTPUT
 
 
So, we can see now that issue has been resolved and for TASK [Install Httpd service] ansible is skipping web server 03 and for TASK [Install apache2 service] ansible is skipping web server 01 and 02.
 
That’s how we can do lots of interesting things with conditions in the playbook. For example, depending on free ram size with fact variables we can skip or perform some tasks, etc.
 
As we have learned conditions in the playbook, now let’s deploy some sample files on all web servers 01,02 and 03 and this time, I hope you will understand what is being written in the playbook.
 

Deploying sample file on different web servers using a playbook

 
So, to deploy files on web servers we are going to use copy and service modules of ansible.
  • Copy module will copy the files from source to destination.
  • The service module will help us in starting and enabling the service.
But before that let’s create a sample index.html file in the files directory with the below commands.
  1. mkdir files    
  2. vim files/index.html  
 
Below is our playbook.
  1. ---                                                                       
  2. - hosts: WebServersGroup                                                    
  3.   become: yes                                                               
  4.   tasks:                                                                    
  5.           - name: Install httpd service                                     
  6.             yum:                                                            
  7.               name: httpd                                                   
  8.               state: present                                                
  9.             when: ansible_os_family == "RedHat"                                                                                                                                                                        
  10.           - name: Install apache2 service                                  
  11.             apt:                                                            
  12.               name: apache2                                               
  13.               state: present                                              
  14.             when: ansible_os_family == "Debian"                                                                                                                                                                      
  15.           - name: Start and Enable httpd service                            
  16.             service:                                                        
  17.               name: httpd                                                   
  18.               state: started                                                
  19.               enabled: yes                                                  
  20.             when: ansible_os_family == "RedHat"                                                                                                                                                                       
  21.           - name: Start and Enable apache2 service                          
  22.             service:                                                       
  23.               name: apache2                                                 
  24.               state: started                                                
  25.               enabled: yes                                                  
  26.             when: ansible_os_family == "Debian"                                                                                                                                                                      
  27.           - name: Deploy index file                                        
  28.             copy:                                                          
  29.               src: files/index.html                                        
  30.               dest: /var/www/html/index.html                                
  31.               owner: root                                                   
  32.               group: root                                                   
  33.               mode: '0644'                                                  
  34.               backup: yes                                                                                                                                                                                            
  35.           - name: Restart httpd service on CentOS                           
  36.             service:                                                        
  37.               name: httpd                                                   
  38.               state: restarted                                              
  39.               enabled: yes                                                  
  40.             when: ansible_os_family == "RedHat"                                                                                                                                                                      
  41.           - name: Restart apache2 service on Ubuntu                        
  42.             service:                                                        
  43.               name: apache2                                                 
  44.               state: restarted                                             
  45.               enabled: yes                                                  
  46.             when: ansible_os_family == "Debian"    
OUTPUT
 
 
We can see that all of our tasks have been completed successfully. Now, let’s open one of the web servers using the public IP in AWS and paste it on the browser and see if it works.
 
On Browser
 
Clearly, this content is coming from the same file which we just deployed. Great, 😊 we are done.
 

Summary

 
So, we have learned about the debug module for printing fact variables and also saw conditions in the playbook and deployed sample files with different tasks in plays in playbooks. We have lots of other interesting things coming up so keep learning.
 
You can also check out some of my previous articles of the series “Learning Ansible” here,


Similar Articles