ITPub博客

首页 > 数据库 > 数据库开发技术 > Ansible企业实战:深入解析Ansible实战案例

Ansible企业实战:深入解析Ansible实战案例

原创 数据库开发技术 作者:yj11290301 时间:2024-05-11 18:00:00 0 删除 编辑


点击蓝字,立即关注

Ansible企业实战:深入解析Ansible实战案例



Chrony企业实战分析

前言


在企业级的IT部署中,时间同步服务是确保系统正常运行的关键组成部分。它对于日志分析、事务处理和数据同步等方面至关重要。Chrony作为一个广泛使用的NTP(网络时间协议)客户端和服务器,其在企业中的自动化部署变得尤为重要。今天,我们将通过一个实战案例的展示,来深入理解如何利用Ansible来自动化部署Chrony


除了精彩的内容,我们还为大家精心准备了一份粉丝福利,记得查看文章末尾哦!



往期回顾

Ansible入门:解锁IT自动化的神器
Ansible入门:掌握Ansible命令,提升自动化运维效率
Ansible模块精通:提高效率必备技能
Ansible模块精通:掌握copy、fetch与file模块
Ansible自动化必备:三模块详解指南
Ansible自动化运维必备“四剑客”
Ansible模块:cron、Firewalld、Selinux模块深度解析
Ansible Playbook:自动化运维的神器教程


Chrony介绍



 Chrony的基本工作原理。Chrony通过与一个或多个NTP服务器进行通信,获取准确的时间信息,并根据这些信息调整本地系统的时间。这样,无论系统的硬件时钟是否准确,都能够保证系统时间的精确性。 

  然后,我们来看看如何在企业环境中部署Chrony。通常情况下,我们需要在每一台需要时间同步的服务器上安装并配置Chrony。

  这个过程中,我们可能需要编写复杂的配置文件,手动设置NTP服务器地址,甚至可能需要重启服务器来使配置生效。这个过程不仅耗时,而且容易出错。


接下来,我将介绍如何使用Ansible来实现Chrony服务的自动化配置。Ansible是一种强大的自动化工具,它可以简化和加速IT基础设施的配置和管理。通过编写Ansible playbook,我们可以定义一组任务,用于自动安装、配置和启动Chrony服务。


chrony_client(客户端)


接下来我将使用playbook 在不同的 Linux 发行版(CentOS/Rocky 和 Ubuntu)上安装和配置 chrony 时间同步服务


CentOS/Rocky 部分



  1. 安装 chrony:在 CentOS 或 Rocky 上安装 chrony。

  2. 删除默认的时间服务器:从 /etc/chrony.conf 文件中删除所有以 pool 或 server 开头的行。这些行通常包含默认的 NTP 服务器设置。

  3. 添加新的时间服务器:在 /etc/chrony.conf 文件中添加两个新的 NTP 服务器({{ SERVER1 }} 和 {{ SERVER2 }}),这两个变量应该在 vars.yml 文件中定义。

  4. 重启 chronyd 服务:当上述更改应用于配置文件时,通过 handler 重启 chronyd 服务。但是,请注意,您在删除 pool 和 server 行时都通知了重启,但通常只需要在配置完成后重启一次。

---    - hosts: chronyclient    vars_files:      - vars.yml  # 包含 SERVER1 和 SERVER2 变量的文件      tasks:      # CentOS/Rocky 部分      - name: 在 CentOS 或 Rocky 上安装 chrony        yum:          name: chrony        when:          - (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")        - name: 从 /etc/chrony.conf 文件中删除默认的 pool 行        lineinfile:          path: /etc/chrony.conf          regexp: '^pool.*'          state: absent        when:          - (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")        notify:          - 重启 chronyd 服务        - name: 从 /etc/chrony.conf 文件中删除默认的 server 行        lineinfile:          path: /etc/chrony.conf          regexp: '^server.*'          state: absent        when:          - (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")        notify:          - 重启 chronyd 服务        - name: 在 CentOS 或 Rocky 的 /etc/chrony.conf 文件中添加新的时间服务器        lineinfile:          path: /etc/chrony.conf          insertafter: '^# Please consider .*'          line: "server {{ SERVER1 }} iburst\nserver {{ SERVER2 }} iburst"        when:          - (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")        notify:          - 重启 chronyd 服务        # 重启 chronyd 服务的 handler      - name: 重启 chronyd 服务        service:          name: chronyd          state: restarted        listen: "重启 chronyd 服务"

Ubuntu 部分



  1. 删除 lock 文件:删除 Ubuntu 系统上的 APT 锁文件,以确保 APT 可以正常运行。但是,这些锁文件通常是由 APT 在运行时自动管理的,所以直接删除它们可能不是最佳做法。

  2. 更新 APT 缓存:更新 Ubuntu 的 APT 包缓存。

  3. 安装 chrony:在 Ubuntu 上安装 chrony。

  4. 删除Ubuntu的默认时间服务器:从/etc/chronychrony.conf 文件中删除所有以 pool 开头的行。但是,这里只提到了 pool 而没有提到 server,这可能是因为 Ubuntu 的默认配置可能只使用 pool。

# Ubuntu 部分      - name: 删除 Ubuntu 的 APT 锁文件(不推荐直接操作)        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: 更新 Ubuntu 的 APT 包缓存        apt:          update_cache: yes        when:          - ansible_distribution=="Ubuntu"        - name: 在 Ubuntu 上安装 chrony        apt:          name: chrony        when:          - ansible_distribution=="Ubuntu"        - name: 从 Ubuntu 的 /etc/chrony/chrony.conf 文件中删除默认的 pool 行        lineinfile:          path: /etc/chrony/chrony.conf          regexp: '^pool.*'          state: absent        when:          - ansible_distribution=="Ubuntu"      # 注意:这里没有为 Ubuntu 添加重启 chrony 服务的任务,因为 chrony 在 Ubuntu 上通常作为 chronyd 运行    # 如果需要重启 chrony,可以添加相应的 service 任务和 handler      handlers:      - name: 重启 chronyd 服务        service:          name: chronyd          state: restarted
整体脚本


---- 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


chrony_server(服务端)

服务端脚本主要操作了以一下内容


  1. 主机和任务定义:

  • 使用了Ansible的YAML格式来定义了一系列的主机(hosts)和任务(tasks)。

  • 主机可能是指向某个或多个服务器的标识符。

  • 包安装:

    • 对于CentOS或Rocky Linux系统,使用yum模块安装了chrony包。

    • 对于Ubuntu系统,使用apt模块来安装chrony包。

  • 文件编辑:

    • 使用lineinfile模块来编辑/etc/chrony.conf(CentOS/Rocky)或/etc/chrony/chrony.conf(Ubuntu)文件,添加、删除或替换特定的行。

    • 例如,删除以pool或server开头的行,添加时间服务器行(如server ntp.aliyun.com iburst),以及允许所有IP地址访问(allow 0.0.0.0/0)。

  • 文件替换:

    • 使用replace模块来替换/etc/chrony.conf文件中的特定字符串,例如将#(local).*替换为local stratum 10。

  • 删除文件:

    • 使用file模块来删除特定的锁文件,如/var/lib/dpkg/lock,/var/lib/apt/lists/lock等。

  • 更新软件包缓存:

    • 对于Ubuntu系统,使用apt模块的update_cache选项来更新软件包缓存。

  • 服务管理:

    • 使用systemd模块来确保chronyd服务正在运行并设置为开机启动。

    • 使用service模块的handlers部分来定义在特定条件满足时(如配置文件更改后)重启chronyd服务。

  • 条件判断:

    • 使用when子句来根据Ansible收集到的事实(facts)来应用特定的任务,例如判断当前的系统分布(ansible_distribution)。

  • 循环和列表:

    • 使用loop关键字来迭代一个列表,例如删除多个锁文件或向配置文件中添加多行。



    在CentOS或Rocky Linux上安装chrony


    # 定义目标主机,这里假设为名为chronyserver的主机  ---  hosts: chronyserver    tasks:    # 任务1:在CentOS或Rocky Linux上安装chrony  - name: install CentOS or Rocky chrony    yum:      name: chrony    when:      - (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")    # 任务2:如果CentOS或Rocky Linux的/etc/chrony.conf文件中包含'^pool.*'字符串的行,则删除该行  - 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    # 任务3:与任务2类似,但删除'^server.*'字符串的行  - 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    # 任务4:在CentOS或Rocky Linux的/etc/chrony.conf文件中添加指定的时间服务器  - name: add Time server for CentOS or Rocky /etc/chrony.conf file    lineinfile:      path: /etc/chrony.conf      insertafter: '^# Please consider .*'      line: "server ntp.aliyun.com iburst\nserver time1.cloud.tencent.com iburst\nserver ntp.tuna.tsinghua.edu.cn iburst"    when:      - (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")    notify:      - restart chronyd    # 任务5:在CentOS或Rocky Linux的/etc/chrony.conf文件中,将'^#(allow).*'字符串替换为'allow 0.0.0.0/0'  - name: Substitution '^#(allow).*' string for CentOS or Rocky /etc/chrony.conf file    replace:      path: /etc/chrony.conf      regexp: '^#(allow).*'      replace: '\1 0.0.0.0/0'    when:      - (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")    notify:      - restart chronyd    # 任务6:在CentOS或Rocky Linux的/etc/chrony.conf文件中,将'^#(local).*'字符串替换为'local stratum 10'  - name: Substitution '^#(local).*' string for CentOS or Rocky /etc/chrony.conf file    replace:      path: /etc/chrony.conf      regexp: '^#(local).*'      replace: '\1 stratum 10'    when:      - (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")    notify:      - restart chronyd
    如果系统为Ubuntu


    # 任务7:如果系统为Ubuntu,则删除指定的锁文件  - 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"  # 任务8:如果系统为Ubuntu,则更新apt包列表  - name: apt update  apt:    update_cache: yes    force: yes  when:    - ansible_distribution=="Ubuntu"  # 注意:任务列表在此处截断,假设"install Ubuntu"是后续在Ubuntu上安装某个软件或服务的占位符  # ...    # handlers部分(未在提供的文本中显示,但通常在playbook的末尾定义)  # handlers用于处理由tasks触发的操作,例如重启服务  handlers:  - name: restart chronyd    service:      name: chronyd      state: restarted


    整体代码


    ---- hosts: chronyserver  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 ntp.aliyun.com iburst\nserver time1.cloud.tencent.com iburst\nserver ntp.tuna.tsinghua.edu.cn iburst"      when:        - (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")      notify:        - restart chronyd    - name: Substitution '^#(allow).*' string for CentOS or Rocky /etc/chrony.conf file      replace:        path: /etc/chrony.conf        regexp: '^#(allow).*'        replace: '\1 0.0.0.0/0'      when:        - (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")      notify:        - restart chronyd    - name: Substitution '^#(local).*' string for CentOS or Rocky /etc/chrony.conf file      replace:        path: /etc/chrony.conf        regexp: '^#(local).*'        replace: '\1 stratum 10'      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 ntp.aliyun.com iburst\nserver time1.cloud.tencent.com iburst\nserver ntp.tuna.tsinghua.edu.cn iburst"      when:        - ansible_distribution=="Ubuntu"      notify:        - restart chronyd    - name: add 'allow 0.0.0.0/0' string and 'local stratum 10' string for Ubuntu /etc/chrony/chrony.conf file      lineinfile:        path: /etc/chrony/chrony.conf        line: "{{ item }}"      loop:        - "allow 0.0.0.0/0"        - "local stratum 10"      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
    vars.yml

     用于存储键值对形式的配置数据,包含两个键值对,其中 SERVER1 和 SERVER2 是键(keys),而 172.31.0.101 和 172.31.0.104 是对应的值(values)。

    vars.ymlSERVER1: 172.31.0.101SERVER2: 172.31.0.104
    总结


    通过Ansible自动化Chrony配置,我们可以确保企业环境中所有主机的时间都是精确且一致的,这对于维护系统的可靠性和性能至关重要。通过编写合适的Ansible Playbook和模板,我们可以简化时间同步的管理工作,提高效率,同时减少出错的机会。这种自动化的方法不仅适用于Chrony,还可以扩展到其他系统管理任务,帮助团队更好地利用Ansible的灵活性和可扩展性。



    END



    扫码关注我们



    敬请关注博主,我们将持续为您带来云计算行业及更多前沿技术的最新信息,助您把握技术动态,领先一步!”


    若您渴望深入了解云计算知识,或希望与云计算、运维、网络等领域的精英们共同交流学习,欢迎您私信博主,博主将为您提供宝贵的免费学习资料,并引荐您加入专业交流群。在这里,您可以与各路大咖切磋技艺,共享行业前沿资讯,一同成长进步。期待您的参与,让我们共同开启云计算领域的探索之旅!



    微信号 | a2459155652
    商务合作|学术交流|简历指导



    扫码关注
    云计算运维学习交流群



    独家粉丝福利大放送!


    🌟亲爱的朋友们,你是否对云计算充满好奇,想要深入了解这一前沿科技?那么,现在就有一个绝佳的机会摆在你面前!🌟

    💡**只要简单两步,你就能轻松获取珍贵的云计算资料一份!**💡

    1️⃣ 立即关注博主:不错过任何一次与前沿科技近距离接触的机会!

    2️⃣ 回复关键词“云计算资料”:打开通往云计算知识宝库的大门!

    🎁这份资料包含了云计算的精华内容,无论是初学者还是专业人士,都能从中获得宝贵的启示和收获。🎁

    点个「在看」再走吧!





    原文作者:网络豆云计算学堂;如有侵权,请联系管理员删除。

    请登录后发表评论 登录
    全部评论

    注册时间:2023-09-15

    • 博文量
      76
    • 访问量
      33957