{"id":9687,"date":"2022-10-10T17:52:27","date_gmt":"2022-10-10T12:22:27","guid":{"rendered":"https:\/\/www.skynats.com\/?p=9687"},"modified":"2023-06-15T17:03:35","modified_gmt":"2023-06-15T11:33:35","slug":"ansible-set-timezone-on-ubuntu","status":"publish","type":"post","link":"https:\/\/www.skynats.com\/blog\/ansible-set-timezone-on-ubuntu\/","title":{"rendered":"Ansible Set Timezone On Ubuntu | How To Fix?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">With the assistance of <a href=\"https:\/\/www.skynats.com\/blog\/\" target=\"_blank\" rel=\"noreferrer noopener\">Server Management Services<\/a> from Skynats, let&#8217;s examine ansible how to set the timezone Ubuntu in more detail, along with the configurations required to set this up.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-editing-the-timezone-file-in-ubuntu-using-ansible\">Editing the Timezone File in Ubuntu Using Ansible<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Using the &#8220;contents&#8221; option, the file&#8217;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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Of course, it only makes sense to include the file contents in the playbook when they are brief.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can keep lengthy setups in a separate file because the playbook can make the contents of longer files overwhelming to read.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- name: Update Timezone to Etc\/UTC\r\ncopy: content=\"Etc\/UTC\\n\" dest=\/etc\/timezone owner=root group=root mode=0644\r\nsudo: true\r\nregister: timezone\r\n\r\nYAML<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It is clear that all we are doing is adding one line to the file \/etc\/timezone: &#8220;Etc\/UTC&#8221; followed by a return (newline character). The copy module&#8217;s outcomes are registered in a variable called &#8220;timezone&#8221; for later use. We need Sudo rights to change this setting because it affects the entire system.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-update-and-configure-system-timezone-in-ubuntu-using-ansible\">Update and Configure System Timezone in Ubuntu Using Ansible<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- name: Reconfigure Timezone Data\r\ncommand: dpkg-reconfigure -f noninteractive tzdata\r\nsudo: true\r\nwhen: timezone.changed\r\n\r\nYAML<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When the contents of \/etc\/timezone have changed, we can use the &#8220;timezone&#8221; 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ansible configuration Use the command lines listed below to change the timezone on the Ubuntu host:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>gistfile1.yml<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name: Group by Distribution\r\nhosts: all\r\ntasks: - group_by: key=${ansible_distribution}\r\n\r\n- name: Set Time Zone\r\nhosts: Ubuntu\r\ngather_facts: False\r\ntasks: - name: Set timezone variables\r\ncopy: content='America\/Los_Angeles'\r\ndest=\/etc\/timezone\r\nowner=root\r\ngroup=root\r\nmode=0644\r\nbackup=yes\r\nnotify: - update timezone\r\nhandlers: - name: update timezone\r\ncommand: dpkg-reconfigure --frontend noninteractive tzdata.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then, use gistfile2.sh to complete the ansible set timezone ubuntu processes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:~$ ll \/etc\/ | grep timezone\r\n-rw-r--r-- 1 root root 20 May 16 06:27 timezone\r\n-rw-r--r-- 1 root root 20 May 16 06:22 timezone.2013-05-16@06:24~\r\n-rw-r--r-- 1 root root 20 May 16 06:24 timezone.2013-05-16@06:27~\r\n~$ cat \/etc\/timezone America\/Los_Angeles ~$ cat \/etc\/timezone.2013-05-16@06:24~\r\nAmerica\/Los_Angeles.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Are you looking for an answer to another query?\u00a0<a href=\"https:\/\/www.skynats.com\/contact-us\/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Contact<\/em><\/a>\u00a0our technical support team.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the assistance of Server Management Services from Skynats, let&#8217;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 &#8220;contents&#8221; option, the file&#8217;s contents can be specified as a string. The copy module will [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[236],"class_list":["post-9687","post","type-post","status-publish","format-standard","hentry","category-blog","tag-ubuntu"],"_links":{"self":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/9687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/comments?post=9687"}],"version-history":[{"count":0,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/9687\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/media?parent=9687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/categories?post=9687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/tags?post=9687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}