In this exercise, you use one of the system roles in conjunction with tasks to configure time synchronization and the time zone on your managed hosts.
Outcomes
Install the system roles for Red Hat Enterprise Linux.
Find and use the system roles documentation.
Use the redhat.rhel_system_roles.timesync role in a playbook to configure time synchronization on remote hosts.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares your environment and ensures that all required resources are available.
[student@workstation ~]$ lab start role-system
Procedure 7.4. Instructions
Change into the /home/student/role-system directory.
[student@workstation ~]$ cd ~/role-system
[student@workstation role-system]$Install the system roles on your control node, workstation.lab.example.com.
Confirm that the system roles have been installed by using ansible-galaxy.
Use the ansible-galaxy collection list command to list the installed collections.
[student@workstation role-system]$ ansible-galaxy collection listCreate the collections directory.
[student@workstation role-system]$ mkdir -p collectionsThe ./collections directory is not a default search location for collections.
Add the collections_paths key to the ansible.cfg file, so that the ./collections directory is searched.
[defaults]
inventory=./inventory
remote_user=devops
collections_paths=./collections:~/.ansible/collections:/usr/share/ansible/collectionsUse the ansible-galaxy command to install the redhat.rhel_system_roles collection from the provided tarball.
[student@workstation role-system]$ansible-galaxy collection \>install -p collections/ redhat-rhel_system_roles-1.19.3.tar.gzProcess install dependency map Starting collection install process Installing 'redhat.rhel_system_roles:1.19.3' to '/home/student/role-system/collections/ansible_collections/redhat/rhel_system_roles' redhat.rhel_system_roles:1.19.3 was installed successfully
Use the ansible-galaxy collection list command to verify that the system roles are now available.
[student@workstation role-system]$ ansible-galaxy collection list
# /home/student/role-system/collections/ansible_collections
Collection Version
------------------------ -------
redhat.rhel_system_roles 1.19.3Create the configure_time.yml playbook with one play that targets the database_servers host group and runs the redhat.rhel_system_roles.timesync role in its roles section.
---
- name: Time Synchronization
hosts: database_servers
roles:
- redhat.rhel_system_roles.timesyncThe role documentation contains a description of each role variable, including the default value for the variable. Determine the role variables that you must override to meet the requirements for time synchronization.
Place role variable values in a file named timesync.yml.
Because these variable values apply to all hosts in the inventory, place the timesync.yml file in the group_vars/all subdirectory.
Review the Role Variables section of the README.md file for the redhat.rhel_system_roles.timesync role.
[student@workstation role-system]$cat \>collections/ansible_collections/redhat/rhel_system_roles/roles/timesync/README.md...output omitted... Role Variables -------------- ...output omitted... # List of NTP serverstimesync_ntp_servers:- hostname: foo.example.com # Hostname or address of the server minpoll: 4 # Minimum polling interval (default 6) maxpoll: 8 # Maximum polling interval (default 10) iburst: yes # Flag enabling fast initial synchronization # (default no) pool: no # Flag indicating that each resolved address # of the hostname is a separate NTP server # (default no) ...output omitted... # Name of the package which should be installed and configured for NTP. # Possible values are "chrony" and "ntp". If not defined, the currently active # or enabled service will be configured. If no service is active or enabled, a # package specific to the system and its version will be selected.timesync_ntp_provider: chrony...output omitted...
Create the group_vars/all subdirectory.
[student@workstation role-system]$ mkdir -pv group_vars/all
mkdir: created directory 'group_vars'
mkdir: created directory 'group_vars/all'Create a group_vars/all/timesync.yml file, adding variable definitions to satisfy the time synchronization requirements.
The file now contains:
---
#redhat.rhel_system_roles.timesync variables for all hosts
timesync_ntp_provider: chrony
timesync_ntp_servers:
- hostname: classroom.example.com
iburst: yesAdd two tasks to the configure_time.yml file to get and conditionally set the time zone for each host.
Ensure that both tasks run after the redhat.rhel_system_roles.timesync role.
Because hosts do not belong to the same time zone, use a variable (host_timezone) for the time zone name.
Create a post_tasks section in the configure_time.yml playbook, then add the first task.
post_tasks:
- name: Get time zone
ansible.builtin.command: timedatectl show
register: current_timezone
changed_when: falseAdd a second task to set the time zone, but only when the time zone is incorrect.
Because system logging and other services use the system time zone, reboot each host when the time zone is modified.
Add a notify keyword to the task, with an associated value of reboot host.
The post_tasks section of the play should now read:
- name: Set time zone
ansible.builtin.command: "timedatectl set-timezone {{ host_timezone }}"
when: host_timezone not in current_timezone.stdout
notify: reboot hostAdd the reboot host handler to the Time Synchronization play.
The complete playbook now contains:
---
- name: Time Synchronization
hosts: database_servers
roles:
- redhat.rhel_system_roles.timesync
post_tasks:
- name: Get time zone
ansible.builtin.command: timedatectl show
register: current_timezone
changed_when: false
- name: Set time zone
ansible.builtin.command: "timedatectl set-timezone {{ host_timezone }}"
when: host_timezone not in current_timezone.stdout
notify: reboot host
handlers:
- name: reboot host
ansible.builtin.reboot:For each data center, create a file named timezone.yml that contains an appropriate value for the host_timezone variable.
Use the timedatectl list-timezones command to find the valid time zone string for each data center.
Create the group_vars subdirectories for the na_datacenter and europe_datacenter host groups.
[student@workstation role-system]$mkdir -pv \>group_vars/{na_datacenter,europe_datacenter}mkdir: created directory 'group_vars/na_datacenter' mkdir: created directory 'group_vars/europe_datacenter'
Use the timedatectl list-timezones command to determine the time zone for both the US and European data centers:
[student@workstation role-system]$timedatectl list-timezones | grep ChicagoAmerica/Chicago [student@workstation role-system]$timedatectl list-timezones | grep HelsinkiEurope/Helsinki
Create the timezone.yml for both data centers:
[student@workstation role-system]$echo "host_timezone: America/Chicago" > \>group_vars/na_datacenter/timezone.yml[student@workstation role-system]$echo "host_timezone: Europe/Helsinki" > \>group_vars/europe_datacenter/timezone.yml
Run the configure_time.yml playbook.
Use the ansible-navigator run --syntax-check command to validate the playbook syntax.
[student@workstation role-system]$ansible-navigator run \>-m stdout configure_time.yml --syntax-checkplaybook: /home/student/role-system/configure_time.yml
Run the configure_time.yml playbook.
[student@workstation role-system]$ansible-navigator run \>-m stdout configure_time.ymlPLAY [Time Synchronization] ************************************************** TASK [Gathering Facts] ******************************************************* ok: [serverb.lab.example.com] ok: [servera.lab.example.com] TASK [redhat.rhel_system_roles.timesync : Set version specific variables] **** ...output omitted... TASK [redhat.rhel_system_roles.timesync : Enable timemaster] ***************** skipping: [servera.lab.example.com] skipping: [serverb.lab.example.com] RUNNING HANDLER [redhat.rhel_system_roles.timesync : restart chronyd] ******** changed: [servera.lab.example.com] changed: [serverb.lab.example.com] TASK [Get time zone] ********************************************************* ok: [servera.lab.example.com] ok: [serverb.lab.example.com] TASK [Set time zone] ********************************************************* changed: [serverb.lab.example.com] changed: [servera.lab.example.com] RUNNING HANDLER [reboot host] ************************************************ changed: [serverb.lab.example.com] changed: [servera.lab.example.com] PLAY RECAP ******************************************************************* servera.lab.example.com : ok=18 changed=6 unreachable=0 failed=0 skipped=25 rescued=0 ignored=0 serverb.lab.example.com : ok=18 changed=6 unreachable=0 failed=0 skipped=25 rescued=0 ignored=0
Verify the time zone settings of each server.
Use the following commands to see the output of the date command on the servera and serverb machines.
The actual time zones listed might vary depending on the time of year, and whether daylight savings is active.
[student@workstation role-system]$ssh servera dateTue Aug 16 07:43:33 PMCDT2022 [student@workstation role-system]$ssh serverb dateWed Aug 17 03:43:41 AMEEST2022
Each server has a time zone setting based on its geographic location.
This concludes the section.