Ansible 配置 YUM 仓库示例

任务描述

  1. 使用 FTP 服务器搭建一个 RHEL 8 的仓库,包含 basestream 库,地址分别为:

    • http://192.168.50.100/pub/rhel8/BaseOS
    • http://192.168.50.100/pub/rhel8/AppStream
  2. 编写 Ansible Playbook,通过 yum_repository 模块在受管主机上完成 YUM 仓库的设置,仓库设置要求如下:

    • 仓库 1:

      • 名称: base
      • 描述: rhel8 base software
      • 状态: 启用
    • 仓库 2:

      • 名称: stream
      • 描述: rhel8 stream software
      • 状态: 启用
  3. 部署完成后进行验证。

部署步骤与解释

第一步:设置 FTP 服务器上的 YUM 仓库

假设我们已经在 192.168.50.100 服务器上配置好了 FTP 服务器,并将 RHEL 8 的 BaseOSAppStream 仓库放置在以下路径:

  • http://192.168.50.100/pub/rhel8/BaseOS
  • http://192.168.50.100/pub/rhel8/AppStream
第二步:编写 Ansible Playbook

创建一个新的 Ansible Playbook 文件 setup_repos.yml,内容如下:

---
- name: Configure YUM repositories
  hosts: all
  become: yes
  tasks:
    - name: Configure base repository
      yum_repository:
        name: base
        description: "rhel8 base software"
        baseurl: http://192.168.50.100/pub/rhel8/BaseOS
        enabled: yes
        gpgcheck: no

    - name: Configure stream repository
      yum_repository:
        name: stream
        description: "rhel8 stream software"
        baseurl: http://192.168.50.100/pub/rhel8/AppStream
        enabled: yes
        gpgcheck: no

解释:

  • hosts: all:表示该 Playbook 将应用于所有受管主机。
  • become: yes:表示使用 sudo 权限执行任务。
  • yum_repository 模块用于配置 YUM 仓库。

    • name:仓库的名称。
    • description:仓库的描述。
    • baseurl:仓库的 URL。
    • enabled:启用仓库。
    • gpgcheck:关闭 GPG 签名检查。
第三步:运行 Playbook

在控制节点上运行以下命令来执行 Playbook:

ansible-playbook -i inventory setup_repos.yml

inventory 文件应包含受管主机的列表,例如:

[all]
192.168.50.101
192.168.50.102
第四步:验证部署

在受管主机上运行以下命令来验证仓库配置:

yum repolist

输出应该包含配置的 basestream 仓库。

完整示例

Playbook 文件 setup_repos.yml

---
- name: Configure YUM repositories
  hosts: all
  become: yes
  tasks:
    - name: Configure base repository
      yum_repository:
        name: base
        description: "rhel8 base software"
        baseurl: http://192.168.50.100/pub/rhel8/BaseOS
        enabled: yes
        gpgcheck: no

    - name: Configure stream repository
      yum_repository:
        name: stream
        description: "rhel8 stream software"
        baseurl: http://192.168.50.100/pub/rhel8/AppStream
        enabled: yes
        gpgcheck: no

inventory 文件

[all]
192.168.50.101
192.168.50.102

运行 Playbook

在控制节点上运行以下命令:

ansible-playbook -i inventory setup_repos.yml

验证命令

在受管主机上运行以下命令进行验证:

yum repolist

本文由mdnice多平台发布


逼格高的汤圆
10 声望2 粉丝