Getting Started With Ansible - Part Nine

This is in continuation of the articles of the series “Learning Ansible”. In our previous articles, we have learned about loops, variables, and their precedence.
So, in this article we will be covering the below topics: 
  • Install NTP service along with other packages on all servers 
  • Set up local NTP configuration for RedHat and Ubuntu servers
So, let’s get started now.
 
Here we are going to write a playbook to deploy NTP service. NTP keeps the system time in check so that it doesn’t drift because if the servers time is not in sync, then there could be a lot of issues as all our log files contain logs with the timestamp.
 
This service will be syncing the machine time with global NTP servers.
 
One thing to note here is when you set up any servers (VM, EC2 or physical machines) then make sure that all the machines have NTP service.
 
Before getting started let’s see what all do we have. We have a local ansible config and inventory file with the name invent which we created in the previous articles with the screenshot below:
 
Inventory File
 
Ansible
 
Hosts file at /etc/hosts
 
Ansible  
 
Local ansible config file
 
Ansible  
 
Our Directory
 
Ansible
 

Install NTP service along with other packages on all servers

 
Now, let’s write a playbook to install NTP service with other packages on all servers.
 
Create a playbook with the below command
  1. vim ntp_playbook.yml
Below is the code for the playbook,
  1. ---                                                                                                                                                                                 
  2.                                                                                                                                                                                     
  3. - name: Deploying NTP Service                                                                                                                                                       
  4.   hosts: all                                                                                                                                                                        
  5.   become: yes                                                                                                                                                                       
  6.   tasks:                                                                                                                                                                            
  7.           - name: Install packages on RedHat OS                                                                                                                                     
  8.             yum:                                                                                                                                                                    
  9.               name: "{{item}}"                                                                                                                                                      
  10.               state: present                                                                                                                                                        
  11.             loop:                                                                                                                                                                   
  12.               - ntp                                                                                                                                                                 
  13.               - unzip                                                                                                                                                               
  14.               - git                                                                                                                                                                 
  15.               - wget                                                                                                                                                                
  16.               - zip                                                                                                                                                                 
  17.             when: ansible_os_family == "RedHat"                                                                                                                                     
  18.                                                                                                                                                                                     
  19.           - name: Install packages on Debian OS                                                                                                                                     
  20.             apt:                                                                                                                                                                    
  21.               name: "{{item}}"                                                                                                                                                      
  22.               state: present                                                                                                                                                        
  23.             loop:                                                                                                                                                                   
  24.               - ntp                                                                                                                                                                 
  25.               - unzip                                                                                                                                                               
  26.               - git                                                                                                                                                                 
  27.               - wget                                                                                                                                                                
  28.               - zip                                                                                                                                                                 
  29.             when: ansible_os_family == "Debian"                                                                                                                                     
  30.                                                                                                                                                                                     
  31.           - name: Start and Enable NTP service in RedHat OS                                                                                                                         
  32.             service:                                                                                                                                                                
  33.               name: ntpd                                                                                                                                                            
  34.               state: started                                                                                                                                                        
  35.               enabled: yes                                                                                                                                                          
  36.             when: ansible_os_family == "RedHat"                                                                                                                                     
  37.                                                                                                                                                                                     
  38.           - name: Start and Enable NTP service in Debian OS                                                                                                                         
  39.             service:                                                                                                                                                                
  40.               name: ntp                                                                                                                                                             
  41.               state: started                                                                                                                                                        
  42.               enabled: yes                                                                                                                                                          
  43.             when: ansible_os_family == "Debian"   
 
Let’s run the playbook and see the output.
 
Ansible
 
We can see that all the packages along with NTP have been installed successfully and service has also been started.
 

Set up local NTP configuration for RedHat and Ubuntu servers

 
As the NTP service has been installed so we can log in to any server and can see the NTP config file.
  • Login to the web server 01 with the below command
    ssh devops@websrv01

  • Open the configuration file with the below command
    vim /etc/ntp.conf

  • Setting present in /etc/ntp.conf file

    Ansible
Now if we want to use the NTP service from any of the global NTP servers then these 4 lines need to be changed. So, what we do now is we will copy the entire /etc/ntp.conf file and paste it inside the directory in our ansible machine.
 
Suppose we have 100 servers then we will have to manually login to each server and change this setting which is a pretty cumbersome and inefficient approach.
 

Steps to create NTP configuration in ansible machine for RedHat OS based servers

  1. Create a folder named files and the create a file for RedHat OS based servers,

    mkdir files
    vim files/ntp_redhat.conf
  1. Copy all the information from websrv 01 ntp configuration and paste it inside ansible machine ntp_redhat.conf file.
  1. We are going to use NTP server for, suppose, North America region… keeping in mind that our servers reside in North America from here
  2. Then we will update the server 0,1,2 and 3 in ntp_redhat.conf file from the below information for north America region.

    Ansible
Below is the updated ntp_redhat.conf file. 
 
Ansible  
 

Steps to create NTP configuration in ansible machine for Debian OS based servers

 
Now we will do the same for Debian based OS server which is our web server 03.
  1. Create an NTP configuration file for Debian OS based servers.

    vim files/ntp_debian.conf
  1. Login to the websrv03 and copy all the information from websrv 03 ntp configuration and paste it inside ansible machine ntp_debian.conf
  2. Update the pool 0,1,2 and 3 in ntp_debian.conf file with the information for the North America region as done above.
Below is the updated ntp_debian.conf file.
 
Ansible  
 
Now our files directory contains 2 NTP configuration files.
 
Ansible
Now, we are going to deploy these NTP config files for RedHat and Debian based systems on our servers and will restart the NTP service. For this, we need to add 4 more tasks in our playbook.
 
Below is the updated playbook code.
  1. ---                                                                                                                                                                                                                   
  2.                                                                                                                                                                                                                       
  3. - name: Deploying NTP Service                                                                                                                                                                                         
  4.   hosts: all                                                                                                                                                                                                          
  5.   become: yes                                                                                                                                                                                                         
  6.   tasks:                                                                                                                                                                                                              
  7.           - name: Install packages on RedHat OS                                                                                                                                                                       
  8.             yum:                                                                                                                                                                                                      
  9.               name: "{{item}}"                                                                                                                                                                                        
  10.               state: present                                                                                                                                                                                          
  11.             loop:                                                                                                                                                                                                     
  12.               - ntp                                                                                                                                                                                                   
  13.               - unzip                                                                                                                                                                                                 
  14.               - git                                                                                                                                                                                                   
  15.               - wget                                                                                                                                                                                                  
  16.               - zip                                                                                                                                                                                                   
  17.             when: ansible_os_family == "RedHat"                                                                                                                                                                       
  18.                                                                                                                                                                                                                       
  19.           - name: Install packages on Debian OS                                                                                                                                                                       
  20.             apt:                                                                                                                                                                                                      
  21.               name: "{{item}}"                                                                                                                                                                                        
  22.               state: present                                                                                                                                                                                          
  23.             loop:                                                                                                                                                                                                     
  24.               - ntp                                                                                                                                                                                                   
  25.               - unzip                                                                                                                                                                                                 
  26.               - git                                                                                                                                                                                                   
  27.               - wget                                                                                                                                                                                                  
  28.               - zip                                                                                                                                                                                                   
  29.             when: ansible_os_family == "Debian"                                                                                                                                                                       
  30.                                                                                                                                                                                                                       
  31.           - name: Start and Enable NTP service in RedHat OS                                                                                                                                                           
  32.             service:                                                                                                                                                                                                  
  33.               name: ntpd                                                                                                                                                                                              
  34.               state: started                                                                                                                                                                                          
  35.               enabled: yes                                                                                                                                                                                            
  36.             when: ansible_os_family == "RedHat"                                                                                                                                                                       
  37.                                                                                                                                                                                                                       
  38.           - name: Start and Enable NTP service in Debian OS                                                                                                                                                           
  39.             service:                                                                                                                                                                                                  
  40.               name: ntp                                                                                                                                                                                               
  41.               state: started                                                                                                                                                                                          
  42.               enabled: yes                                                                                                                                                                                            
  43.             when: ansible_os_family == "Debian"                                                                                                                                                                       
  44.                                                                                                                                                                                                                       
  45.           - name: Deploy the NTP configuration file for Debian OS                                                                                                                                                     
  46.             copy:                                                                                                                                                                                                     
  47.               src: files/ntp_debian.conf                                                                                                                                                                              
  48.               dest: /etc/ntp.conf                                                                                                                                                                                     
  49.               backup: yes                                                                                                                                                                                             
  50.             when: ansible_os_family == "Debian"                                                                                                                                                                       
  51.                                                                                                                                                                                                                       
  52.           - name: Deploy the NTP configuration file for RedHat OS                                                                                                                                                     
  53.             copy:                                                                                                                                                                                                     
  54.               src: files/ntp_redhat.conf                                                                                                                                                                              
  55.               dest: /etc/ntp.conf                                                                                                                                                                                     
  56.               backup: yes                                                                                                                                                                                             
  57.             when: ansible_os_family == "RedHat"                                                                                                                                                                       
  58.                                                                                                                                                                                                                       
  59.           - name: Restart NTP service in RedHat OS                                                                                                                                                                    
  60.             service:                                                                                                                                                                                                  
  61.               name: ntpd                                                                                                                                                                                              
  62.               state: restarted                                                                                                                                                                                        
  63.             when: ansible_os_family == "RedHat"                                                                                                                                                                       
  64.                                                                                                                                                                                                                       
  65.           - name: Restart NTP service in Debian OS                                                                                                                                                                    
  66.             service:                                                                                                                                                                                                  
  67.               name: ntp                                                                                                                                                                                               
  68.               state: restarted                                                                                                                                                                                        
  69.             when: ansible_os_family == "Debian"    
OUTPUT
 
Ansible  
So, we can see that our newly added tasks have been executed successfully and our local NTP configuration files have been deployed and the service has restarted.
 

Summary

 
In this article, we have learned how we can install NTP service and how we can set up the local NTP configuration which can be deployed on multiple machines rather than setting up on each machine one by one.
 
In the next article in this series, we will be learning some more interesting concepts.
 
I hope you find this article helpful. Stay tuned for more … Cheers!!
 
You can also check out some of my previous articles of the series “Learning Ansible” here: 


Similar Articles