# ntp.yml
- name: 為系統設置時鐘同步
hosts: centossrvs
# 定義變量
vars:
ntp_srv: server ntp1.aliyun.com # NTP服務器地址使用阿里云的
# 定義任務
tasks:
- name: 設置時區
community.general.timezone:
name: Asia/Shanghai
- name: 安裝chrony
ansible.builtin.yum:
name: chrony
state: present
- name: 設置時鐘同步源
ansible.builtin.lineinfile:
dest: /etc/chrony.conf
regexp: 'centos.pool.ntp.org'
line: '{{ ntp_srv }}'
notify:
- restart chrony # 通知重啟chrony
# 定義處理程序
handlers:
- name: restart chrony
ansible.builtin.service:
name: chronyd
state: restarted
enabled: yes # 開機自動啟動
# ntp_hosts
all:
children:
centossrvs:
hosts:
192.168.0.122:
192.168.0.121:
root@test:/home/test# ansible-playbook -i ntp_hosts ntp.yml
PLAY [為系統設置時鐘同步] ***********************************************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [192.168.0.112]
ok: [192.168.0.111]
TASK [設置時區] *********************************************************************************************************************************************************************************************
changed: [192.168.0.111]
changed: [192.168.0.112]
TASK [安裝chrony] *******************************************************************************************************************************************************************************************
ok: [192.168.0.111]
ok: [192.168.0.112]
TASK [設置時鐘同步源] ***************************************************************************************************************************************************************************************
changed: [192.168.0.112]
changed: [192.168.0.111]
RUNNING HANDLER [restart chrony] ****************************************************************************************************************************************************************************
changed: [192.168.0.112]
changed: [192.168.0.111]
PLAY RECAP **************************************************************************************************************************************************************************************************
192.168.0.111 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.0.112 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0