zl程序教程

您现在的位置是:首页 >  其他

当前栏目

a7.ansible 生产实战案例 -- chrony客户端roles

案例客户端 -- 实战 生产 Ansible 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