diff --git a/README.md b/README.md index 6b60b8c20da611824083e5f93aa4b171f23b9572..0007d13091ccb9e6c436301e9b8f830321cb6946 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,10 @@ Set the `systemd` variable to `false` to prevent any tasks in this role being ru When the `systemd_timesyncd_reboot` variable is set to `true` servers which have incorrect clocks will be rebooted by this role in order to correct their clocks, it defaults to `false`. +### systemd_tz + +The time zone to be used (the hardware clock is set to UTC), for example `Europe/London`, it defaults to `Etc/UTC`. + ### systemd_units A list of systemd units to configure, for example: diff --git a/defaults/main.yml b/defaults/main.yml index 6775b5413ef4c4446d8cfaa420bba8704947b7fa..b4cfe28ba4d3aaaf9debb94d1847c706757769ff 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,6 +1,7 @@ --- systemd: true systemd_timesyncd_reboot: false +systemd_tz: Etc/UTC systemd_units: - name: systemd-timesyncd files: diff --git a/meta/argument_specs.yml b/meta/argument_specs.yml index fd69c50d1eaecdbb6e59aed7122946f3e18279c7..5f42aedd377d72e475a84ce6d92416dd8c867f52 100644 --- a/meta/argument_specs.yml +++ b/meta/argument_specs.yml @@ -70,4 +70,8 @@ argument_specs: type: bool required: false description: Reboot if the clock is not synced with time servers. + systemd_tz: + type: str + required: true + description: The timezone for the clock to be set to. ... diff --git a/tasks/timesyncd.yml b/tasks/timesyncd.yml index f34a433260d907eb63e7bb059fd02c735676d71c..fdce6287c3413b3473ff192a36a660b470bf9718 100644 --- a/tasks/timesyncd.yml +++ b/tasks/timesyncd.yml @@ -42,12 +42,24 @@ - name: Set a variable for the timedatectl status dictionary ansible.builtin.set_fact: systemd_timedatectl_status: "{{ systemd_timedatectl_status_command.stdout | community.general.jc('timedatectl') }}" + systemd_timedatectl_tz: "{{ (systemd_timedatectl_status_command.stdout | community.general.jc('timedatectl') | community.general.json_query('time_zone') | ansible.builtin.split(' '))[0] }}" - name: Debug the timedatectl status ansible.builtin.debug: var: systemd_timedatectl_status verbosity: 2 + - name: Debug the timedatectl tz + ansible.builtin.debug: + var: systemd_timedatectl_tz + verbosity: 2 + + - name: Set the timezone + community.general.timezone: + name: "{{ systemd_tz }}" + hwclock: UTC + when: systemd_timedatectl_tz != systemd_tz + - name: Set the time if not synchronized block: