在 CentOS 9 Stream 上安装 Ansible 的最简单方法是通过系统自带的 AppStream 仓库。此外,您还可以选择使用额外的 EPEL Next 仓库,该仓库由 Fedora 特殊兴趣小组维护,提供高质量的额外软件包,类似于针对 RHEL、CentOS 和 Oracle Linux 的 EPEL 仓库。

以下是详细的安装步骤和实际演示:

安装步骤

1. 更新系统包列表

sudo dnf update

2. 安装 Ansible

sudo dnf install ansible-core

3. 验证安装

安装完成后,可以通过以下命令验证 Ansible 是否正确安装:

ansible --version

输出示例:

ansible [core 2.12.0]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /bin/ansible
  python version = 3.9.8 (main, Nov  8 2021, 00:00:00) [GCC 11.2.1 20211019 (Red Hat 11.2.1-6)]
  jinja version = 2.11.3
  libyaml = True

使用 EPEL Next 仓库安装 Ansible(可选)

如果您需要额外的软件包,可以启用 EPEL Next 仓库。以下是步骤:

1. 安装 EPEL Next 仓库

sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-next-release-latest-9.noarch.rpm

2. 安装 Ansible

sudo dnf install ansible-core

3. 验证安装

ansible --version

实际演示

1. 登录到 CentOS 9 Stream 系统

ssh devops@centos-stream.example.com

2. 切换到 root 用户

sudo su

3. 检查系统版本

cat /etc/redhat-release

输出示例:

CentOS Stream release 9

4. 搜索 Ansible 包

dnf search ansible

5. 查看 Ansible 核心包信息

dnf info ansible-core

输出示例:

Available Packages
Name         : ansible-core
Version      : 2.12.0
Release      : 1.el9
Architecture : x86_64
Size         : 2.4 M
Source       : ansible-core-2.12.0-1.el9.src.rpm
Repository   : appstream
Summary      : SSH-based configuration management, deployment, and task execution system
URL          : http://ansible.com
License      : GPLv3+
Description  : Ansible is a radically simple model-driven configuration management, multi-node deployment, and remote task execution system. Ansible works over SSH and does not require any software or daemons to be installed on remote nodes. Extension modules can be written in any language and are transferred to managed machines automatically.

6. 安装 Ansible 核心包

dnf install ansible-core

7. 验证安装

ansible --version

输出示例:

ansible [core 2.12.0]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /bin/ansible
  python version = 3.9.8 (main, Nov  8 2021, 00:00:00) [GCC 11.2.1 20211019 (Red Hat 11.2.1-6)]
  jinja version = 2.11.3
  libyaml = True

总结

在 CentOS 9 Stream 上安装 Ansible 非常简单,您可以通过以下两种方式选择:

  1. 使用系统自带的 AppStream 仓库:这是最简单的方法,适用于大多数用户。
  2. 使用额外的 EPEL Next 仓库:如果您需要更多软件包,可以选择启用 EPEL Next 仓库。

通过以上步骤,您可以在 CentOS 9 Stream 上快速安装并开始使用 Ansible。