介绍
在这个实验(Lab)中,你将学习如何在 Red Hat Enterprise Linux (RHEL) 系统上安装 Ansible Core。Ansible 是一个强大的自动化工具,它允许你管理和配置系统、部署应用程序以及编排复杂的 IT 工作流程。
你将使用 dnf 包管理器,并使用 sudo 权限来安装 ansible-core 包,该包提供了核心的 Ansible 引擎和命令行工具。安装完成后,你将通过检查其版本并运行基本命令来验证 Ansible 是否正常工作。
对于使用 Red Hat Enterprise Linux 系统的系统管理员和 DevOps 工程师来说,这是一项基本技能。
使用 dnf 安装 Ansible Core
在这一步中,你将使用 dnf 包管理器安装 ansible-core 包。Ansible Core 提供了基本的 Ansible 引擎,包括 ansible、ansible-playbook 和其他用于自动化任务的核心命令行工具。
dnf (Dandified YUM) 包管理器是 Red Hat Enterprise Linux 上用于管理软件包的标准工具。由于安装软件需要管理权限,因此你必须使用 sudo 命令。
运行以下命令,使用自动确认来安装 Ansible Core:
sudo dnf install ansible-core -y
-y 标志会自动回答所有提示“是”,使安装过程非交互式。系统将下载并安装 ansible-core 及其 Python 依赖项,包括用于模板的 Jinja2 和用于 YAML 处理的 PyYAML。
你应该看到类似于以下的输出,显示包的解析和安装进度:
Updating Subscription Management repositories.
Last metadata expiration check: ...
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
ansible-core noarch 2.16.x-x.el9 rhel-9-appstream xx M
Installing dependencies:
python3-jinja2 noarch x.x.x-x.el9 rhel-9-appstream xxx k
python3-yaml x86_64 x.x.x-x.el9 rhel-9-appstream xxx k
...
Transaction Summary
================================================================================
Install XX Packages
Complete!
验证 Ansible 安装
现在你已经安装了 Ansible Core,让我们通过检查版本并确认基本的命令行工具是否可用,来验证安装是否成功。
首先,通过运行以下命令来检查 Ansible 的版本:
ansible --version
此命令显示有关你的 Ansible 安装的详细信息,包括核心版本、Python 版本以及各种组件的位置。你应该看到类似这样的输出:
ansible [core 2.14.18]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/labex/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.9/site-packages/ansible
ansible collection location = /home/labex/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.9.21 (main, Feb 10 2025, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-5)] (/usr/bin/python3)
jinja version = 3.1.2
libyaml = True
让我们了解每一行的含义:
- **ansible [core 2.14.18]**:显示已安装的 Ansible Core 版本
- config file:指向包含默认设置的主 Ansible 配置文件
- configured module search path:Ansible 查找自定义模块的目录
- ansible python module location:安装核心 Ansible Python 代码的位置
- ansible collection location:存储 Ansible 集合(打包的模块和插件)的目录
- executable location:ansible 命令二进制文件的实际位置
- python version:Ansible 使用的 Python 解释器版本
- jinja version:Ansible 用于动态内容的模板引擎版本
- libyaml = True:确认快速 YAML 解析器可用于提高性能
这确认了 Ansible 已正确安装并可以使用。接下来,我们还要检查 ansible-playbook 命令是否可用:
ansible-playbook --version
你应该看到类似 ansible-playbook 工具的版本信息,该工具对于运行 Ansible playbook 至关重要。
使用简单命令测试 Ansible
在这一步中,你将通过对本地系统运行一个简单的命令来测试你的 Ansible 安装。切换到项目目录,并使用预先配置的 inventory 文件来运行一个 Ansible ad-hoc 命令。
导航到项目目录并测试基本的 ping 功能:
cd /home/labex/project
ansible localhost -m ping
ping 模块实际上并没有发送 ICMP 数据包;相反,它验证 Ansible 是否可以连接到目标并执行 Python 代码。成功的响应将如下所示:
localhost | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
让我们分解一下这个输出:
- localhost | SUCCESS:显示命令在 localhost 目标上成功执行
- ansible_facts:包含在执行期间发现的系统信息
- discovered_interpreter_python:Ansible 找到并将使用的 Python 解释器路径
- changed: false:表示系统未发生任何更改(ping 是只读的)
- **ping: "pong"**:经典的响应,确认 Ansible 的连接性
“pong”响应确认 Ansible 正在正常工作,并且可以与目标系统通信。
我们还可以使用 setup 模块测试收集系统信息:
ansible localhost -m setup -a "filter=ansible_distribution*"
此命令使用 setup 模块收集系统事实,特别是过滤发行版信息。你应该看到包含有关你的 Red Hat Enterprise Linux 系统的详细信息的输出:
localhost | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "RedHat",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_search_string": "Red Hat",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "9",
"ansible_distribution_release": "Plow",
"ansible_distribution_version": "9.6",
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false
}
了解系统事实输出:
- ansible_distribution:Linux 发行版名称 (RedHat)
- ansible_distribution_file_parsed:Ansible 是否成功读取发行版文件
- ansible_distribution_file_path:包含发行版信息的文件
- ansible_distribution_file_search_string:用于标识发行版的文本模式
- ansible_distribution_file_variety:发行版系列 (RedHat 系列)
- ansible_distribution_major_version:主版本号 (9)
- ansible_distribution_release:发布代号 (Plow)
- ansible_distribution_version:完整的版本号 (9.6)
- discovered_interpreter_python:Ansible 发现的 Python 解释器
这确认了 Ansible 可以成功地从目标主机收集系统信息,这对于基于系统特征创建条件自动化至关重要。
探索可用的 Ansible 模块
Ansible 附带数百个内置模块,用于各种自动化任务。让我们探索一些可用的模块,以了解安装后立即可用的功能。
要查看可用模块的列表,请运行:
ansible-doc -l | head -20
ansible-doc -l 命令列出所有可用的模块,使用 head -20 显示前 20 个模块。这让你了解 Ansible 提供的广泛的自动化功能。你将看到类似这样的输出:
ansible.builtin.add_host Add a host (and alternatively a grou...
ansible.builtin.apt Manages apt-packages
ansible.builtin.apt_key Add or remove an apt key
ansible.builtin.apt_repository Add and remove APT repositories
ansible.builtin.assemble Assemble configuration files from fr...
ansible.builtin.assert Asserts given expressions are true
ansible.builtin.async_status Obtain status of asynchronous task
ansible.builtin.blockinfile Insert/update/remove a text block su...
ansible.builtin.command Execute commands on targets
ansible.builtin.copy Copy files to remote locations
ansible.builtin.cron Manage cron.d and crontab entries
ansible.builtin.debconf Configure a .deb package
ansible.builtin.debug Print statements during execution
ansible.builtin.dnf Manages packages with the `dnf' pack...
ansible.builtin.dpkg_selections Dpkg package selection selections
ansible.builtin.expect Executes a command and responds to p...
ansible.builtin.fail Fail with custom message
ansible.builtin.fetch Fetch files from remote nodes
ansible.builtin.file Manage files and file properties
ansible.builtin.find Return a list of files based on spec...
了解模块列表格式:
- **ansible.builtin.**:表示这些是 Ansible Core 附带的内置模块
- Module name:在 playbook 或 ad-hoc 命令中调用模块时使用的名称
- Description:对模块功能的简要说明
你通常会用到的一些重要模块:
- command:在目标系统上执行 shell 命令
- copy:将文件从你的控制机复制到远程主机
- dnf:在 Red Hat 系统上安装、更新或删除软件包
- file:创建目录、设置权限或管理文件属性
- debug:在 playbook 执行期间打印消息以进行故障排除
要获取特定模块的详细文档,你可以将 ansible-doc 命令与模块名称一起使用。例如,要了解 copy 模块:
ansible-doc copy
这将显示 copy 模块的全面文档,包括示例和参数说明。 ansible-doc 命令为任何 Ansible 模块提供详细的文档,让你轻松学习如何使用不同的自动化功能。阅读完毕后,按 q 退出文档查看器。
总结
在这个实验中,你成功地学习了如何在 Red Hat Enterprise Linux 系统上安装和验证 Ansible Core。这是你完成的任务:
安装 Ansible Core:你使用
sudo dnf install ansible-core -y命令从官方 Red Hat 存储库安装了核心 Ansible 包及其依赖项。验证安装:你通过检查
ansible和ansible-playbook命令的版本来确认 Ansible 已正确安装。测试基本功能:你运行了简单的 Ansible 命令来验证安装是否正常工作,包括:
- 使用
ping模块测试连接性 - 使用
setup模块收集系统事实
- 使用
探索可用模块:你学习了如何使用
ansible-doc发现和阅读 Ansible 模块的广泛库的文档。
你现在已经在 RHEL 上拥有一个功能齐全的 Ansible 安装,并且了解了开始自动化你的基础设施所需的基本命令。这个基础为你准备了更高级的 Ansible 主题,例如编写 playbook、管理 inventory 和实施复杂的自动化工作流程。



