Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@
service:
name: chronyd
state: restarted
when: __timesync_is_booted | d(false)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Handler will be skipped if __timesync_is_booted is not set.

Confirm that skipping the handler when __timesync_is_booted is undefined is intentional, as this may affect environments where the variable is missing.


- name: Restart ntpd
service:
name: ntpd
state: restarted
when: __timesync_is_booted | d(false)

- name: Restart ptp4l
service:
name: ptp4l
state: restarted
when: __timesync_is_booted | d(false)

- name: Restart phc2sys
service:
name: phc2sys
state: restarted
when: __timesync_is_booted | d(false)

# wokeignore:rule=master
- name: Restart timemaster
service:
# wokeignore:rule=master
name: timemaster
state: restarted
when: __timesync_is_booted | d(false)
1 change: 1 addition & 0 deletions meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ galaxy_info:
galaxy_tags:
- centos
- chrony
- containerbuild
- el6
- el7
- el8
Expand Down
43 changes: 31 additions & 12 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- name: Populate service facts
service_facts:
when: __timesync_is_booted | d(false)

- name: Set variable `timesync_services` with filtered uniq service names
set_fact:
Expand All @@ -18,6 +19,24 @@
map('regex_replace', '@$', '') |
unique |
list }}"
when: __timesync_is_booted | d(false)

- name: Populate service facts when in bootc mode
command: systemctl list-unit-files --type=service -l # noqa command-instead-of-module
register: timesync_services_output
changed_when: false
when: not __timesync_is_booted | d(false)

- name: Set variable `timesync_services` with filtered uniq service names when in bootc mode
set_fact:
timesync_services: "{{ timesync_services_output.stdout_lines |
select('search', ' enabled$') |
map('regex_replace', ' +enabled$', '') |
map('regex_replace', '[.]service.*$', '') |
map('regex_replace', '@$', '') |
unique |
list }}"
when: not __timesync_is_booted | d(false)

- name: Check that variable 'timesync_services' is defined
assert:
Expand Down Expand Up @@ -250,7 +269,7 @@
- name: Disable chronyd
service:
name: chronyd
state: stopped
state: "{{ 'stopped' if __timesync_is_booted else omit }}"
enabled: false
when:
- timesync_mode != 1 or timesync_ntp_provider != 'chrony'
Expand All @@ -264,7 +283,7 @@
- name: Disable ntpd
service:
name: ntpd
state: stopped
state: "{{ 'stopped' if __timesync_is_booted else omit }}"
enabled: false
when:
- timesync_mode != 1 or timesync_ntp_provider != 'ntp'
Expand All @@ -278,7 +297,7 @@
- name: Disable ntpdate
service:
name: ntpdate
state: stopped
state: "{{ 'stopped' if __timesync_is_booted else omit }}"
enabled: false
when: "'ntpdate' in timesync_services"
register: __disable_result
Expand All @@ -290,7 +309,7 @@
- name: Disable sntp
service:
name: sntp
state: stopped
state: "{{ 'stopped' if __timesync_is_booted else omit }}"
enabled: false
when: "'sntp' in timesync_services"
register: __disable_result
Expand All @@ -302,7 +321,7 @@
- name: Disable ptp4l
service:
name: ptp4l
state: stopped
state: "{{ 'stopped' if __timesync_is_booted else omit }}"
enabled: false
when:
- timesync_mode != 2
Expand All @@ -316,7 +335,7 @@
- name: Disable phc2sys
service:
name: phc2sys
state: stopped
state: "{{ 'stopped' if __timesync_is_booted else omit }}"
enabled: false
when:
- timesync_mode != 2 or not timesync_mode2_hwts
Expand All @@ -334,7 +353,7 @@
__timemstr: timemaster
service:
name: "{{ __timemstr }}"
state: stopped
state: "{{ 'stopped' if __timesync_is_booted else omit }}"
enabled: false
when:
- timesync_mode != 3
Expand All @@ -348,7 +367,7 @@
- name: Enable chronyd
service:
name: chronyd
state: started
state: "{{ 'started' if __timesync_is_booted else omit }}"
enabled: true
when:
- timesync_mode == 1
Expand All @@ -357,7 +376,7 @@
- name: Enable ntpd
service:
name: ntpd
state: started
state: "{{ 'started' if __timesync_is_booted else omit }}"
enabled: true
when:
- timesync_mode == 1
Expand All @@ -366,14 +385,14 @@
- name: Enable ptp4l
service:
name: ptp4l
state: started
state: "{{ 'started' if __timesync_is_booted else omit }}"
enabled: true
when: timesync_mode == 2

- name: Enable phc2sys
service:
name: phc2sys
state: started
state: "{{ 'started' if __timesync_is_booted else omit }}"
enabled: true
when:
- timesync_mode == 2
Expand All @@ -384,6 +403,6 @@
service:
# wokeignore:rule=master
name: timemaster
state: started
state: "{{ 'started' if __timesync_is_booted else omit }}"
enabled: true
when: timesync_mode == 3
21 changes: 21 additions & 0 deletions tasks/set_vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,24 @@
- "default.yml"
paths:
- "{{ role_path }}/vars"

- name: Determine if system is booted with systemd
when: __timesync_is_booted is not defined
block:
- name: Run systemctl
# noqa command-instead-of-module
command: systemctl is-system-running
register: __is_system_running
changed_when: false
failed_when: false
check_mode: false

- name: Require installed systemd
fail:
msg: "Error: This role requires systemd to be installed."
when: '"No such file or directory" in __is_system_running.msg | d("")'

- name: Set flag to indicate that systemd runtime operations are available
set_fact:
# see https://www.man7.org/linux/man-pages/man1/systemctl.1.html#:~:text=is-system-running%20output
__timesync_is_booted: "{{ __is_system_running.stdout != 'offline' }}"
15 changes: 10 additions & 5 deletions tests/inventory.yaml.j2
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
all:
hosts:
{{ inventory_hostname }}:
{% for key in ["ansible_all_ipv4_addresses", "ansible_all_ipv6_addresses",
{% if __ansible_connection in ["local", "buildah"] %}
ansible_connection: {{ __ansible_connection }}
ansible_host: localhost
{% else %}
{% for key in ["ansible_all_ipv4_addresses", "ansible_all_ipv6_addresses",
"ansible_default_ipv4", "ansible_default_ipv6", "ansible_host",
"ansible_port", "ansible_ssh_common_args",
"ansible_ssh_private_key_file","ansible_user"] %}
{% if key in hostvars[inventory_hostname] %}
{{ key }}: {{ hostvars[inventory_hostname][key] }}
{% endif %}
{% endfor %}
{% if key in hostvars[inventory_hostname] %}
{{ key }}: {{ hostvars[inventory_hostname][key] | to_json }}
{% endif %}
{% endfor %}
{% endif %}
33 changes: 11 additions & 22 deletions tests/tasks/setup.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
---
# common test setup tasks
- name: Determine if system is ostree and set flag
when: not __timesync_is_ostree is defined
block:
- name: Check if system is ostree
stat:
path: /run/ostree-booted
register: __ostree_booted_stat
- name: Run the role only to get vars needed for validation
include_role:
name: linux-system-roles.timesync
public: true
tasks_from: set_vars.yml

- name: Set flag to indicate system is ostree
set_fact:
__timesync_is_ostree: "{{ __ostree_booted_stat.stat.exists }}"
- name: Skip test on ostree systems if unsupported
meta: end_host
when:
- __timesync_ostree_unsupported | d(false)
- __timesync_is_ostree | d(false)

- name: Ensure iproute for gathering default_ipv4 fact
package:
Expand All @@ -25,20 +25,9 @@
when: __required_facts |
difference(ansible_facts.keys() | list) | length > 0
vars:
__required_facts:
- default_ipv4
- distribution
- distribution_major_version
- distribution_version
- os_family
__required_facts: "{{ __timesync_required_facts + ['default_ipv4'] }}"
__required_facts_subsets: "{{ ['!all', '!min'] + __required_facts }}"

- name: Debug
debug:
msg: facts {{ ansible_facts | to_nice_json }}

- name: Skip test on ostree systems if unsupported
meta: end_host
when:
- __timesync_ostree_unsupported | d(false)
- __timesync_is_ostree | d(false)
53 changes: 43 additions & 10 deletions tests/tests_chrony.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,59 @@
include_role:
name: linux-system-roles.timesync
public: true
when: not __bootc_validation | d(false)

- name: Run the role only to get vars needed for validation
include_role:
name: linux-system-roles.timesync
public: true
tasks_from: set_vars.yml
when: __bootc_validation | d(false)

- name: Run chrony tests
tags: tests::verify
block:
- name: Flush handlers
meta: flush_handlers
when: not __bootc_validation | d(false)

- name: Wait for services to start
wait_for:
timeout: 2
when: not __bootc_validation | d(false)

- name: Create QEMU deployment during bootc end-to-end test
delegate_to: localhost
command: "{{ lsr_scriptdir | quote }}/bootc-buildah-qcow.sh {{ ansible_host | quote }}"
changed_when: true
when: ansible_connection == "buildah"

- name: Check headers for ansible_managed, fingerprint
include_tasks: tasks/check_header.yml
loop:
- "{{ timesync_chrony_conf_path }}"
- "{{ timesync_chrony_sysconfig_path }}"
loop_control:
loop_var: __file
vars:
__fingerprint: "system_role:timesync"

# check settings in files - only check we can do in non-booted mode
- name: Check for settings in chrony.conf
command: grep -q {{ item | quote }} {{ timesync_chrony_conf_path }}
loop: "{{ timesync_chrony_custom_settings +
(timesync_ntp_servers | selectattr('hostname') | map(attribute='hostname') | list) +
['makestep ' ~ timesync_step_threshold ~ ' 3'] +
([sourcedirstr] if sourcedirstr | length > 0 else []) }}"
vars:
sourcedirstr: "{{ 'sourcedir ' ~ timesync_chrony_dhcp_sourcedir
if timesync_dhcp_ntp_servers and timesync_chrony_dhcp_sourcedir
else '' }}"
changed_when: false

- name: Skip remaining checks unless booted
meta: end_host
when: not __timesync_is_booted

- name: Get list of currently used time sources
shell: chronyc -n sources || ntpq -pn
Expand All @@ -58,16 +101,6 @@
that:
- "'tracking.log' in logfiles.stdout"

- name: Check headers for ansible_managed, fingerprint
include_tasks: tasks/check_header.yml
loop:
- "{{ timesync_chrony_conf_path }}"
- "{{ timesync_chrony_sysconfig_path }}"
loop_control:
loop_var: __file
vars:
__fingerprint: "system_role:timesync"

always:
- name: Cleanup after tests
include_tasks: tasks/cleanup.yml
13 changes: 10 additions & 3 deletions tests/tests_default_wrapper.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
---
- name: Create static inventory from hostvars
tags:
- 'tests::slow'
- tests::slow
- tests::booted
hosts: all
tasks:
- name: Save connection method
set_fact:
__ansible_connection: "{{ ansible_connection }}"

- name: Create temporary file
tempfile:
state: file
Expand All @@ -20,12 +25,14 @@

- name: Run tests_default.yml normally
tags:
- 'tests::slow'
- tests::slow
- tests::booted
import_playbook: tests_default.yml

- name: Run tests_default.yml in check_mode
tags:
- 'tests::slow'
- tests::slow
- tests::booted
hosts: all
tasks:
- name: Run ansible-playbook with tests_default.yml in check mode
Expand Down
Loading
Loading