# 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