Ansible playbook剧本实战(9)

14.使用Collection来管理Ansible资源

# Collection 是 Ansible 内容的一个打包单元。Collections允许将相关的Ansible内容,包括角色(Roles)、模块(Modules)、插件(Plugins)、Playbook 和文档等打包成一个独立的单元,并发布到Ansible Galaxy或其他存储库上,以便其他用户轻松发现和安装。

# 基于本地文件,安装当前目录下的collection

ansible-galaxy collection install ./gls-utils-0.0.1.tar.gz -p collections


# 列举出安装的collection

ansible-galaxy collection list


# 通过安装的collection完成对应的操作

vim bck.yml

---
- name: Backup the system configuration
  hosts: servera.lab.example.com
  become: true
  gather_facts: false

  tasks:
    - name: Ensure the machine is up
      gls.utils.newping:
        data: pong

    - name: Ensure configuration files are saved
      include_role:
        name: gls.utils.backup
      vars:
        backup_id: backup_etc
        backup_files:
          - /etc/sysconfig
          - /etc/yum.repos.d


# 运行剧本

ansible-navigator run -m stdout bck.yml

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

15.使用system Role配置和管理特定的系统功能。

# 安装system role

ansible-galaxy collection install redhat-rhel_system_roles-1.19.3.tar.gz -p collections


# 查看安装的system-role

ansible-galaxy collection list


# 设置时区同步服务器

vim group_vars/all/timesync.yml

---
timesync_ntp_provider: chrony
timesync_ntp_servers:
  - hostname: classroom.example.com
    iburst: yes



# 在对应目录下设置时区的变量

vim group_vars/na_datacenter/timezone.yml

host_timezone: America/Chicago


vim group_vars/europe_datacenter/timezone.yml

host_timezone: Europe/Helsinki


# 编写剧本文件,如果主机清单下的主机时区与时区同步服务器的时间不一致就设置成对应的时区然后重启,整个剧本就是实现时间同步的

vim configure_time.yml

---
- name: Time Synchronization
  hosts: database_servers
  roles:
    - redhat.rhel_system_roles.timesync

  post_tasks:
    - name: Get time zone
      ansible.builtin.command: timedatectl show
      register: current_timezone
      changed_when: false

    - name: Set time zone
      ansible.builtin.command: "timedatectl set-timezone {{ host_timezone }}"
      when: host_timezone not in current_timezone.stdout
      notify: reboot host

  handlers:
    - name: reboot host
      ansible.builtin.reboot:


# 运行剧本,此时ping两个服务器地址可以看到都重启了所以都ping不通,最后查看两个主机的时区信息确实发生了更改

ansible-navigator run -m stdout configure_time.yml

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kisy夏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值