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.
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.
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 ntp_debian.conf ntp_debian.j2
- mv ntp_redhat.conf ntp_redhat.j2
Next, we will open the ntp_debian.j2 file and will add the variables .
BEFORE
AFTER
Similarly, we will open the ntp_ redhat.j2 file and will add the variables.
BEFORE
AFTER
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/all
And will write the below code for NTP server in India,
- ntp0: '0.in.pool.ntp.org'
- ntp1: '1.in.pool.ntp.org'
- ntp2: '2.in.pool.ntp.org'
- ntp3: '3.in.pool.ntp.org'
Now, our directory looks something like below.
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"
Let’s run the playbook with the below command.
- ansible-playbook ntp_playbook.yml
OUTPUT
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,