ANSIBLE – IN AND OUT | Templates| PART 11

In the previous article, we created locally NTP configuration files for Debian and RedHat OS based servers and there, we updated the NTP related lines as seen below.

Now, suppose if we have multiple data centers in different geo locations, then we could have different values for the NTP servers and this could also change on different environments and in that case, we will have to keep changing the values for different environments or projects.

So how to put variables in file. The solution is TEMPLATES.

A template in Ansible is a file which contains all your configuration parameters, but the dynamic values are given as variables. 

As of now, we have the below directory structure.

Now, let’s create a directory named templates.

mkdir templates

And, copy the conf files from files folder to the templates folder and rename the files with the j2 extension.

cp -r files/* templates/  
mv templates/ntp_debian.conf templates/ntp_debian.j2  
mv templates/ntp_redhat.conf templates/ntp_redhat.j2  

Next, we will open the ntp_debian.j2 file and will add the variables .

Before
After

Similarly we will do it for redhat system as well.

So, by now, we have defined the variables in the config files. We need to define the values of the variables now. 

We will create a group_vars directory and will define the values of variables there.

mkdir group_vars  
vim group_vars/all 
ntp0: '0.in.pool.ntp.org'  
ntp1: '1.in.pool.ntp.org'  
ntp2: '2.in.pool.ntp.org'  
ntp3: '3.in.pool.ntp.org'  

Next, we need to update the tasks in playbook ntp_playbook.yml and will replace the copy module with template module.

The main difference between copy module and template module is copy module simply copies the file from source and push it to the destination whereas template module is smart, and it checks for the template file and then see if it contains variables, gets the values of those variables, replace the value and then push it to the destination.

Below is the updated playbook code

---                                                                                                                                                                                                               
                                                                                                                                                                                                                  
- name: Deploying NTP Service                                                                                                                                                                                     
  hosts: all                                                                                                                                                                                                      
  become: yes                                                                                                                                                                                                     
  tasks:                                                                                                                                                                                                          
          - name: Install packages on RedHat OS                                                                                                                                                                   
            yum:                                                                                                                                                                                                  
              name: "{{item}}"                                                                                                                                                                                    
              state: present                                                                                                                                                                                      
            loop:                                                                                                                                                                                                 
              - ntp                                                                                                                                                                                               
              - unzip                                                                                                                                                                                             
              - git                                                                                                                                                                                               
              - wget                                                                                                                                                                                              
              - zip                                                                                                                                                                                               
            when: ansible_os_family == "RedHat"                                                                                                                                                                   
                                                                                                                                                                                                                  
          - name: Install packages on Debian OS                                                                                                                                                                   
            apt:                                                                                                                                                                                                  
              name: "{{item}}"                                                                                                                                                                                    
              state: present                                                                                                                                                                                      
            loop:                                                                                                                                                                                                 
              - ntp                                                                                                                                                                                               
              - unzip                                                                                                                                                                                             
              - git                                                                                                                                                                                               
              - wget                                                                                                                                                                                              
              - zip                                                                                                                                                                                               
            when: ansible_os_family == "Debian"                                                                                                                                                                   
                                                                                                                                                                                                                  
          - name: Start and Enable NTP service in RedHat OS                                                                                                                                                       
            service:                                                                                                                                                                                              
              name: ntpd                                                                                                                                                                                          
              state: started                                                                                                                                                                                      
              enabled: yes                                                                                                                                                                                        
            when: ansible_os_family == "RedHat"                                                                                                                                                                   
                                                                                                                                                                                                                  
          - name: Start and Enable NTP service in Debian OS                                                                                                                                                       
            service:                                                                                                                                                                                              
              name: ntp                                                                                                                                                                                           
              state: started                                                                                                                                                                                      
              enabled: yes                                                                                                                                                                                        
            when: ansible_os_family == "Debian"                                                                                                                                                                   
                                                                                                                                                                                                                  
          - name: Deploy the NTP configuration file for Debian OS                                                                                                                                                 
            template:                                                                                                                                                                                             
              src: templates/ntp_debian.j2                                                                                                                                                                        
              dest: /etc/ntp.conf                                                                                                                                                                                 
              backup: yes                                                                                                                                                                                         
            when: ansible_os_family == "Debian"                                                                                                                                                                   
            notify:                                                                                                                                                                                               
                - Restart NTP service in Debian OS                                                                                                                                                                
                                                                                                                                                                                                                  
                                                                                                                                                                                                                  
          - name: Deploy the NTP configuration file for RedHat OS                                                                                                                                                 
            template:                                                                                                                                                                                             
              src: templates/ntp_redhat.j2                                                                                                                                                                        
              dest: /etc/ntp.conf                                                                                                                                                                                 
              backup: yes                                                                                                                                                                                         
            when: ansible_os_family == "RedHat"                                                                                                                                                                   
            notify:                                                                                                                                                                                               
                - Restart NTP service in RedHat OS                                                                                                                                                                
                                                                                                                                                                                                                  
  handlers:                                                                                                                                                                                                       
          - name: Restart NTP service in RedHat OS                                                                                                                                                                
            service:                                                                                                                                                                                              
              name: ntpd                                                                                                                                                                                          
              state: restarted                                                                                                                                                                                    
            when: ansible_os_family == "RedHat"                                                                                                                                                                   
                                                                                                                                                                                                                  
          - name: Restart NTP service in Debian OS                                                                                                                                                                
            service:                                                                                                                                                                                              
              name: ntp                                                                                                                                                                                           
              state: restarted                                                                                                                                                                                    
            when: ansible_os_family == "Debian"                                                                                                                                                                   

We can see that new NTP configurations have been pushed and our handlers also got executed.

About the author

Deepak Sood

Deepak Sood is Lead Consultant in an IT firm holding expertise in Devops and QA Architecture with 8 years of experience.

His expertise is in building highly scalable frameworks. His skills include Java, Configuration Management, Containers, and Kubernetes.

Reach out to him using contact form.

View all posts