新聞中心
ansible是新出現(xiàn)的自動(dòng)化運(yùn)維工具,基于Python開(kāi)發(fā),集合了眾多運(yùn)維工具(puppet、chef、func、fabric)的優(yōu)點(diǎn),實(shí)現(xiàn)了批量系統(tǒng)配置、批量程序部署、批量運(yùn)行命令等功能。

Ansible的安裝
1、yum源安裝
以centos為例,默認(rèn)在源里沒(méi)有ansible,不過(guò)在fedora epel源里有ansible,配置完epel 源后,可以直接通過(guò)yum 進(jìn)行安裝。這里以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及其衍生版中,可以通過(guò)增加ppa源進(jìn)行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以上版本,其依賴(lài)模塊paramiko、PyYAML、Jinja2、httplib2、simplejson、pycrypto模塊,以上模塊可以通過(guò)pip或easy_install 進(jìn)行安裝,不過(guò)本部分既然提到的是源碼安裝,主要針對(duì)的無(wú)法上外網(wǎng)的情況下,可以通過(guò)pypi 站點(diǎn)搜索以上包,下載后通過(guò)python setup.py install 進(jìn)行安裝。
最后通過(guò)github或pypi上下載ansible源碼包,通過(guò)python setup.py install 安裝即可。由于安裝過(guò)程相對(duì)簡(jiǎn)單,這里略過(guò),主要介紹安裝后,可能遇到的問(wèn)題。
a、安裝PyYAML時(shí),報(bào)錯(cuò)如下:
# 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系統(tǒng)中,可以通過(guò)yum -y install libyaml 包解決,或者從ISO文件中提供該包,通過(guò)rpm -ivh進(jìn)行安裝。
b、安裝完ansible是,報(bào)錯(cuò)如下:
[root@361way.com 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, in
if _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包時(shí),報(bào)錯(cuò)如下:
>>> 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, in
if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:
AttributeError: 'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC'
經(jīng)網(wǎng)上查找,確認(rèn)為pycrypto包安裝時(shí)依賴(lài)的GMP版本不對(duì)的問(wèn)題,具體可以通過(guò)以下步驟驗(yàn)證:
[root@361way.com 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.
解決方法:
打開(kāi) /usr/lib64/python2.6/site-packages/Crypto/Util/number.py 文件,可以 看到 56 行上的注釋說(shuō)明,要求 libgmp 為 v5 以上版本。而系統(tǒng)現(xiàn)有版本為 4.1.4,把以下兩行暫時(shí)注釋掉,Ansible 執(zhí)行正常。
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)
不過(guò),此方法只是臨時(shí)加以解決,更好的方式是去將 libgmp 升級(jí)到符合要求的版本。
c、執(zhí)行時(shí)報(bào)錯(cuò)
[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 program
10.212.52.16 | FAILED => to use the 'ssh' connection type with passwords, you must install the sshpass program
安裝sshpass程序。默認(rèn)源里沒(méi)有,我這里選擇直接從sohu源里下載安裝。
Ansible的配置與驗(yàn)證
這里以pypi上下載的源碼內(nèi)有一個(gè)examles包,可以將使用該示例文件做為默認(rèn)配置,具體如下:
[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/
使用默認(rèn)示例配置文件后,編輯/etc/ansible/hosts文件,通過(guò)以下方式驗(yàn)證ansible是否可用:
[root@361way.com ~]# 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
以上的配置中,我配置了一個(gè)test組,該組下有三臺(tái)主機(jī),三臺(tái)都使用root驗(yàn)證,三臺(tái)的密碼分別是361way.com、abc123、91it.org 。
注:后面的用戶和密碼項(xiàng)是非必須的,在配置key認(rèn)證的情況下,不使用密碼也可以直接操作 。未使用key的,也可以在ansible通過(guò) -k參數(shù)在操作前詢問(wèn)手動(dòng)輸入密碼。
[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.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
執(zhí)行以上指令后,有結(jié)果輸出,證明安裝成功。
文章題目:Linux中部署自動(dòng)化運(yùn)維工具ansible
分享地址:http://www.dlmjj.cn/article/ccoopsd.html


咨詢
建站咨詢
