Linux中部署自动化运维工具ansible

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

Ansible的安装

1、yum源安装

以centos为例,默认在源里没有ansible,不过在fedora epel源里有ansible,配置完epel 源后,可以直接通过yum 进行安装。这里以centos6.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源进行apt-get安装,具体如下:

$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible

3、源码安装 源码安装需要python2.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 install
libyaml is not found or a compiler error: forcing --without-libyaml
(if libyaml is installed correctly, you may need to
specify the option --include-dirs or uncomment and
modify the parameter include_dirs in setup.cfg)
running install_lib
running install_egg_info
Removing /usr/lib64/python2.6/site-packages/PyYAML-3.11-py2.6.egg-info
Writing /usr/lib64/python2.6/site-packages/PyYAML-3.11-py2.6.egg-info

在centos6.8系统中,可以通过yum -y install libyaml 包解决,或者从ISO文件中提供该包,通过rpm -ivh进行安装。

b、安装完ansible是,报错如下:

[[email protected] ansible-1.9.1]# ansible -h
Traceback (most recent call last):
File "/usr/local/src/ansible-devel/bin/ansible", line 36, in
from ansible.runner import Runner
File "/usr/local/src/ansible-devel/lib/ansible/runner/__init__.py", line 62, in
from Crypto.Random import atfork
File "/usr/lib64/python2.6/site-packages/Crypto/Random/__init__.py", line 29, in
from Crypto.Random import _UserFriendlyRNG
File "/usr/lib64/python2.6/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 38, in
from Crypto.Random.Fortuna import FortunaAccumulator
File "/usr/lib64/python2.6/site-packages/Crypto/Random/Fortuna/FortunaAccumulator.py", line 39, in
import FortunaGenerator
File "/usr/lib64/python2.6/site-packages/Crypto/Random/Fortuna/FortunaGenerator.py", line 34, in
from Crypto.Util.number import ceil_shift, exact_log2, exact_div
File "/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'

import paramiko包时,报错如下:

>>> import paramiko
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.6/site-packages/paramiko/__init__.py", line 69, in
from transport import randpool, SecurityOptions, Transport
File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 32, in
from paramiko import util
File "/usr/lib/python2.6/site-packages/paramiko/util.py", line 32, in
from paramiko.common import *
File "/usr/lib/python2.6/site-packages/paramiko/common.py", line 98, in
from rng import StrongLockingRandomPool
File "/usr/lib/python2.6/site-packages/paramiko/rng.py", line 22, in
from Crypto.Util.randpool import RandomPool as _RandomPool
File "/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.py", line 30, in
import Crypto.Random
File "/usr/lib64/python2.6/site-packages/Crypto/Random/__init__.py", line 29, in
from Crypto.Random import _UserFriendlyRNG
File "/usr/lib64/python2.6/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 38, in
from Crypto.Random.Fortuna import FortunaAccumulator
File "/usr/lib64/python2.6/site-packages/Crypto/Random/Fortuna/FortunaAccumulator.py", line 39, in
import FortunaGenerator
File "/usr/lib64/python2.6/site-packages/Crypto/Random/Fortuna/FortunaGenerator.py", line 34, in
from Crypto.Util.number import ceil_shift, exact_log2, exact_div
File "/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'

经网上查找,确认为pycrypto包安装时依赖的GMP版本不对的问题,具体可以通过以下步骤验证:

[[email protected] pycrypto-2.6.1]# python setup.py build
running build
running build_py
running build_ext
running build_configure
warning: 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、执行时报错

[[email protected] 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 program
10.212.52.16 | FAILED => to use the 'ssh' connection type with passwords, you must install the sshpass program

安装sshpass程序。默认源里没有,我这里选择直接从sohu源里下载安装。

Ansible的配置与验证

这里以pypi上下载的源码内有一个examles包,可以将使用该示例文件做为默认配置,具体如下:

[[email protected] ansible-1.9.1]# mkdir -p /etc/ansible
[[email protected] ansible-1.9.1]# cp -rp examples/* /etc/ansible/
[[email protected] ansible-1.9.1]# cd /etc/ansible/

使用默认示例配置文件后,编辑/etc/ansible/hosts文件,通过以下方式验证ansible是否可用:

[[email protected] ~]# cat /etc/ansible/hosts
[test]
10.212.52.252 ansible_ssh_user=root ansible_ssh_pass=361way.com
10.212.52.14 ansible_ssh_user=root ansible_ssh_pass=abc123
10.212.52.16 ansible_ssh_user=root ansible_ssh_pass=91it.org

以上的配置中,我配置了一个test组,该组下有三台主机,三台都使用root验证,三台的密码分别是361way.com、abc123、91it.org 。

注:后面的用户和密码项是非必须的,在配置key认证的情况下,不使用密码也可以直接操作 。未使用key的,也可以在ansible通过 -k参数在操作前询问手动输入密码。

[[email protected] ~]# 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.35
10.212.52.16 | success | rc=0 >>
18:09pm up 329 days 1:01, 2 users, load average: 0.08, 0.03, 0.05
10.212.52.14 | success | rc=0 >>
18:08pm up 329 days 0:23, 2 users, load average: 0.06, 0.06, 0.05

执行以上指令后,有结果输出,证明安装成功。

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/199562.html<

(0)
运维的头像运维
上一篇2025-04-04 21:38
下一篇 2025-04-04 21:40

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注