chrony服务端playbook
[root@ansible-server ansible]
[chronyserver]
172.31.0.101
172.31.0.104
[chronyclient]
172.31.0.102
172.31.0.103
172.31.0.105
[root@ansible-server ansible]
[root@ansible-server ansible]
[root@ansible-server chrony]
---
- hosts: chronyserver
tasks:
- name: install CentOS or Rocky chrony
yum:
name: chrony
when:
- (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")
- name: delete CentOS or Rocky /etc/chrony.conf file contains '^pool.*' string line
lineinfile:
path: /etc/chrony.conf
regexp: '^pool.*'
state: absent
when:
- (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")
notify:
- restart chronyd
- name: delete CentOS or Rocky /etc/chrony.conf file contains '^server.*' string line
lineinfile:
path: /etc/chrony.conf
regexp: '^server.*'
state: absent
when:
- (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")
notify:
- restart chronyd
- name: add Time server for CentOS or Rocky /etc/chrony.conf file
lineinfile:
path: /etc/chrony.conf
insertafter: '^# Please consider .*'
line: "server ntp.aliyun.com iburst\nserver time1.cloud.tencent.com iburst\nserver ntp.tuna.tsinghua.edu.cn iburst"
when:
- (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")
notify:
- restart chronyd
- name: Substitution '^#(allow).*' string for CentOS or Rocky /etc/chrony.conf file
replace:
path: /etc/chrony.conf
regexp: '^#(allow).*'
replace: '\1 0.0.0.0/0'
when:
- (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")
notify:
- restart chronyd
- name: Substitution '^#(local).*' string for CentOS or Rocky /etc/chrony.conf file
replace:
path: /etc/chrony.conf
regexp: '^#(local).*'
replace: '\1 stratum 10'
when:
- (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")
notify:
- restart chronyd
- name: delete lock files
file:
path: "{{ item }}"
state: absent
loop:
- /var/lib/dpkg/lock
- /var/lib/apt/lists/lock
- /var/cache/apt/archives/lock
when:
- ansible_distribution=="Ubuntu"
- name: apt update
apt:
update_cache: yes
force: yes
when:
- ansible_distribution=="Ubuntu"
- name: install Ubuntu chrony
apt:
name: chrony
force: yes
when:
- ansible_distribution=="Ubuntu"
- name: delete Ubuntu /etc/chrony/chrony.conf file contains '^pool.*' string line
lineinfile:
path: /etc/chrony/chrony.conf
regexp: '^pool.*'
state: absent
when:
- ansible_distribution=="Ubuntu"
notify:
- restart chronyd
- name: add Time server for Ubuntu /etc/chrony/chrony.conf file
lineinfile:
path: /etc/chrony/chrony.conf
insertafter: '^# See http:.*'
line: "server ntp.aliyun.com iburst\nserver time1.cloud.tencent.com iburst\nserver ntp.tuna.tsinghua.edu.cn iburst"
when:
- ansible_distribution=="Ubuntu"
notify:
- restart chronyd
- name: add 'allow 0.0.0.0/0' string and 'local stratum 10' string for Ubuntu /etc/chrony/chrony.conf file
lineinfile:
path: /etc/chrony/chrony.conf
line: "{{ item }}"
loop:
- "allow 0.0.0.0/0"
- "local stratum 10"
when:
- ansible_distribution=="Ubuntu"
notify:
- restart chronyd
- name: start chronyd
systemd:
name: chronyd
state: started
enabled: yes
handlers:
- name: restart chronyd
service:
name: chronyd
state: restarted
[root@ansible-server playbook]
[root@ansible-server ansible]
PLAY [chronyserver] ***************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************
ok: [172.31.0.104]
ok: [172.31.0.101]
TASK [install CentOS or Rocky chrony] *********************************************************************************************************
skipping: [172.31.0.104]
changed: [172.31.0.101]
TASK [delete CentOS or Rocky /etc/chrony.conf file contains '^pool.*' string line] ************************************************************
skipping: [172.31.0.104]
changed: [172.31.0.101]
TASK [delete CentOS or Rocky /etc/chrony.conf file contains '^server.*' string line] **********************************************************
skipping: [172.31.0.104]
ok: [172.31.0.101]
TASK [add Time server for CentOS or Rocky /etc/chrony.conf file] ******************************************************************************
skipping: [172.31.0.104]
changed: [172.31.0.101]
TASK [Substitution '^#(allow).*' string for CentOS or Rocky /etc/chrony.conf file] ************************************************************
skipping: [172.31.0.104]
changed: [172.31.0.101]
TASK [Substitution '^#(local).*' string for CentOS or Rocky /etc/chrony.conf file] ************************************************************
skipping: [172.31.0.104]
changed: [172.31.0.101]
TASK [delete lock files] **********************************************************************************************************************
skipping: [172.31.0.101] => (item=/var/lib/dpkg/lock)
skipping: [172.31.0.101] => (item=/var/lib/apt/lists/lock)
skipping: [172.31.0.101] => (item=/var/cache/apt/archives/lock)
changed: [172.31.0.104] => (item=/var/lib/dpkg/lock)
changed: [172.31.0.104] => (item=/var/lib/apt/lists/lock)
changed: [172.31.0.104] => (item=/var/cache/apt/archives/lock)
TASK [apt update] *****************************************************************************************************************************
skipping: [172.31.0.101]
changed: [172.31.0.104]
TASK [install Ubuntu chrony] ******************************************************************************************************************
skipping: [172.31.0.101]
changed: [172.31.0.104]
TASK [delete Ubuntu /etc/chrony/chrony.conf file contains '^pool.*' string line] **************************************************************
skipping: [172.31.0.101]
changed: [172.31.0.104]
TASK [add Time server for Ubuntu /etc/chrony/chrony.conf file] ********************************************************************************
skipping: [172.31.0.101]
changed: [172.31.0.104]
TASK [add 'allow 0.0.0.0/0' string and 'local stratum 10' string for Ubuntu /etc/chrony/chrony.conf file] *************************************
skipping: [172.31.0.101] => (item=allow 0.0.0.0/0)
skipping: [172.31.0.101] => (item=local stratum 10)
changed: [172.31.0.104] => (item=allow 0.0.0.0/0)
changed: [172.31.0.104] => (item=local stratum 10)
TASK [start chronyd] **************************************************************************************************************************
ok: [172.31.0.104]
changed: [172.31.0.101]
RUNNING HANDLER [restart chronyd] *************************************************************************************************************
changed: [172.31.0.104]
changed: [172.31.0.101]
PLAY RECAP ************************************************************************************************************************************
172.31.0.101 : ok=9 changed=7 unreachable=0 failed=0 skipped=6 rescued=0 ignored=0
172.31.0.104 : ok=9 changed=7 unreachable=0 failed=0 skipped=6 rescued=0 ignored=0
[root@rocky8-client ~]
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88 2 6 17 27 -2352us[-8301us] +/- 35ms
^- 139.199.215.251 2 6 17 27 +11ms[ +11ms] +/- 32ms
^? 101.6.6.172 0 7 0 - +0ns[ +0ns] +/- 0ns
root@ubuntu1804-client:~
210 Number of sources = 3
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88 2 6 17 30 +51us[ +181ms] +/- 36ms
^+ 139.199.215.251 2 6 65 26 +7955us[+7955us] +/- 33ms
^? 101.6.6.172 0 7 0 - +0ns[ +0ns] +/- 0ns