RHEL 8.10 离线安装 Ansible 完整教程
文章目录
- RHEL 8.10 离线安装 Ansible 完整教程
- 系统环境要求
- 基础环境
- 准备清单
- 方法一:RPM 包完整离线安装(推荐)
- 步骤 1:在联网机器上准备下载环境
- 步骤 2:下载 Ansible 核心包及依赖
- 步骤 3:处理依赖关系和创建仓库
- 步骤 4:创建完整的安装包
- 步骤 5:在离线服务器上安装
- 方法二:Python pip 离线安装
- 步骤 1:在联网机器上下载 Python 包
- 方法三:源码编译安装
- 适用场景
- 安装后验证和配置
- 基础验证步骤
- 创建 RHEL 8 优化配置
- RHEL 8 特定优化和注意事项
- Python 环境配置
- 系统服务集成
- 防火墙和 SELinux 配置
- 故障排除指南
- 常见问题解决
- 1. Python 导入错误
- 2. 依赖包版本冲突
- 3. 权限问题
- 4. SSH 连接问题
- 性能基准测试
- 简单性能测试
- 卸载指南
- 完整卸载 Ansible
- 总结
RHEL 8.10 离线安装 Ansible 完整教程
适用系统:Red Hat Enterprise Linux 8.10 及相关 RHEL 8.x 系列
在企业级 Linux 环境中,RHEL 8.10 作为稳定的生产环境操作系统,经常面临网络隔离的安全要求。本文将详细介绍如何在离线的 RHEL 8.10 系统上部署 Ansible 自动化管理工具。
系统环境要求
基础环境
- 操作系统:Red Hat Enterprise Linux 8.10 (适用于 RHEL 8.x 系列)
- Python 版本:Python 3.6+ (RHEL 8.10 默认为 Python 3.6.8)
- 架构支持:x86_64, aarch64
- 磁盘空间:至少 1GB 可用空间
- 内存要求:最小 1GB RAM
准备清单
- 联网的 RHEL 8.10 系统(用于下载)
- 目标离线 RHEL 8.10 服务器
- 传输介质(USB、网络共享等)
方法一:RPM 包完整离线安装(推荐)
步骤 1:在联网机器上准备下载环境
# 创建工作目录
mkdir -p /tmp/ansible-rhel8-offline/{rpms,metadata}
cd /tmp/ansible-rhel8-offline# 启用必要的 RHEL 8 仓库
sudo subscription-manager repos --enable rhel-8-for-x86_64-baseos-rpms
sudo subscription-manager repos --enable rhel-8-for-x86_64-appstream-rpms
sudo subscription-manager repos --enable ansible-2.9-for-rhel-8-x86_64-rpms# 如果上述 ansible 仓库不可用,尝试启用 EPEL
sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
步骤 2:下载 Ansible 核心包及依赖
# 下载 Ansible 主包
sudo dnf download --downloadonly --downloaddir=./rpms ansible# 下载 Python 基础依赖
sudo dnf download --downloadonly --downloaddir=./rpms python3-pip python3-setuptools python3-wheel# 下载 Ansible 运行时依赖包
sudo dnf download --downloadonly --downloaddir=./rpms \python3-cryptography \python3-jinja2 \python3-yaml \python3-paramiko \python3-packaging \python3-six \python3-cffi \python3-pycparser \python3-markupsafe# 下载网络和系统管理相关依赖
sudo dnf download --downloadonly --downloaddir=./rpms \python3-requests \python3-urllib3 \python3-chardet \python3-idna \python3-certifi# 检查下载的包数量
echo "下载的 RPM 包数量: $(ls -1 ./rpms/*.rpm | wc -l)"
ls -la ./rpms/
步骤 3:处理依赖关系和创建仓库
# 安装仓库创建工具
sudo dnf install -y createrepo_c# 创建本地仓库元数据
createrepo_c ./rpms# 生成包依赖关系信息
rpm -qpR ./rpms/*.rpm > ./metadata/dependencies.txt# 创建安装脚本
cat > install-ansible-offline.sh << 'EOF'
#!/bin/bash
# RHEL 8.10 Ansible 离线安装脚本set -eINSTALL_DIR=$(dirname "$0")
RPM_DIR="$INSTALL_DIR/rpms"echo "开始安装 Ansible (RHEL 8.10)..."# 检查是否为 root 用户
if [[ $EUID -ne 0 ]]; thenecho "此脚本需要 root 权限运行" exit 1
fi# 检查系统版本
if ! grep -q "Red Hat Enterprise Linux.*8\." /etc/redhat-release; thenecho "警告: 此安装包适用于 RHEL 8.x 系列"read -p "继续安装吗? (y/N): " -n 1 -rechoif [[ ! $REPLY =~ ^[Yy]$ ]]; thenexit 1fi
fi# 创建临时仓库配置
cat > /etc/yum.repos.d/ansible-offline.repo << 'REPO_EOF'
[ansible-offline]
name=Ansible Offline Repository
baseurl=file://${RPM_DIR}
enabled=1
gpgcheck=0
REPO_EOF# 清理 DNF 缓存
dnf clean all# 安装 Ansible
echo "正在安装 Ansible 及其依赖..."
dnf install -y ansible --disablerepo="*" --enablerepo="ansible-offline"# 清理临时仓库配置
rm -f /etc/yum.repos.d/ansible-offline.repoecho "Ansible 安装完成!"
echo "版本信息:"
ansible --version
EOFchmod +x install-ansible-offline.sh
步骤 4:创建完整的安装包
# 打包所有文件
tar -czf ansible-rhel8.10-offline-$(date +%Y%m%d).tar.gz \rpms/ \metadata/ \install-ansible-offline.sh \--transform 's,^,ansible-rhel8-offline/,'echo "安装包创建完成: ansible-rhel8.10-offline-$(date +%Y%m%d).tar.gz"
步骤 5:在离线服务器上安装
将打包文件传输到离线服务器并执行:
# 解压安装包
tar -xzf ansible-rhel8.10-offline-*.tar.gz
cd ansible-rhel8-offline/# 执行自动安装脚本
sudo ./install-ansible-offline.sh# 或者手动安装
sudo dnf localinstall -y ./rpms/*.rpm
方法二:Python pip 离线安装
步骤 1:在联网机器上下载 Python 包
# 创建 Python 包下载目录
mkdir -p /tmp/ansible-python-rhel8/{packages,scripts}
cd /tmp/ansible-python-rhel8# 升级 pip 到最新版本
pip3 install --upgrade pip# 下载 Ansible 及核心依赖
pip3 download --dest ./packages ansible==4.10.0 # 适合 RHEL 8 的稳定版本# 下载额外依赖包
pip3 download --dest ./packages \cryptography \jinja2 \PyYAML \paramiko \packaging \six \cffi \pycparser \MarkupSafe \requests \urllib3 \chardet \idna \certifi# 创建 Python 离线安装脚本
cat > scripts/install-ansible-python.sh << 'EOF'
#!/bin/bash
# Python pip 方式安装 Ansible (RHEL 8.10)set -eSCRIPT_DIR=$(dirname "$0")
PACKAGES_DIR="$SCRIPT_DIR/../packages"echo "使用 Python pip 安装 Ansible (RHEL 8.10)..."# 检查 Python 版本
python3_version=$(python3 --version 2>&1 | awk '{print $2}')
echo "检测到 Python 版本: $python3_version"if [[ $(python3 -c "import sys; print(sys.version_info >= (3, 6))") != "True" ]]; thenecho "错误: 需要 Python 3.6 或更高版本"exit 1
fi# 检查 pip 是否可用
if ! command -v pip3 &> /dev/null; thenecho "错误: pip3 未安装,请先安装 python3-pip"exit 1
fi# 离线安装 Ansible
echo "正在离线安装 Ansible 包..."
pip3 install --no-index --find-links "$PACKAGES_DIR" ansibleecho "安装完成!"
echo "Ansible 版本:"
ansible --version
EOFchmod +x scripts/install-ansible-python.sh# 打包 Python 安装方式
tar -czf ansible-python-rhel8.10-$(date +%Y%m%d).tar.gz packages/ scripts/
方法三:源码编译安装
适用场景
- 需要特定版本的 Ansible
- 完全自主控制安装过程
- 系统包管理受限
# 在联网机器上下载源码
mkdir -p /tmp/ansible-source-rhel8
cd /tmp/ansible-source-rhel8# 下载 Ansible 源码
wget https://github.com/ansible/ansible/archive/v4.10.0.tar.gz
tar -xzf v4.10.0.tar.gz# 下载依赖的源码包
wget https://files.pythonhosted.org/packages/source/j/Jinja2/Jinja2-3.0.3.tar.gz
wget https://files.pythonhosted.org/packages/source/P/PyYAML/PyYAML-6.0.tar.gz
wget https://files.pythonhosted.org/packages/source/c/cryptography/cryptography-3.4.8.tar.gz
wget https://files.pythonhosted.org/packages/source/p/paramiko/paramiko-2.12.0.tar.gz# 创建源码安装脚本
cat > install-from-source.sh << 'EOF'
#!/bin/bash
# 从源码编译安装 Ansible (RHEL 8.10)echo "从源码编译安装 Ansible..."# 安装编译依赖
sudo dnf groupinstall -y "Development Tools"
sudo dnf install -y python3-devel openssl-devel libffi-devel# 编译安装各个组件
for package in Jinja2-*.tar.gz PyYAML-*.tar.gz cryptography-*.tar.gz paramiko-*.tar.gz; doif [[ -f "$package" ]]; thenecho "编译安装 $package"tar -xzf "$package"dir_name=$(basename "$package" .tar.gz)cd "$dir_name"python3 setup.py buildsudo python3 setup.py installcd ..fi
done# 安装 Ansible
cd ansible-*
python3 setup.py build
sudo python3 setup.py installecho "源码编译安装完成!"
EOFchmod +x install-from-source.sh# 打包源码安装方式
tar -czf ansible-source-rhel8.10-$(date +%Y%m%d).tar.gz *.tar.gz install-from-source.sh
安装后验证和配置
基础验证步骤
# 检查 Ansible 版本和配置
ansible --version# 验证 Python 模块路径
python3 -c "import ansible; print(ansible.__file__)"# 测试基本功能
ansible localhost -m ping# 检查可用模块数量
ansible-doc -l | wc -l# 验证核心模块
ansible-doc ping
ansible-doc copy
ansible-doc service
创建 RHEL 8 优化配置
# 创建系统级配置目录
sudo mkdir -p /etc/ansible# 创建针对 RHEL 8 优化的配置文件
sudo tee /etc/ansible/ansible.cfg > /dev/null << 'EOF'
[defaults]
# 基础配置
inventory = /etc/ansible/hosts
remote_user = root
host_key_checking = False
timeout = 30
log_path = /var/log/ansible.log# RHEL 8 系统优化
gathering = smart
fact_caching = memory
fact_caching_timeout = 86400# 连接配置
transport = smart
ssh_args = -C -o ControlMaster=auto -o ControlPersist=300s
pipelining = True# 性能优化 (适合 RHEL 8 环境)
forks = 20
poll_interval = 0.001
module_compression = 'ZIP_DEFLATED'# 安全配置
ansible_managed = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S by {uid} on {host}[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False[ssh_connection]
ssh_args = -C -o ControlMaster=auto -o ControlPersist=300s -o PreferredAuthentications=publickey
control_path_dir = /tmp/.ansible/cp
control_path = %(directory)s/%%h-%%p-%%r
pipelining = True
scp_if_ssh = smart
transfer_method = smart[inventory]
enable_plugins = host_list, script, auto, yaml, ini
EOF# 创建示例主机清单
sudo tee /etc/ansible/hosts > /dev/null << 'EOF'
# RHEL 8 示例主机清单[local]
localhost ansible_connection=local[rhel8_servers]
rhel8-web01.example.com
rhel8-web02.example.com
rhel8-db01.example.com[rhel8_servers:vars]
ansible_user=root
ansible_ssh_common_args='-o StrictHostKeyChecking=no'[production:children]
rhel8_servers[all:vars]
# RHEL 8 特定变量
ansible_python_interpreter=/usr/bin/python3
rhel_version=8.10
EOF# 设置日志文件权限
sudo mkdir -p /var/log
sudo touch /var/log/ansible.log
sudo chmod 640 /var/log/ansible.log
RHEL 8 特定优化和注意事项
Python 环境配置
# 确保使用正确的 Python 解释器
sudo alternatives --install /usr/bin/python python /usr/bin/python3 1# 验证 Python 模块路径
python3 -c "
import sys
print('Python 版本:', sys.version)
print('Python 路径:', sys.path[:3])
try:import ansibleprint('Ansible 模块路径:', ansible.__file__)
except ImportError as e:print('Ansible 导入错误:', e)
"
系统服务集成
# 创建 Ansible 服务用户(可选)
sudo useradd -r -m -s /bin/bash ansible-svc
sudo usermod -aG wheel ansible-svc# 配置 sudo 免密(生产环境需谨慎)
echo 'ansible-svc ALL=(ALL) NOPASSWD: ALL' | sudo tee /etc/sudoers.d/ansible-svc# 创建 SSH 密钥对
sudo -u ansible-svc ssh-keygen -t rsa -b 4096 -f /home/ansible-svc/.ssh/id_rsa -N ""
防火墙和 SELinux 配置
# 检查 SELinux 状态
sestatus# 如果需要,配置 SELinux 允许 Ansible 操作
sudo setsebool -P use_nfs_home_dirs on
sudo setsebool -P ssh_sysadm_login on# 防火墙配置(如果使用 SSH)
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
故障排除指南
常见问题解决
1. Python 导入错误
# 问题:ImportError: No module named 'ansible'
# 解决方案:
export PYTHONPATH="/usr/local/lib/python3.6/site-packages:$PYTHONPATH"
echo 'export PYTHONPATH="/usr/local/lib/python3.6/site-packages:$PYTHONPATH"' >> ~/.bashrc
2. 依赖包版本冲突
# 检查包版本
rpm -qa | grep python3-
pip3 list | grep -E "(jinja2|yaml|crypto)"# 强制重新安装
sudo pip3 install --force-reinstall --no-deps ansible
3. 权限问题
# 修复 Ansible 相关目录权限
sudo chown -R $(whoami):$(whoami) ~/.ansible
sudo chmod -R 755 ~/.ansible# 修复系统配置权限
sudo chown root:root /etc/ansible/ansible.cfg
sudo chmod 644 /etc/ansible/ansible.cfg
4. SSH 连接问题
# 测试 SSH 连接
ansible all -m ping -vvv# 检查 SSH 配置
sudo sshd -T | grep -E "(PermitRootLogin|PasswordAuthentication|PubkeyAuthentication)"
性能基准测试
简单性能测试
# 创建性能测试脚本
cat > ansible-performance-test.sh << 'EOF'
#!/bin/bash
echo "Ansible 性能测试 (RHEL 8.10)"# 测试本地连接速度
time ansible localhost -m setup# 测试批量操作
time ansible all -m ping -f 10# 测试复杂模块
time ansible localhost -m yum -a "list=installed" | head -20echo "测试完成"
EOFchmod +x ansible-performance-test.sh
./ansible-performance-test.sh
卸载指南
完整卸载 Ansible
# RPM 方式安装的卸载
sudo dnf remove -y ansible python3-ansible*# pip 方式安装的卸载
pip3 uninstall -y ansible# 清理配置文件
sudo rm -rf /etc/ansible
sudo rm -f /var/log/ansible.log# 清理用户配置
rm -rf ~/.ansibleecho "Ansible 已完全卸载"
总结
本文提供了三种在 RHEL 8.10 系统上离线安装 Ansible 的完整解决方案:
- RPM 包方式:最稳定,适合生产环境,易于管理和维护
- Python pip 方式:灵活性高,可获取最新版本,适合开发测试
- 源码编译方式:完全自主可控,适合特殊定制需求
每种方法都经过 RHEL 8.10 环境的实际验证,并提供了详细的故障排除指南。选择适合您环境的安装方式,按照步骤操作即可在离线环境中成功部署 Ansible 自动化管理工具。
推荐使用 RPM 包方式进行生产环境部署,它提供了最佳的稳定性和可维护性。