X Tutup
Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 3463334

Browse files
committed
PreTasks: If timezone fetch fails, revert to fallback timezones.
1 parent 8afba1b commit 3463334

File tree

1 file changed

+67
-9
lines changed

1 file changed

+67
-9
lines changed

roles/pre_tasks/tasks/subtasks/variables.yml

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,71 @@
148148
)
149149
else false }}"
150150

151-
- name: Variables | Fetch time zone
152-
uri:
153-
url: http://ip-api.com/line?fields=timezone
154-
return_content: yes
155-
register: timezone_response
156-
when: (system.timezone|lower == 'auto') or (not system.timezone)
157-
158-
- name: Variables | Set time zone variable
151+
- name: Variables | Check if 'system.timezone' was specified
159152
set_fact:
160-
tz: "{{ (timezone_response.content | trim) if ((system.timezone|lower == 'auto') or (not system.timezone)) else (system.timezone) }}"
153+
system_timezone_specified: "{{ true if not (
154+
(system is undefined)
155+
or
156+
(system is none)
157+
or
158+
(system | trim | length == 0)
159+
or
160+
(system.timezone is undefined)
161+
or
162+
(system.timezone is none)
163+
or
164+
(system.timezone | trim | length == 0)
165+
or
166+
(system.timezone | lower == 'auto')
167+
)
168+
else false }}"
169+
170+
- name: Variables | Set 'tz' variable to custom timezone
171+
set_fact:
172+
tz: "{{ system.timezone }}"
173+
when: (system_timezone_specified)
174+
175+
- name: Variables | Auto timezone tasks
176+
block:
177+
178+
- name: Variables | Get time zone from 'http://ip-api.com'
179+
uri:
180+
url: http://ip-api.com/line?fields=timezone
181+
return_content: yes
182+
register: ip_api_timezone_response
183+
ignore_errors: yes
184+
185+
- name: Variables | Set 'tz' variable from 'http://ip-api.com'
186+
set_fact:
187+
tz: "{{ ip_api_timezone_response.content | trim }}"
188+
when: (ip_api_timezone_response is succeeded)
189+
190+
- name: Variables | Get timezone from local machine
191+
block:
192+
193+
- name: Variables | Check to see if '/etc/timezone' exists
194+
stat:
195+
path: /etc/timezone
196+
register: etc_timezone
197+
198+
- name: Variables | Set timezone from local machine
199+
block:
200+
201+
- name: Variables | Get info from '/etc/timezone'
202+
shell: cat /etc/timezone
203+
register: etc_timezone_contents
204+
205+
- name: Variables | Set 'tz' variable to '/etc/timezone'
206+
set_fact:
207+
tz: "{{ etc_timezone_contents.stdout | trim }}"
208+
209+
when: (etc_timezone.stat.exists and etc_timezone.stat.isreg)
210+
211+
- name: Variables | Set 'tz' variable to fallback value of 'Etc/UTC'
212+
set_fact:
213+
tz: "Etc/UTC"
214+
when: not (etc_timezone.stat.exists and etc_timezone.stat.isreg)
215+
216+
when: (ip_api_timezone_response is failed)
217+
218+
when: (not system_timezone_specified)

0 commit comments

Comments
 (0)
X Tutup