ansible 时间同步 实例
目录结构
hosts
deployment- timesyncclient.yml
roles- timesyncclient- tasks-main.yml- templater- files
vim hosts
[timesyncclient]
192.168.106.130 hostname=rocky90-106-130
[aliyuan]
192.168.106.130 hostname=rocky90-106-130
[init]
192.168.106.130 hostname=rocky90-106-130[all:vars]
ansible_ssh_user=root
ansible_ssh_pass=kc@123456
ansible_ssh_port=22
deployment/timesyncclient.yml
---
- hosts: timesyncclientsudo: yesroles: - ../roles/timesyncclient
vim roles/timesyncclient/tasks/main.yml
---
### 安装并配置 chrony 客户端
########### to centos or rockylinux #############
# 安装
- name: Cenots7.x_Rocky9.x_install_chronyyum:name: chronystate: presentwhen: ansible_distribution=="CentOS" or ansible_distribution=="Rocky"
# 删除^pool.*行
- name: Cenots7.x_Rocky9.x_delete_chrony.conf '^pool.*' string linelineinfile:path: /etc/chrony.confregexp: '^pool.*'state: absentwhen: ansible_distribution=="CentOS" or ansible_distribution=="Rocky"
# 删除行server.*行
- name: Cenots7.x_Rocky9.x_delete_chrony.conf '^server.*' string linelineinfile:path: /etc/chrony.confregexp: '^server.*'state: absentwhen: ansible_distribution=="CentOS" or ansible_distribution=="Rocky"
# 添加时间时间服务器
- name: Cenots7.x_Rocky9.x_add_time_server TIME_SERVER1 and TIME_SERVER2lineinfile:path: /etc/chrony.confinsertafter: '^# Please consider .*'line: "server {{ time_sync_service_url_1 }} iburst\nserver {{ time_sync_service_url_2 }} iburst"when: ansible_distribution=="CentOS" or ansible_distribution=="Rocky"
# 重启 chrony 服务
- name: Cenots7.x_Rocky9.x_restart_chrony_serviceservice:name: chronydstate: restartedenabled: yes############### to ubuntu server ##################
# 删除锁文件
- name: Ubuntu_delete_lock_filesfile:path: "{{ item }}"state: absentloop:- /var/lib/dpkg/lock- /var/lib/apt/lists/lock- /var/cache/apt/archives/lockwhen: ansible_distribution=="Ubuntu"
# 更新源
- name: Ubuntu_apt_updateapt:update_cache: yes force: yes when: ansible_distribution=="Ubuntu"
# 安装
- name: Ubuntu_install_chronyapt:name: chronyforce: yeswhen: ansible_distribution=="Ubuntu"
# 删除^pool.*行
- name: Ubuntu_delete_chrony.conf '^pool.*' string linelineinfile:path: /etc/chrony/chrony.confregexp: '^pool.*'state: absentwhen: ansible_distribution=="Ubuntu"
# 添加时间时间服务器
- name: Ubuntu_add_time_server TIME_SERVER1 and TIME_SERVER2lineinfile:path: /etc/chrony.confinsertafter: '^# Please consider .*'line: "server {{ time_sync_service_url_1 }} iburst\nserver {{ time_sync_service_url_2 }} iburst"
# 开机启动 chronyd
- name: start chronydsystemd:name: chronydstate: restarted#state: startedenabled: yes
执行命令
ansible-playbook -i hosts deployment/timesyncclient.yml
本文来自博客园,站在巨人的肩膀上,坚持开源精神,遵循开源协议:Apache Licene 2.0协议。
