Set Local Timezone with Ansible
Automatically derived from your IP
You know this annoyance when you install a fresh linux server and it has time in UTC? At least I end up like that with Armbian when I don't access it with interactive shell.
It turns out that there is a very simple way of setting your timezone automatically based in your IP location, especially if you use Ansible for that.
Now, Ansible has two tasks:
ipinfoio_facts: which uses http://ipinfo.io/ to approximate your lacation based on your external IP address;
timezone: which sets your timezone.
Thus everything you need to do, is to have these two tasks sequentially in your playbook
---
- hosts: "all"
become: true
tasks:
- name: Get IP geolocation data
ipinfoio_facts:
- name: "Set timezone to {{ ansible_facts.timezone }}"
timezone:
name: "{{ ansible_facts.timezone }}"
when: ansible_facts.timezone is defined
As you can guess from the code, the ipinfoio_facts
task is going to populate ansible_facts
with various location datai, and out of which we will use timezone
to set, well..., the time zone (if it's available).
That's all. I hope I helped someone ☺️