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:
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. - 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"
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.
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. - 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. copy:
  42. src: files/ntp_debian.conf
  43. dest: /etc/ntp.conf
  44. backup: yes
  45. when: ansible_os_family == "Debian"
  46. - name: Deploy the NTP configuration file for RedHat OS
  47. copy:
  48. src: files/ntp_redhat.conf
  49. dest: /etc/ntp.conf
  50. backup: yes
  51. when: ansible_os_family == "RedHat"
  52. - name: Restart NTP service in RedHat OS
  53. service:
  54. name: ntpd
  55. state: restarted
  56. when: ansible_os_family == "RedHat"
  57. - name: Restart NTP service in Debian OS
  58. service:
  59. name: ntp
  60. state: restarted
  61. 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: