Skip to content

Commit

Permalink
Merge pull request #14 from mlichvar/master
Browse files Browse the repository at this point in the history
Use timesync prefix for all variables
  • Loading branch information
pcahyna authored Jul 24, 2018
2 parents dc86e2c + f616cae commit 05acb58
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 104 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The variables that can be passed to this role are as follows:

```
# List of NTP servers
ntp_servers:
timesync_ntp_servers:
- hostname: foo.example.com # Hostname or address of the server
minpoll: 4 # Minimum polling interval (default 6)
maxpoll: 8 # Maximum polling interval (default 10)
Expand All @@ -24,7 +24,7 @@ ntp_servers:
# (default no)
# List of PTP domains
ptp_domains:
timesync_ptp_domains:
- number: 0 # PTP domain number
interfaces: [ eth0 ] # List of interfaces in the domain
delay: 0.000010 # Assumed maximum network delay to the
Expand All @@ -36,22 +36,22 @@ ptp_domains:
# (default 1)
# Flag enabling use of NTP servers provided by DHCP (default no)
dhcp_ntp_servers: no
timesync_dhcp_ntp_servers: no
# Minimum offset of the clock which can be corrected by stepping (default is
# specific to NTP/PTP implementation: chrony 1.0, ntp 0.128, linuxptp 0.00002).
# Zero threshold disables all steps.
clock_step_threshold: 1.0
timesync_step_threshold: 1.0
# Minimum number of selectable time sources required to allow synchronization
# of the clock (default 1)
min_time_sources: 1
timesync_min_sources: 1
# Name of the package which should be installed and configured for NTP.
# Possible values are "chrony" and "ntp". If not defined, the currently active
# or enabled service will be configured. If no service is active or enabled, a
# package specific to the system and its version will be selected.
ntp_provider: chrony
timesync_ntp_provider: chrony
```

Example Playbook
Expand All @@ -62,7 +62,7 @@ Install and configure ntp to synchronize the system clock with three NTP servers
```
- hosts: targets
vars:
ntp_servers:
timesync_ntp_servers:
- hostname: foo.example.com
iburst: yes
- hostname: bar.example.com
Expand All @@ -79,7 +79,7 @@ grandmaster in PTP domain number 0, which is accessible on interface eth0:
```
- hosts: targets
vars:
ptp_domains:
timesync_ptp_domains:
- number: 0
interfaces: [ eth0 ]
roles:
Expand All @@ -93,14 +93,14 @@ synchronization:
```
- hosts: targets
vars:
ntp_servers:
timesync_ntp_servers:
- hostname: foo.example.com
maxpoll: 6
- hostname: bar.example.com
maxpoll: 6
- hostname: baz.example.com
maxpoll: 6
ptp_domains:
timesync_ptp_domains:
- number: 0
interfaces: [ eth0, eth1 ]
transport: L2
Expand Down
10 changes: 5 additions & 5 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
ntp_servers: []
ptp_domains: []
dhcp_ntp_servers: false
clock_step_threshold: -1.0
min_time_sources: 1
timesync_ntp_servers: []
timesync_ptp_domains: []
timesync_dhcp_ntp_servers: false
timesync_step_threshold: -1.0
timesync_min_sources: 1
2 changes: 1 addition & 1 deletion library/timesync_provider.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ case $ntp_provider_count in
esac

if [ -z "$error_message" ]; then
printf '{"ansible_facts": {"ntp_provider_current": "%s"}}' "$ntp_provider_current"
printf '{"ansible_facts": {"timesync_ntp_provider_current": "%s"}}' "$ntp_provider_current"
else
printf '{"failed": "True", "msg": "%s"}' "$error_message"
fi
88 changes: 44 additions & 44 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,107 @@

- name: Check if only NTP is needed
set_fact:
sync_mode: 1
when: ptp_domains|length == 0
timesync_mode: 1
when: timesync_ptp_domains|length == 0

- name: Check if single PTP is needed
set_fact:
sync_mode: 2
when: sync_mode is not defined and (ntp_servers|length == 0) and ptp_domains|length == 1 and ptp_domains[0].interfaces|length == 1
timesync_mode: 2
when: timesync_mode is not defined and (timesync_ntp_servers|length == 0) and timesync_ptp_domains|length == 1 and timesync_ptp_domains[0].interfaces|length == 1

- name: Check if both NTP and PTP are needed
set_fact:
sync_mode: 3
when: sync_mode is not defined
timesync_mode: 3
when: timesync_mode is not defined

- name: Determine current NTP provider
timesync_provider:
when: sync_mode != 2 and (ntp_provider is not defined or ntp_provider == '')
when: timesync_mode != 2 and (timesync_ntp_provider is not defined or timesync_ntp_provider == '')

- name: Select NTP provider
set_fact:
ntp_provider: "{{ ntp_provider_default if ntp_provider_current == '' else ntp_provider_current }}"
when: sync_mode != 2 and (ntp_provider is not defined or ntp_provider == '')
timesync_ntp_provider: "{{ timesync_ntp_provider_default if timesync_ntp_provider_current == '' else timesync_ntp_provider_current }}"
when: timesync_mode != 2 and (timesync_ntp_provider is not defined or timesync_ntp_provider == '')

- name: Install chrony
package: name=chrony state=present
when: sync_mode != 2 and ntp_provider == 'chrony'
when: timesync_mode != 2 and timesync_ntp_provider == 'chrony'

- name: Install ntp
package: name=ntp state=present
when: sync_mode != 2 and ntp_provider == 'ntp'
when: timesync_mode != 2 and timesync_ntp_provider == 'ntp'

- name: Install linuxptp
package: name=linuxptp state=present
when: sync_mode != 1
when: timesync_mode != 1

- name: Run phc_ctl on PTP interface
command: phc_ctl -q {{ ptp_domains[0].interfaces[0] }}
register: phc_ctl_output
command: phc_ctl -q {{ timesync_ptp_domains[0].interfaces[0] }}
register: timesync_phc_ctl_output
changed_when: False
when: sync_mode == 2
when: timesync_mode == 2
ignore_errors: yes

- name: Check if PTP interface supports HW timestamping
set_fact:
mode2_hwts: "{{ phc_ctl_output.rc == 0 }}"
when: sync_mode == 2
timesync_mode2_hwts: "{{ timesync_phc_ctl_output.rc == 0 }}"
when: timesync_mode == 2

- name: Get chrony version
command: rpm -q --qf '%{version}' chrony
args:
warn: no
register: chrony_version
register: timesync_chrony_version
changed_when: False
when: sync_mode != 2 and ntp_provider == 'chrony'
when: timesync_mode != 2 and timesync_ntp_provider == 'chrony'

- name: Get ntp version
command: rpm -q --qf '%{version}' ntp
args:
warn: no
register: ntp_version
register: timesync_ntp_version
changed_when: False
when: sync_mode != 2 and ntp_provider == 'ntp'
when: timesync_mode != 2 and timesync_ntp_provider == 'ntp'

- name: Generate chrony.conf file
template: src=chrony.conf.j2 dest=/etc/chrony.conf backup=yes mode=0644
notify: restart {{ 'chronyd' if sync_mode == 1 else 'timemaster' }}
when: sync_mode != 2 and ntp_provider == 'chrony'
notify: restart {{ 'chronyd' if timesync_mode == 1 else 'timemaster' }}
when: timesync_mode != 2 and timesync_ntp_provider == 'chrony'

- name: Generate chronyd sysconfig file
template: src=chronyd.sysconfig.j2 dest=/etc/sysconfig/chronyd backup=yes mode=0644
notify: restart chronyd
when: sync_mode == 1 and ntp_provider == 'chrony'
when: timesync_mode == 1 and timesync_ntp_provider == 'chrony'

- name: Generate ntp.conf file
template: src=ntp.conf.j2 dest=/etc/ntp.conf backup=yes mode=0644
notify: restart {{ 'ntpd' if sync_mode == 1 else 'timemaster' }}
when: sync_mode != 2 and ntp_provider == 'ntp'
notify: restart {{ 'ntpd' if timesync_mode == 1 else 'timemaster' }}
when: timesync_mode != 2 and timesync_ntp_provider == 'ntp'

- name: Generate ntpd sysconfig file
template: src=ntpd.sysconfig.j2 dest=/etc/sysconfig/ntpd backup=yes mode=0644
notify: restart ntpd
when: sync_mode == 1 and ntp_provider == 'ntp'
when: timesync_mode == 1 and timesync_ntp_provider == 'ntp'

- name: Generate ptp4l.conf file
template: src=ptp4l.conf.j2 dest=/etc/ptp4l.conf backup=yes mode=0644
notify: restart ptp4l
when: sync_mode == 2
when: timesync_mode == 2

- name: Generate ptp4l sysconfig file
template: src=ptp4l.sysconfig.j2 dest=/etc/sysconfig/ptp4l backup=yes mode=0644
notify: restart ptp4l
when: sync_mode == 2
when: timesync_mode == 2

- name: Generate phc2sys sysconfig file
template: src=phc2sys.sysconfig.j2 dest=/etc/sysconfig/phc2sys backup=yes mode=0644
notify: restart phc2sys
when: sync_mode == 2 and mode2_hwts
when: timesync_mode == 2 and timesync_mode2_hwts

- name: Generate timemaster.conf file
template: src=timemaster.conf.j2 dest=/etc/timemaster.conf backup=yes mode=0644
notify: restart timemaster
when: sync_mode == 3
when: timesync_mode == 3

- name: Update network sysconfig file
lineinfile:
Expand All @@ -112,18 +112,18 @@
mode: 0644
regexp: '^PEERNTP='
line: 'PEERNTP=no'
state: "{{ 'absent' if dhcp_ntp_servers else 'present' }}"
notify: restart {{ ntp_provider + 'd' }}
when: sync_mode == 1
state: "{{ 'absent' if timesync_dhcp_ntp_servers else 'present' }}"
notify: restart {{ timesync_ntp_provider + 'd' }}
when: timesync_mode == 1

- name: Disable chronyd
service: name=chronyd state=stopped enabled=no
when: sync_mode != 1 or ntp_provider != 'chrony'
when: timesync_mode != 1 or timesync_ntp_provider != 'chrony'
ignore_errors: yes

- name: Disable ntpd
service: name=ntpd state=stopped enabled=no
when: sync_mode != 1 or ntp_provider != 'ntp'
when: timesync_mode != 1 or timesync_ntp_provider != 'ntp'
ignore_errors: yes

- name: Disable ntpdate
Expand All @@ -136,35 +136,35 @@

- name: Disable ptp4l
service: name=ptp4l state=stopped enabled=no
when: sync_mode != 2
when: timesync_mode != 2
ignore_errors: yes

- name: Disable phc2sys
service: name=phc2sys state=stopped enabled=no
when: sync_mode != 2 or not mode2_hwts
when: timesync_mode != 2 or not timesync_mode2_hwts
ignore_errors: yes

- name: Disable timemaster
service: name=timemaster state=stopped enabled=no
when: sync_mode != 3
when: timesync_mode != 3
ignore_errors: yes

- name: Enable chronyd
service: name=chronyd state=started enabled=yes
when: sync_mode == 1 and ntp_provider == 'chrony'
when: timesync_mode == 1 and timesync_ntp_provider == 'chrony'

- name: Enable ntpd
service: name=ntpd state=started enabled=yes
when: sync_mode == 1 and ntp_provider == 'ntp'
when: timesync_mode == 1 and timesync_ntp_provider == 'ntp'

- name: Enable ptp4l
service: name=ptp4l state=started enabled=yes
when: sync_mode == 2
when: timesync_mode == 2

- name: Enable phc2sys
service: name=phc2sys state=started enabled=yes
when: sync_mode == 2 and mode2_hwts
when: timesync_mode == 2 and timesync_mode2_hwts

- name: Enable timemaster
service: name=timemaster state=started enabled=yes
when: sync_mode == 3
when: timesync_mode == 3
14 changes: 7 additions & 7 deletions templates/chrony.conf.j2
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
# {{ ansible_managed }}

{% for value in ntp_servers %}
{% for value in timesync_ntp_servers %}
{{ 'pool' if 'pool' in value and value['pool'] else 'server' }} {{
value['hostname'] }}{{
' minpoll {0}'.format(value['minpoll']) if 'minpoll' in value else '' }}{{
' maxpoll {0}'.format(value['maxpoll']) if 'maxpoll' in value else '' }}{{
' iburst' if 'iburst' in value and value else '' }}
{% endfor %}

{% if clock_step_threshold != 0.0 %}
{% if timesync_step_threshold != 0.0 %}
# Allow the system clock to be stepped in the first three updates.
makestep {{ clock_step_threshold if clock_step_threshold > 0.0 else '1.0' }} 3
makestep {{ timesync_step_threshold if timesync_step_threshold > 0.0 else '1.0' }} 3

{% endif %}
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

{% if min_time_sources > 1 %}
{% if timesync_min_sources > 1 %}
# Increase the minimum number of selectable sources required to adjust
# the system clock.
minsources {{ min_time_sources }}
minsources {{ timesync_min_sources }}

{% endif %}
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

{% if chrony_version.stdout|version_compare('2.0', '<') %}
{% if timesync_chrony_version.stdout|version_compare('2.0', '<') %}
# Decrease weight of stratum in source selection.
stratumweight 0.001

Expand All @@ -34,7 +34,7 @@ bindcmdaddress 127.0.0.1
bindcmdaddress ::1

{% endif %}
{% if chrony_version.stdout|version_compare('2.2', '<') %}
{% if timesync_chrony_version.stdout|version_compare('2.2', '<') %}
# Specify file containing keys for NTP and command authentication.
keyfile /etc/chrony.keys

Expand Down
12 changes: 6 additions & 6 deletions templates/ntp.conf.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# {{ ansible_managed }}

{% for value in ntp_servers %}
{% for value in timesync_ntp_servers %}
{{ 'pool' if 'pool' in value and value['pool'] else 'server' }} {{
value['hostname'] }}{{
' minpoll {0}'.format(value['minpoll']) if 'minpoll' in value else '' }}{{
Expand All @@ -13,7 +13,7 @@ driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery
{% if ntp_version.stdout|version_compare('4.2.6', '<') %}
{% if timesync_ntp_version.stdout|version_compare('4.2.6', '<') %}
restrict -6 default nomodify notrap nopeer noquery
{% endif %}

Expand All @@ -23,14 +23,14 @@ restrict -6 default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1

{% if clock_step_threshold >= 0.0 %}
{% if timesync_step_threshold >= 0.0 %}
# Specify the step threshold.
tinker step {{ clock_step_threshold }}
tinker step {{ timesync_step_threshold }}

{% endif %}
{% if min_time_sources > 1 %}
{% if timesync_min_sources > 1 %}
# Increase the minimum number of selectable sources required to adjust
# the system clock.
tos minsane {{ min_time_sources }}
tos minsane {{ timesync_min_sources }}

{% endif %}
2 changes: 1 addition & 1 deletion templates/phc2sys.sysconfig.j2
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# {{ ansible_managed }}

OPTIONS="-a -r{{ ' -F ' ~ clock_step_threshold if clock_step_threshold >= 0.0 else '' }}"
OPTIONS="-a -r{{ ' -F ' ~ timesync_step_threshold if timesync_step_threshold >= 0.0 else '' }}"
Loading

0 comments on commit 05acb58

Please sign in to comment.