Bookmark this page

Guided Exercise: Reusing Content with System Roles

In this exercise, you will use one of the Red Hat Enterprise Linux System Roles in conjunction with a normal task to configure time synchronization and the time zone on your managed hosts.

Outcomes

You should be able to:

  • Install the Red Hat Enterprise Linux System Roles.

  • Find and use the RHEL System Roles documentation.

  • Use the rhel-system-roles.timesync role in a playbook to configure time synchronization on remote hosts.

Scenario Overview

Your organization maintains two data centers: one in the United States (Chicago) and one in Finland (Helsinki). To aid log analysis of database servers across data centers, ensure the system clock on each host is synchronized using Network Time Protocol. To aid time-of-day activity analysis across data centers, ensure each database server has a time zone set that corresponds to the host's data center location.

Time synchronization has the following requirements:

  • Use the NTP server located at classroom.example.com. Enable the iburst option to accelerate initial time synchronization.

  • Use the chrony package for time synchronization.

Log in to workstation as student using student as the password.

On workstation, run the lab role-system start command. This creates the working directory, /home/student/role-system, and populates it with an Ansible configuration file and host inventory.

[student@workstation ~]$ lab role-system start

Procedure 7.1. Instructions

  1. Change to the /home/student/role-system working directory.

    [student@workstation ~]$ cd ~/role-system
    [student@workstation role-system]$
  2. Install the Red Hat Enterprise Linux system roles on the control node, workstation.lab.example.com. Verify the installed location of the roles on the control node.

    1. Use the ansible-galaxy command to verify that no roles are initially available for use in the playbook project.

      [student@workstation role-system]$ ansible-galaxy list
      # /home/student/role-system/roles
      # /usr/share/ansible/roles
      # /etc/ansible/roles

      The ansible-galaxy command searches three directories for roles, as indicated by the roles_path entry in the ansible.cfg file:

      • ./roles

      • /usr/share/ansible/roles

      • /etc/ansible/roles

      The above output indicates there are no roles in any of these directories.

    2. Install the rhel-system-roles package.

      [student@workstation role-system]$ sudo yum install rhel-system-roles

      Enter y when prompted to install the package.

    3. Use the ansible-galaxy command to verify that the system roles are now available.

      [student@workstation role-system]$ ansible-galaxy list
      # /home/student/role-system/roles
      # /usr/share/ansible/roles
      ...output omitted...
      - rhel-system-roles.timesync, (unknown version)
      - rhel-system-roles.tlog, (unknown version)
      # /etc/ansible/roles

      The roles are located in the /usr/share/ansible/roles directory. Any role beginning with linux-system-roles is actually a symlink to the corresponding rhel-system-roles role.

  3. Create a playbook, configure_time.yml, with one play that targets the database_servers host group. Include the rhel-system-roles.timesync role in the roles section of the play.

    ---
    - name: Time Synchronization
      hosts: database_servers
    
      roles:
        - rhel-system-roles.timesync
  4. The role documentation contains a description of each role variable, including the default value for the variable. Determine the role variables to 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.

    1. Review the Role Variables section of the README.md file for the rhel-system-roles.timesync role.

      [student@workstation role-system]$ cat \
      > /usr/share/doc/rhel-system-roles/timesync/README.md
      ...output omitted...
      Role Variables
      --------------
      ...output omitted...
      # List of NTP servers
      timesync_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...
    2. 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'
    3. Create a new file group_vars/all/timesync.yml using a text editor. Add variable definitions to satisfy the time synchronization requirements. The file now contains:

      ---
      #rhel-system-roles.timesync variables for all hosts
      
      timesync_ntp_provider: chrony
      
      timesync_ntp_servers:
        - hostname: classroom.example.com
          iburst: yes
  5. Add a task to configure_time.yml, to set the time zone for each host. Ensure the task uses the timezone module and executes after the 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.

    1. Review the Examples section of the timezone module documentation.

      [student@workstation role-system]$ ansible-doc timezone | grep -A 4 "EXAMPLES"
      EXAMPLES:
      
      - name: set timezone to Asia/Tokyo
        timezone:
          name: Asia/Tokyo
    2. Add a task to the post_tasks section of the play in the configure_time.yml playbook. Model the task after the example from the documentation, but use the host_timezone variable for the time zone name.

      The documentation in ansible-doc timezone recommends a restart of the Cron service if the module changes the timezone, to make sure Cron jobs run at the right times. Since 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 read:

        post_tasks:
          - name: Set timezone
            timezone:
              name: "{{ host_timezone }}"
            notify: reboot host
    3. Add the reboot host handler to the Time Synchronization play. The complete playbook now contains:

      ---
      - name: Time Synchronization
        hosts: database_servers
      
        roles:
          - rhel-system-roles.timesync
      
        post_tasks:
          - name: Set timezone
            timezone:
              name: "{{ host_timezone }}"
            notify: reboot host
      
        handlers:
          - name: reboot host
            reboot:
  6. 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.

    1. 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'
    2. 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 Chicago
      America/Chicago
      [student@workstation role-system]$ timedatectl list-timezones | grep Helsinki
      Europe/Helsinki
    3. 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
  7. Run the playbook.

    [student@workstation role-system]$ ansible-playbook configure_time.yml
    
    PLAY [Time Synchronization] **************************************************
    
    TASK [Gathering Facts] *******************************************************
    ok: [serverb.lab.example.com]
    ok: [servera.lab.example.com]
    
    TASK [rhel-system-roles.timesync : Check if only NTP is needed] **************
    ok: [servera.lab.example.com]
    ok: [serverb.lab.example.com]
    
    ...output omitted...
    
    TASK [rhel-system-roles.timesync : Enable timemaster] ************************
    skipping: [servera.lab.example.com]
    skipping: [serverb.lab.example.com]
    
    RUNNING HANDLER [rhel-system-roles.timesync : restart chronyd] ***************
    changed: [servera.lab.example.com]
    changed: [serverb.lab.example.com]
    
    TASK [Set timezone] **********************************************************
    changed: [serverb.lab.example.com]
    changed: [servera.lab.example.com]
    
    RUNNING HANDLER [reboot host] ************************************************
    changed: [serverb.lab.example.com]
    changed: [servera.lab.example.com]
    
    servera.lab.example.com    : ok=17   changed=6    unreachable=0    failed=0
      skipped=20   rescued=0    ignored=6
    serverb.lab.example.com    : ok=17   changed=6    unreachable=0    failed=0
      skipped=20   rescued=0    ignored=6
  8. Verify the time zone settings of each server. Use an Ansible ad hoc command to see the output of the date command on all the database servers.

    Note

    The actual timezones listed will vary depending on the time of year, and whether daylight savings is active.

    [student@workstation role-system]$ ansible database_servers -m shell -a date
    servera.lab.example.com | CHANGED | rc=0 >>
    Fri Jul 16 17:38:40 CDT 2021
    serverb.lab.example.com | CHANGED | rc=0 >>
    Sat Jul 17 01:38:40 EEST 2021

    Each server has a time zone setting based on its geographic location.

Finish

Run the lab role-system finish command to cleanup the managed host.

[student@workstation ~]$ lab role-system finish

This concludes the guided exercise.

Revision: rh294-8.4-9cb53f0