With the assistance of Server Management Services from Skynats, let’s examine ansible how to set the timezone Ubuntu in more detail, along with the configurations required to set this up.
Editing the Timezone File in Ubuntu Using Ansible
Using the “contents” option, the file’s contents can be specified as a string. The copy module will ensure that the data is saved on the remote machine in a file with the proper file permissions.
Of course, it only makes sense to include the file contents in the playbook when they are brief.
We can keep lengthy setups in a separate file because the playbook can make the contents of longer files overwhelming to read.
- name: Update Timezone to Etc/UTC
copy: content="Etc/UTC\n" dest=/etc/timezone owner=root group=root mode=0644
sudo: true
register: timezone
YAML
It is clear that all we are doing is adding one line to the file /etc/timezone: “Etc/UTC” followed by a return (newline character). The copy module’s outcomes are registered in a variable called “timezone” for later use. We need Sudo rights to change this setting because it affects the entire system.
Update and Configure System Timezone in Ubuntu Using Ansible
In order for the system to recognize the changes, we modified the /etc/timezone file. We need to update the system with the new timezone data in this situation. We can figure out when the /etc/timezone file was modified and take the necessary steps.
- name: Reconfigure Timezone Data
command: dpkg-reconfigure -f noninteractive tzdata
sudo: true
when: timezone.changed
YAML
When the contents of /etc/timezone have changed, we can use the “timezone” variable to decide whether or not to perform this reconfiguration operation. If the /etc/timezone file has not changed, it is not necessary to change anything.
Ansible configuration Use the command lines listed below to change the timezone on the Ubuntu host:
gistfile1.yml
name: Group by Distribution
hosts: all
tasks: - group_by: key=${ansible_distribution}
- name: Set Time Zone
hosts: Ubuntu
gather_facts: False
tasks: - name: Set timezone variables
copy: content='America/Los_Angeles'
dest=/etc/timezone
owner=root
group=root
mode=0644
backup=yes
notify: - update timezone
handlers: - name: update timezone
command: dpkg-reconfigure --frontend noninteractive tzdata.
Then, use gistfile2.sh to complete the ansible set timezone ubuntu processes.
:~$ ll /etc/ | grep timezone
-rw-r--r-- 1 root root 20 May 16 06:27 timezone
-rw-r--r-- 1 root root 20 May 16 06:22 timezone.2013-05-16@06:24~
-rw-r--r-- 1 root root 20 May 16 06:24 timezone.2013-05-16@06:27~
~$ cat /etc/timezone America/Los_Angeles ~$ cat /etc/timezone.2013-05-16@06:24~
America/Los_Angeles.
Conclusion
We have now completed our study of how to configure Ubuntu for Ansible Set Timezone. We have completed the configuration with our Tech Support Team at Skynats.
Are you looking for an answer to another query? Contact our technical support team.