Getting Started With Ansible - Part Eleven (Templates)

This is in continuation to the articles of the series “Learning Ansible”. In the previous article we saw handlers and their real-life use case.

This is in continuation of the articles of the series “Learning Ansible”. In the previous article, we saw handlers and their real-life use case.
 
Here, in this article, we will be learning about templates in Ansible.
 
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.
 
Ansible
 
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.
 
Ansible
 
Now, let’s create a directory named templates.
  1. mkdir templates   
And, copy the conf files from files folder to the templates folder and rename the files with the j2 extension.
  1. cp -r files/* templates/  
  2. mv ntp_debian.conf ntp_debian.j2  
  3. mv ntp_redhat.conf ntp_redhat.j2  
Next, we will open the ntp_debian.j2 file and will add the variables .
 
BEFORE
 
Ansible
 
AFTER
 
Ansible
 
Similarly, we will open the ntp_ redhat.j2 file and will add the variables.
 
BEFORE
 
Ansible
 
AFTER
 
Ansible
 
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.
  1. mkdir group_vars  
  2. vim group/all   
And will write the below code for NTP server in India,
  1. ntp0: '0.in.pool.ntp.org'  
  2. ntp1: '1.in.pool.ntp.org'  
  3. ntp2: '2.in.pool.ntp.org'  
  4. ntp3: '3.in.pool.ntp.org'   
Now, our directory looks something like below.
 
Ansible  
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,
  1. ---  
  2. - name: Deploying NTP Service  
  3. hosts: all  
  4. become: yes  
  5. tasks:  
  6. - name: Install packages on RedHat OS  
  7. yum:  
  8. name: "{{item}}"  
  9. state: present  
  10. loop:  
  11. - ntp  
  12. - unzip  
  13. - git  
  14. - wget  
  15. - zip  
  16. when: ansible_os_family == "RedHat"  
  17. - name: Install packages on Debian OS  
  18. apt:  
  19. name: "{{item}}"  
  20. state: present  
  21. loop:  
  22. - ntp  
  23. - unzip  
  24. - git  
  25. - wget  
  26. - zip  
  27. when: ansible_os_family == "Debian"  
  28. - name: Start and Enable NTP service in RedHat OS  
  29. service:  
  30. name: ntpd  
  31. state: started  
  32. enabled: yes  
  33. when: ansible_os_family == "RedHat"  
  34. - name: Start and Enable NTP service in Debian OS  
  35. service:  
  36. name: ntp  
  37. state: started  
  38. enabled: yes  
  39. when: ansible_os_family == "Debian"  
  40. - name: Deploy the NTP configuration file for Debian OS  
  41. template:  
  42. src: templates/ntp_debian.j2  
  43. dest: /etc/ntp.conf  
  44. backup: yes  
  45. when: ansible_os_family == "Debian"  
  46. notify:  
  47. - Restart NTP service in Debian OS  
  48. - name: Deploy the NTP configuration file for RedHat OS  
  49. template:  
  50. src: templates/ntp_redhat.j2  
  51. dest: /etc/ntp.conf  
  52. backup: yes  
  53. when: ansible_os_family == "RedHat"  
  54. notify:  
  55. - Restart NTP service in RedHat OS  
  56. handlers:  
  57. - name: Restart NTP service in RedHat OS  
  58. service:  
  59. name: ntpd  
  60. state: restarted  
  61. when: ansible_os_family == "RedHat"  
  62. - name: Restart NTP service in Debian OS  
  63. service:  
  64. name: ntp  
  65. state: restarted  
  66. when: ansible_os_family == "Debian"  
Let’s run the playbook with the below command.
  1. ansible-playbook ntp_playbook.yml   
OUTPUT
 
Ansible
 
We can see that new NTP configurations have been pushed and our handlers also got executed.
 

Summary

 
In this article, we have learned about the templates in Ansible and also, we saw examples on how they actually work and how they can be implemented in a real-life project.
 
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,