a7.ansible 生产实战案例 -- chrony客户端roles
2023-09-14 09:15:22 时间
chrony客户端roles
源码下载地址:https://github.com/raymond999999/ansible
[root@ansible-server ansible]# mkdir -p roles/chrony-client/{tasks,handlers,vars}
[root@ansible-server ansible]# cd roles/chrony-client/
[root@ansible-server chrony-client]# ls
handlers tasks vars
[root@ansible-server chrony-client]# vim vars/main.yml
SERVER1: 172.31.0.101
SERVER2: 172.31.0.104
[root@ansible-server chrony-client]# vim tasks/install_chrony_yum.yml
- 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 {{ SERVER1 }} iburst\nserver {{ SERVER2 }} iburst"
when:
- (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")
notify:
- restart chronyd
[root@ansible-server chrony-client]# vim tasks/install_chrony_apt.yml
- 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 {{ SERVER1 }} iburst\nserver {{ SERVER2 }} iburst"
when:
- ansible_distribution=="Ubuntu"
notify:
- restart chronyd
[root@ansible-server chrony-client]# vim tasks/service.yml
- name: start chronyd
systemd:
name: chronyd
state: started
enabled: yes
[root@ansible-server chrony-client]# vim tasks/main.yml
- include: install_chrony_yum.yml
- include: install_chrony_apt.yml
- include: service.yml
[root@ansible-server chrony-client]# vim handlers/main.yml
- name: restart chronyd
systemd:
name: chronyd
state: restarted
[root@ansible-server chrony-client]# cd ../../
[root@ansible-server ansible]# tree roles/chrony-client/
roles/chrony-client/
├── handlers
│ └── main.yml
├── tasks
│ ├── install_chrony_apt.yml
│ ├── install_chrony_yum.yml
│ ├── main.yml
│ └── service.yml
└── vars
└── main.yml
3 directories, 6 files
[root@ansible-server ansible]# vim chrony_client_role.yml
---
- hosts: chronyclient
roles:
- role: chrony-client
[root@ansible-server ansible]# ansible-playbook chrony_client_role.yml
PLAY [chronyclient] ***************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************
ok: [172.31.0.103]
ok: [172.31.0.102]
ok: [172.31.0.105]
TASK [chrony-client : install CentOS or Rocky chrony] *****************************************************************************************
skipping: [172.31.0.105]
changed: [172.31.0.103]
changed: [172.31.0.102]
TASK [chrony-client : delete CentOS or Rocky /etc/chrony.conf file contains '^pool.*' string line] ********************************************
skipping: [172.31.0.105]
ok: [172.31.0.103]
changed: [172.31.0.102]
TASK [chrony-client : delete CentOS or Rocky /etc/chrony.conf file contains '^server.*' string line] ******************************************
skipping: [172.31.0.105]
changed: [172.31.0.103]
ok: [172.31.0.102]
TASK [chrony-client : add Time server for CentOS or Rocky /etc/chrony.conf file] **************************************************************
skipping: [172.31.0.105]
changed: [172.31.0.103]
changed: [172.31.0.102]
TASK [chrony-client : delete lock files] ******************************************************************************************************
skipping: [172.31.0.102] => (item=/var/lib/dpkg/lock)
skipping: [172.31.0.102] => (item=/var/lib/apt/lists/lock)
skipping: [172.31.0.102] => (item=/var/cache/apt/archives/lock)
skipping: [172.31.0.103] => (item=/var/lib/dpkg/lock)
skipping: [172.31.0.103] => (item=/var/lib/apt/lists/lock)
skipping: [172.31.0.103] => (item=/var/cache/apt/archives/lock)
changed: [172.31.0.105] => (item=/var/lib/dpkg/lock)
changed: [172.31.0.105] => (item=/var/lib/apt/lists/lock)
changed: [172.31.0.105] => (item=/var/cache/apt/archives/lock)
TASK [chrony-client : apt update] *************************************************************************************************************
skipping: [172.31.0.102]
skipping: [172.31.0.103]
changed: [172.31.0.105]
TASK [chrony-client : install Ubuntu chrony] **************************************************************************************************
skipping: [172.31.0.102]
skipping: [172.31.0.103]
changed: [172.31.0.105]
TASK [chrony-client : delete Ubuntu /etc/chrony/chrony.conf file contains '^pool.*' string line] **********************************************
skipping: [172.31.0.102]
skipping: [172.31.0.103]
changed: [172.31.0.105]
TASK [chrony-client : add Time server for Ubuntu /etc/chrony/chrony.conf file] ****************************************************************
skipping: [172.31.0.102]
skipping: [172.31.0.103]
changed: [172.31.0.105]
TASK [chrony-client : start chronyd] **********************************************************************************************************
changed: [172.31.0.103]
ok: [172.31.0.105]
changed: [172.31.0.102]
RUNNING HANDLER [chrony-client : restart chronyd] *********************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.105]
changed: [172.31.0.102]
PLAY RECAP ************************************************************************************************************************************
172.31.0.102 : ok=7 changed=5 unreachable=0 failed=0 skipped=5 rescued=0 ignored=0
172.31.0.103 : ok=7 changed=5 unreachable=0 failed=0 skipped=5 rescued=0 ignored=0
172.31.0.105 : ok=8 changed=6 unreachable=0 failed=0 skipped=4 rescued=0 ignored=0
[root@centos8-client ~]# chronyc sources -nv
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 172.31.0.101 3 6 17 31 +220us[ +249us] +/- 31ms
^+ 172.31.0.104 3 6 17 32 -155us[ -155us] +/- 36ms
[root@centos7-client ~]# chronyc sources -nv
210 Number of sources = 2
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 172.31.0.101 3 6 37 58 +19us[-3541us] +/- 31ms
^+ 172.31.0.104 3 6 37 58 -21us[-3574us] +/- 33ms
root@ubuntu2004-client:~# chronyc sources -nv
210 Number of sources = 2
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 172.31.0.101 3 6 77 11 +123us[ +362us] +/- 32ms
^+ 172.31.0.104 3 6 77 10 -569us[ -569us] +/- 32ms
相关文章
- Socket通讯成功案例TCP/UDP
- Microsoft Visual Studio Installer Projects 安装包的制作案例--------打包Winform安装程序以及创建桌面快捷方式和菜单程序以及设置卸载和开机启动
- FluentValidation具体使用案例
- 纯js实现人脸识别眨眨眼张张嘴案例——ccv.js
- Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
- identity4 系列————纯js客户端案例篇[四]
- 13个应用案例,讲述最真实的大数据故事
- Atiti attilax主要成果与解决方案与案例rsm版 v2
- BigData之Storm:Apache Storm的简介、深入理解、下载、案例应用之详细攻略
- DataScience:数据预处理/特征工程之两大类别特征、四大数据类型(数值型/类别型/字符串型/时间型简介、代码实现(数值型特征离散化/类别型特征编码数字化)、案例应用之详细攻略
- DL之SSD:SSD算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略
- 案例复现,带你分析Priority Blocking Queue比较器异常导致的NPE问题
- Python 实操案例:一文详解10种聚类算法
- 智能优化算法应用:基于麻雀搜索算法的工程优化案例
- struts2充分国际化案例 错误解决
- 大咖or学员?金融&银行软件测试案例分享!
- Autolisp:利用AuoCAD之Lisp编程案例之自动智能获取所选对象的面积并标注在指定位置