Setting the Timezone
I'll Use My Local Timezone, Easy!
It seems simple … Set the timezone on your machines to the same one you use on a day to day basis... or just leave the timezone at the default. It's not like it really matters right?… Except timezone selection is a very critical part of programming global systems. As the Yeller team writes in The Worst Server Setup Mistake You Can Make, timezone is an important thing to get right at the beginning.
Unless you have very good reasons, you should be using UTC in most cases.
The Problem with Timezones
“It's Five O'Clock Somewhere”
Different countries around the world are in different timezones.
When we're running multiple locations (“regions”) of servers, they'll likely be in different local timezones.
This causes a huge headache when looking into issues and troubleshooting.
What is UTC?
Universal Coordinated Time, annoyingly called “UTC,” is constant and unchanging.
Previously called “Greenwich Mean Time” (GMT), UTC is a standard time used in military, weather, and more.
Why use UTC?
When troubleshooting, it helps to have a constant time.
Having all machines synchronized to one timezone allows us to look at logs for a specific time window.
If an event happens at “15:00 UTC” then we can gather relevant information on all machines.
Whether the machine is next door or halfway across the world, the times will all be the same and we won't have to worry about converting the timezones or worse, accidentally missing important logs.
The Mess of Daylight Saving Time
If you're in the United States, you have experienced “Daylight Saving Time” (DST) switches twice a year.
This “jump” forwards or backwards in time can leave gaps or even potentially overwrite log information.
Let's say you already had a system for managing DST on your machines.
The United States has recently ended DST, which throws another wrench in the works.
If you have 20-year old legacy code that handled DST, now that code itself needs to be changed.
All of this could have been prevented with a standard, non-changing timezone. That's UTC.
Editing the Timezone File with Ansible
We've seen the copy module in action during the last lesson to copy a full file. But this time we add a twist... Instead of actually needing to have a file on your local file system, we can actually just provide the contents of the file as a string using the “contents” option. The copy module will take care of making sure our contents gets written to a file on the remote system with the proper file permissions. Of course, putting the file contents directly in our playbook is only useful when file contents are short. Bigger file contents can get overwhelming to look at when mixed with our playbook, so stick to a separate file for lengthy configurations.
- name: Update Timezone to Etc/UTC copy: content="Etc/UTC\n" dest=/etc/timezone owner=root group=root mode=0644 sudo: true register: timezone
As you can see, we're just adding one line: “Etc/UTC” plus a return (newline character) which gets placed in the file /etc/timezone. Then we “register” (store) the copy module's results in a variable called “timezone” for later use. We need sudo privileges to change this file because this is a system-wide configuration.
Updating the System Timezone with Ansible
We changed the /etc/timezone file, but to ensure the system picks up on our changes, we need to reconfigure the system using the new timezone information. Remember we registered the “timezone” variable. When “timezone” has a status of “changed” we can know when the /etc/timezone file was modified and act accordingly.
- name: Reconfigure Timezone Data command: dpkg-reconfigure -f noninteractive tzdata sudo: true when: timezone.changed
We check the “timezone” variable and only run this reconfiguration task when the contents of /etc/timezone was actually changed. If the /etc/timezone file has not changed, there is no need to reconfigure.
With minimal effort, we've saved many hours of headaches that would have resulted from using a local timezone and dealing with Daylight Savings Time changes (DST).
--- - name: Configure the Timezone hosts: all tasks: - name: Update Timezone to Etc/UTC copy: content="Etc/UTC\n" dest=/etc/timezone owner=root group=root mode=0644 sudo: true register: timezone - name: Reconfigure Timezone Data (if changed) shell: dpkg-reconfigure -f noninteractive tzdata sudo: true when: timezone.changed