a5.ansible 生产实战案例 --chrony客户端playbook

chrony客户端playbook

[root@ansible-server ansible]# cd playbook/chrony

[root@ansible-server chrony]# vars.yml 
SERVER1: 172.31.0.101                              
SERVER2: 172.31.0.104

[root@ansible-server chrony]# vim install_chrony_client.yml 
---
- hosts: chronyclient
  vars_files:
    vars.yml

  tasks:
    - 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
    - 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
    - name: start chronyd
      systemd:
        name: chronyd
        state: started
        enabled: yes

  handlers:
    - name: restart chronyd
      service:
        name: chronyd
        state: restarted

[root@ansible-server chrony]# cd ../../
[root@ansible-server ansible]# ansible-playbook playbook/chrony/install_chrony_client.yml 

PLAY [chronyclient] ***************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************
ok: [172.31.0.103]
ok: [172.31.0.105]
ok: [172.31.0.102]

TASK [install CentOS or Rocky chrony] *********************************************************************************************************
skipping: [172.31.0.105]
changed: [172.31.0.103]
changed: [172.31.0.102]

TASK [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 [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 [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 [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 [apt update] *****************************************************************************************************************************
skipping: [172.31.0.102]
skipping: [172.31.0.103]
changed: [172.31.0.105]

TASK [install Ubuntu chrony] ******************************************************************************************************************
skipping: [172.31.0.102]
skipping: [172.31.0.103]
changed: [172.31.0.105]

TASK [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 [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 [start chronyd] **************************************************************************************************************************
changed: [172.31.0.103]
ok: [172.31.0.105]
changed: [172.31.0.102]

RUNNING HANDLER [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    55   +708us[ +788us] +/-   28ms
^+ 172.31.0.104                  3   6    17    55   -902us[ -902us] +/-   32ms

[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    17     9   -522us[ -526us] +/-   34ms
^+ 172.31.0.104                  3   6    17    10   +590us[ +590us] +/-   35ms

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    17    52   -483us[ -400us] +/-   33ms
^+ 172.31.0.104                  3   6    17    52   +169us[ +252us] +/-   34ms
上一篇:【操作系统】MIT 6.s081 LAB6


下一篇:我,35岁Android开发,高龄入职鹅厂,Android高级工程师面试实战