行业资讯

时间:2025-08-13 浏览量:(10)

Ansible 自动化运维工具的安装、配置与验证

Ansible 是一款新出现的自动化运维工具,基于 Python 开发,集合了众多运维工具(如 puppet、chef、func、fabric)的优点,可实现批量系统配置、批量程序部署、批量运行命令等功能。

一、Ansible 的安装

1. yum 源安装(以 CentOS 为例)

默认 CentOS 的源中没有 Ansible,但 Fedora EPEL 源中有。配置好 EPEL 源后,可直接通过 yum 安装。以 CentOS 6.8 为例:
# yum install http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm# yum install ansible

2. apt-get 安装(适用于 Ubuntu 及其衍生版)

通过增加 ppa 源进行安装,具体步骤如下:
$ sudo apt-get install software-properties-common$ sudo apt-add-repository ppa:ansible/ansible$ sudo apt-get update$ sudo apt-get install ansible

3. 源码安装

源码安装需要 Python 2.6 及以上版本,且依赖 paramiko、PyYAML、Jinja2、httplib2、simplejson、pycrypto 等模块。这些模块可通过 pip 或 easy_install 安装;若无法连接外网,可从 pypi 站点搜索相关包,下载后通过python setup.py install安装。
最后,从 github 或 pypi 上下载 Ansible 源码包,通过python setup.py install完成安装。以下主要介绍安装后可能遇到的问题及解决方法:

a. 安装 PyYAML 时报错

# python setup.py installlibyaml is not found or a compiler error: forcing --without-libyaml(if libyaml is installed correctly, you may need tospecify the option --include-dirs or uncomment andmodify the parameter include_dirs in setup.cfg)running install_librunning install_egg_infoRemoving /usr/lib64/python2.6/site-packages/PyYAML-3.11-py2.6.egg-infoWriting /usr/lib64/python2.6/site-packages/PyYAML-3.11-py2.6.egg-info
在 CentOS 6.8 系统中,可通过yum -y install libyaml安装相关包解决,或从 ISO 文件中获取该包,通过rpm -ivh安装。

b. 安装完 Ansible 后报错

[root@361way.com ansible-1.9.1]# ansible -hTraceback (most recent call last):File "/usr/local/src/ansible-devel/bin/ansible", line 36, infrom ansible.runner import RunnerFile "/usr/local/src/ansible-devel/lib/ansible/runner/__init__.py", line 62, infrom Crypto.Random import atforkFile "/usr/lib64/python2.6/site-packages/Crypto/Random/__init__.py", line 29, infrom Crypto.Random import _UserFriendlyRNGFile "/usr/lib64/python2.6/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 38, infrom Crypto.Random.Fortuna import FortunaAccumulatorFile "/usr/lib64/python2.6/site-packages/Crypto/Random/Fortuna/FortunaAccumulator.py", line 39, inimport FortunaGeneratorFile "/usr/lib64/python2.6/site-packages/Crypto/Random/Fortuna/FortunaGenerator.py", line 34, infrom Crypto.Util.number import ceil_shift, exact_log2, exact_divFile "/usr/lib64/python2.6/site-packages/Crypto/Util/number.py", line 56, inif _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:AttributeError: 'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC'
导入 paramiko 包时也可能出现类似错误,经排查,这是 pycrypto 包安装时依赖的 GMP 版本不对导致的。可通过以下步骤验证:
[root@361way.com pycrypto-2.6.1]# python setup.py buildrunning buildrunning build_pyrunning build_extrunning build_configurewarning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.
解决方法:
打开/usr/lib64/python2.6/site-packages/Crypto/Util/number.py文件,第 56 行的注释说明要求 libgmp 为 v5 以上版本。若系统现有版本为 4.1.4,可暂时注释以下两行,使 Ansible 正常执行:
if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:_warn("Not using mpz_powm_sec. You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.", PowmInsecureWarning)
不过,更优的方式是将 libgmp 升级到符合要求的版本。

c. 执行时出现如下报错

[root@361way.com src]# ansible test -m raw -a 'uptime'10.212.52.14 | FAILED => to use the 'ssh' connection type with passwords, you must install the sshpass program10.212.52.16 | FAILED => to use the 'ssh' connection type with passwords, you must install the sshpass program
此时需安装 sshpass 程序,默认源中无此程序,可从 sohu 源下载安装。

二、Ansible 的配置与验证

从 pypi 上下载的源码包中包含 examples 目录,可将该目录下的示例文件作为默认配置,具体步骤如下:
[root@361way.com ansible-1.9.1]# mkdir -p /etc/ansible[root@361way.com ansible-1.9.1]# cp -rp examples/* /etc/ansible/[root@361way.com ansible-1.9.1]# cd /etc/ansible/
使用默认示例配置文件后,编辑/etc/ansible/hosts文件,通过以下方式验证 Ansible 是否可用:
[root@361way.com ~]# cat /etc/ansible/hosts[test]10.212.52.252 ansible_ssh_user=root ansible_ssh_pass=361way.com10.212.52.14 ansible_ssh_user=root ansible_ssh_pass=abc12310.212.52.16 ansible_ssh_user=root ansible_ssh_pass=91it.org
上述配置中,创建了一个 test 组,包含三台主机,均使用 root 用户验证,密码分别为361way.com、abc123、91it.org
注:用户和密码项非必须,配置 key 认证后可不使用密码直接操作;未使用 key 的情况下,可在 ansible 中通过-k参数在操作前手动输入密码。
执行以下指令,若有结果输出,则证明安装成功:
[root@361way.com ~]# ansible test -a 'uptime'10.212.52.252 | success | rc=0 >>18:01pm up 21 days 3:24, 3 users, load average: 0.39, 0.38, 0.3510.212.52.16 | success | rc=0 >>18:09pm up 329 days 1:01, 2 users, load average: 0.08, 0.03, 0.0510.212.52.14 | success | rc=0 >>18:08pm up 329 days 0:23, 2 users, load average: 0.06, 0.06, 0.05

Search Bar

最新资讯

2025-08-05

混合云:企业 IT 解决方案的...

2025-08-04

SoC 启动流程深度解析:从芯...

2025-07-25

租用高性价比香港云服务器的实用...

2025-07-25

如何选购新加坡服务器?关键因素...

2025-08-21

Linux 镜像存放方案指南:...