Skip to content

Commit f42c6ab

Browse files
committed
feat: add support for timesync_ntp_ip_family
Feature: Add support for timesync_ntp_ip_family to allow setting the `-4` or `-6` OPTIONS in the chronyd or ntpd sysconfig file. Reason: When IPv6 is disabled on the node, you must tell chronyd to only listen for IPv4 using OPTIONS="-4" in the sysconfig file. Otherwise, chronyd will log error messages when binding to IPv6 sockets It's also useful to prevent the client from using IPv6 servers when IPv4 is known to work better (e.g. IPv6 over a tunnel). Result: chronyd and ntpd can be configured to work correctly, and the services will not log errors, when IPv6 (or IPv4) is disabled on the node. Signed-off-by: Rich Megginson <[email protected]>
1 parent 43f03f0 commit f42c6ab

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ timesync_chrony_custom_settings:
121121
# the role will fail to ensure the reboot requirement is not overlooked.
122122
# For non-transactional update systems, this variable is ignored.
123123
timesync_transactional_update_reboot_ok: true
124+
125+
# This option is useful on systems where IPv6 or IPv4 are disabled.
126+
# chronyd will work on a IPv6-disabled host without -4, but it logs error messages
127+
# when binding to the IPv6 sockets fails. Adding the -4 option disables those sockets
128+
# and there are no error messages. It's also useful to prevent the client from using
129+
# IPv6 servers when IPv4 is known to work better (e.g. IPv6 over a tunnel).
130+
# Corresponds to the `-4` and `-6` OPTIONS for chronyd and ntpd. Values are:
131+
# * IPv4 - use only IPv4
132+
# * IPv6 - use only IPv6
133+
# * all - use both IPv4 and IPv6
134+
# Default is all
135+
timesync_ntp_ip_family: all
124136
```
125137
126138
## Example Playbooks

defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ timesync_ntp_hwts_interfaces: []
88
timesync_ntp_provider: ""
99
timesync_max_distance: 0
1010
timesync_transactional_update_reboot_ok: null
11+
# options are all, IPv4, IPv6
12+
# default is none which is platform default
13+
timesync_ntp_ip_family: ""

templates/chronyd.sysconfig.j2

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{{ ansible_managed | comment }}
22
{{ "system_role:timesync" | comment(prefix="", postfix="") }}
33

4-
OPTIONS=""
4+
OPTIONS="{{ ' -4' if timesync_ntp_ip_family == 'IPv4'
5+
else ' -6' if timesync_ntp_ip_family == 'IPv6'
6+
else '' }}"

templates/ntpd.sysconfig.j2

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{{ ansible_managed | comment }}
22
{{ "system_role:timesync" | comment(prefix="", postfix="") }}
33

4-
OPTIONS="-g{{ ' -u ntp:ntp -p /var/run/ntpd.pid' if ansible_distribution in ['OracleLinux', 'RedHat', 'CentOS'] and ansible_distribution_major_version | int < 7 else '' }}"
4+
OPTIONS="-g{{ ' -u ntp:ntp -p /var/run/ntpd.pid' if ansible_distribution in ['OracleLinux', 'RedHat', 'CentOS'] and ansible_distribution_major_version | int < 7 else '' }}{{
5+
' -4' if timesync_ntp_ip_family == 'IPv4'
6+
else ' -6' if timesync_ntp_ip_family == 'IPv6'
7+
else '' }}"

tests/tests_default_vars.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
- timesync_ntp_hwts_interfaces
2020
- timesync_ntp_provider
2121
- timesync_max_distance
22+
- timesync_ntp_ip_family

tests/tests_options.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
- name: Test setting OPTIONS
3+
hosts: all
4+
gather_facts: true
5+
tasks:
6+
- name: Run tests
7+
block:
8+
- name: Run role with no arguments to get provider
9+
include_role:
10+
name: linux-system-roles.timesync
11+
public: true
12+
13+
- name: Set vars based on provider
14+
set_fact:
15+
__test_file: "{{ timesync_chrony_sysconfig_path
16+
if timesync_ntp_provider_current == 'chrony'
17+
else timesync_ntp_sysconfig_path }}"
18+
19+
- name: Try timesync_ntp_ip_family IPv4
20+
include_role:
21+
name: linux-system-roles.timesync
22+
vars:
23+
timesync_ntp_ip_family: IPv4
24+
25+
- name: Verify IPv4 setting
26+
command: grep 'OPTIONS=.* -4' {{ __test_file }}
27+
changed_when: false
28+
29+
- name: Try timesync_ntp_ip_family IPv6
30+
include_role:
31+
name: linux-system-roles.timesync
32+
vars:
33+
timesync_ntp_ip_family: IPv6
34+
35+
- name: Verify IPv6 setting
36+
command: grep 'OPTIONS=.* -6' {{ __test_file }}
37+
changed_when: false
38+
39+
always:
40+
- name: Reset OPTIONS
41+
include_role:
42+
name: linux-system-roles.timesync
43+
vars:
44+
timesync_ntp_ip_family: all
45+
46+
- name: Verify reset
47+
shell: |
48+
set -eux
49+
if grep 'OPTIONS=.* -4' {{ __test_file }} || \
50+
grep 'OPTIONS=.* -6' {{ __test_file }}; then
51+
echo ERROR: {{ __test_file }} has incorrect OPTIONS
52+
exit 1
53+
fi
54+
exit 0
55+
changed_when: false

0 commit comments

Comments
 (0)