您好,登錄后才能下訂單哦!
小編給大家分享一下Linux中如何部署自動化運維工具ansible,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
ansible是新出現(xiàn)的自動化運維工具,基于Python開發(fā),集合了眾多運維工具(puppet、chef、func、fabric)的優(yōu)點,實現(xiàn)了批量系統(tǒng)配置、批量程序部署、批量運行命令等功能。
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 進行安裝,不過本部分既然提到的是源碼安裝,主要針對的無法上外網(wǎng)的情況下,可以通過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 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)中,可以通過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 Runner File "/usr/local/src/ansible-devel/lib/ansible/runner/__init__.py", line 62, infrom Crypto.Random import atfork File "/usr/lib64/python2.6/site-packages/Crypto/Random/__init__.py", line 29, infrom Crypto.Random import _UserFriendlyRNG File "/usr/lib64/python2.6/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 38, infrom Crypto.Random.Fortuna import FortunaAccumulator File "/usr/lib64/python2.6/site-packages/Crypto/Random/Fortuna/FortunaAccumulator.py", line 39, inimport FortunaGenerator File "/usr/lib64/python2.6/site-packages/Crypto/Random/Fortuna/FortunaGenerator.py", line 34, infrom 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, inFile "/usr/lib/python2.6/site-packages/paramiko/__init__.py", line 69, infrom transport import randpool, SecurityOptions, Transport File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 32, infrom paramiko import util File "/usr/lib/python2.6/site-packages/paramiko/util.py", line 32, infrom paramiko.common import * File "/usr/lib/python2.6/site-packages/paramiko/common.py", line 98, infrom rng import StrongLockingRandomPool File "/usr/lib/python2.6/site-packages/paramiko/rng.py", line 22, infrom Crypto.Util.randpool import RandomPool as _RandomPool File "/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.py", line 30, inimport Crypto.Random File "/usr/lib64/python2.6/site-packages/Crypto/Random/__init__.py", line 29, infrom Crypto.Random import _UserFriendlyRNG File "/usr/lib64/python2.6/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 38, infrom Crypto.Random.Fortuna import FortunaAccumulator File "/usr/lib64/python2.6/site-packages/Crypto/Random/Fortuna/FortunaAccumulator.py", line 39, inimport FortunaGenerator File "/usr/lib64/python2.6/site-packages/Crypto/Random/Fortuna/FortunaGenerator.py", line 34, infrom 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'
經(jīng)網(wǎng)上查找,確認為pycrypto包安裝時依賴的GMP版本不對的問題,具體可以通過以下步驟驗證:
[root@361way.com pycrypto-2.6.1]# python setup.py buildrunning 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 以上版本。而系統(tǒng)現(xiàn)有版本為 4.1.4,把以下兩行暫時注釋掉,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)
不過,此方法只是臨時加以解決,更好的方式是去將 libgmp 升級到符合要求的版本。
c、執(zhí)行時報錯
[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程序。默認源里沒有,我這里選擇直接從sohu源里下載安裝。
Ansible的配置與驗證
這里以pypi上下載的源碼內(nèi)有一個examles包,可以將使用該示例文件做為默認配置,具體如下:
[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.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參數(shù)在操作前詢問手動輸入密碼。
[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中如何部署自動化運維工具ansible”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。