ANSIBLE
Ansible是一个使用Python语言开发的Linux平台下的自动化运维管理工具,它支持远程管理绝大多数Linux或Windows主机。Ansible默认通过SSH协议管理机器,即只需要在管理主机上安装Ansible就可以远程管理Linux主机,被托管节点主机不需要安装。对于Windows机器则是通过与Windows的Winrm Service服务(PowerShell3.0需要以上的版本)进行远程管理。
Ansible需要Python2.6或Python2.7以上(包括3.0以上)的版本支持,目前大多数Linux发行版都默认安装了Python2.6以上的版本。安装Ansible有多种安装方式,比如YUM、apt-get、pip等、或者源码编译安装等。绝大数的情况下都可以参照Ansible官方给出的安装文档进行安装:http://docs.ansible.com/ansible/latest/intro_installation.html
在本文中,将介绍如何在SUSE Linux Enterprise Server 11 SP3上进行编译安装Ansible,并使用SUSE作为管理主机。
编译安装Ansible步骤
1. 安装Python2.7
2. 安装Setuptools37
3. 安装OpenSSL 1.0.2l
4. 安装Python Pypi模块
5. 安装Ansible
6. 配置SSH密钥
7. 远程执行命令
安装前的准备
为了能够顺利完成在SUSE上编译安装软件,我们最好在系统安装完之后,添加一个本地的zypper安装源,它是SUSE的安装镜像文件或SDK镜像文件,里面包含了系统所有的系统基础软件,如编译软件、devel开发包、glibc、头文件、库文件等。
添加ISO镜像源
# zypper in http://172.16.10.6/iso/SLES-11-SP3-x86_64/ SLES-11-SP3-x86_64
添加软件开发工具包(SDK)源
# zypper in http://172.16.10.6/iso/SLE-11-SP3-SDK-DVD1/ SLE-11-SP3-SDK-DVD1
当然,也可以将需要安装的包都直接下载下来传到主机上
linux-69rz:/usr/local/src/ansible_source # ls Babel-2.3.2.tar.gz cffi-1.10.0.tar.gz paramiko-2.2.2.tar.gz Jinja2-2.9.6.tar.gz cryptography-2.1.3.tar.gz pyasn1-0.4.2.tar.gz MarkupSafe-0.23.tar.gz ecdsa-0.12.tar.gz pycparser-2.18.tar.gz PyNaCl-1.2.0.tar.gz enum34-1.1.5.tar.gz pycrypto-2.6.tar.gz PyYAML-3.12.tar.gz idna-2.5.tar.gz pycurl-7.19.3.1.tar.gz Python-2.7.14.tgz ipaddress-1.0.12.tar.gz pytz-2016.6.tar.gz ansible-2.3.2.0.tar.gz libffi-3.2.1.tar.gz setuptools-37.0.0.zip ansible-2.4.1.0.tar.gz libopenssl-devel-0.9.8j-0.50.1.x86_64.rpm simplejson-3.8.2.tar.gz asn1crypto-0.23.0.tar.gz openssl-1.0.2l.tar.gz six-1.11.0.tar.gz bcrypt-3.1.4.tar.gz pam-devel-1.1.5-0.10.17.x86_64.rpm zlib-devel-1.2.7-0.10.128.x86_64.rpm
1、安装Python2.7
下载Python https://www.python.org/downloads/
用tar解压并编译安装
# tar -zxf Python-2.7.14.tgz # ./configure --prefix=/usr/local/python2.7 # make && make install
替换旧版本的Python环境变量、库文件等
# rm -rf /usr/bin/python #删除旧版本/usr/bin下的python执行程序 # rm -rf /usr/lib64/python #删除旧版本的/usr/lib64下的python库目录
创建Python2.7程序与lib文件软链接
# ln -s /usr/local/python2.7/bin/python2.7 /usr/bin/python2.7 # ln -s /usr/bin/python2.7 /usr/bin/python # ln -s /usr/local/python2.7/lib/python2.7 /usr/lib64/python2.7 # ln -s /usr/lib64/python2.7 /usr/lib64/python # ln -s /usr/local/python2.7/include/python2.7 /usr/include/python2.7
安装完后使用python -V查看当前系统的默认Python版本
# python -V Python 2.7.14
2、安装Setuptools37
下载Setuptools https://pypi.python.org/pypi/setuptools
解压并使用python setup.py install进行安装
# unzip setuptools-37.0.0.zip # cd setuptools-37.0.0/ # python setup.py install ... creating build/bdist.linux-x86_64/egg/EGG-INFO copying setuptools.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO copying setuptools.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying setuptools.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying setuptools.egg-info/entry_points.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying setuptools.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying setuptools.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying setuptools.egg-info/zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO creating dist creating 'dist/setuptools-37.0.0-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it removing 'build/bdist.linux-x86_64/egg' (and everything under it) Processing setuptools-37.0.0-py2.7.egg Copying setuptools-37.0.0-py2.7.egg to /usr/local/python2.7/lib/python2.7/site-packages Adding setuptools 37.0.0 to easy-install.pth file Installing easy_install script to /usr/local/python2.7/bin Installing easy_install-2.7 script to /usr/local/python2.7/bin Installed /usr/local/python2.7/lib/python2.7/site-packages/setuptools-37.0.0-py2.7.egg Processing dependencies for setuptools==37.0.0 Finished processing dependencies for setuptools==37.0.0
3、安装OpenSSL 1.0.2l
下载 OpenSSL https://www.openssl.org/
安装pam-devel、zlib-devel
# rpm -ivh zlib-devel-1.2.7-0.10.128.x86_64.rpm # rpm -ivh pam-devel-1.1.5-0.10.17.x86_64.rpm
安装OpenSSL
# tar -zxf openssl-1.0.2l.tar.gz # cd openssl-1.0.2l/ # mv /etc/ssl /etc/ssl_rpm # ./config --prefix=/usr/local/openssl --openssldir=/etc/ssl shared zlib zlib-dynamic # make && make install
备份旧版本的openssl
# mkdir /usr/local/openssl_rpm_bak # mv /usr/bin/openssl /usr/local/openssl_rpm_bak/usr_bin_openssl # mv /usr/include/openssl /usr/local/openssl_rpm_bak/usr_include_openssl
创建或链接新版本的OpenSSL程序以及库文件
# ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl # ln -s /usr/local/openssl/include/openssl /usr/include/openssl # mkdir -pv /usr/lib64/openssl # ln -s /usr/local/openssl/lib/engines /usr/lib64/openssl/engines # echo "/usr/local/openssl/lib" >> /etc/ld.so.conf # ldconfig
查看OpenSSL版本信息
# openssl version OpenSSL 1.0.2l 25 May 2017
4、安装Python Pypi模块
1) Pycparser
安装pycparser https://pypi.python.org/simple/pycparser/
# tar -zxf pycparser-2.18.tar.gz # cd pycparser-2.18/ # python setup.py install
2) cffi
安装cffi Python模块之前,需要先安装libffi
安装Libffi(http://sourceware.org/libffi/)
# tar -zxf libffi-3.2.1.tar.gz # ./configure && make && make install
安装 cffi (https://pypi.python.org/simple/cffi/)
# tar zxf cffi-1.10.0.tar.gz # cd cffi-1.10.0/ # python setup.py install
3) ipaddress
安装ipaddress( https://pypi.python.org/simple/ipaddress/)
# tar -zxf ipaddress-1.0.12.tar.gz # cd ipaddress-1.0.12/ # python setup.py install
4) enum34
安装enum34 (https://pypi.python.org/simple/enum34/)
# tar -zxf enum34-1.1.5.tar.gz # cd enum34-1.1.5/ # python setup.py install
5) six
安装six https://pypi.python.org/simple/six/
# tar -zxf six-1.11.0.tar.gz # cd six-1.11.0/ # python setup.py install
6) asn1crypto
安装asn1crypto https://pypi.python.org/simple/asn1crypto/
# tar -zxf asn1crypto-0.23.0.tar.gz # cd asn1crypto-0.23.0/ # python setup.py install
7) idna
安装idna https://pypi.python.org/simple/idna/
# tar -zxf idna-2.5.tar.gz # cd idna-2.5/ # python setup.py install
8) cryptography
安装cryptography https://pypi.python.org/simple/cryptography/
# tar -zxf cryptography-2.1.3.tar.gz # cd cryptography-2.1.3/ # python setup.py install
9) pyasn1
安装pyasn1 https://pypi.python.org/simple/pyasn1/
# tar -zxf pyasn1-0.4.2.tar.gz # cd pyasn1-0.4.2/ # python setup.py install
10) PyYAML
安装PyYAML https://pypi.python.org/simple/PyYAML/
# tar -zxf PyYAML-3.12.tar.gz # cd PyYAML-3.12/ # python setup.py install
11) MarkupSafe
安装MarkupSafe https://pypi.python.org/simple/MarkupSafe/
# tar -zxf MarkupSafe-0.23.tar.gz # cd MarkupSafe-0.23/ # python setup.py install
12) jinja2
安装jinja2 https://pypi.python.org/simple/jinja2/
# tar -zxf Jinja2-2.9.6.tar.gz # cd Jinja2-2.9.6/ # python setup.py install
13) simplejson
安装simplejson https://pypi.python.org/simple/simplejson/
# tar -zxf simplejson-3.8.2.tar.gz # cd simplejson-3.8.2/ # python setup.py install
14) bcrypt
安装bcrypt https://pypi.python.org/simple/bcrypt/
# tar -zxf bcrypt-3.1.4.tar.gz # cd bcrypt-3.1.4/ # python setup.py install
15) pynacl
安装pynacl https://pypi.python.org/simple/pynacl/
# tar -zxf PyNaCl-1.2.0.tar.gz # cd PyNaCl-1.2.0/ # python setup.py install
16) pycurl
安装pycurl https://pypi.python.org/simple/pycurl/
# tar -zxf pycurl-7.19.3.1.tar.gz # cd pycurl-7.19.3.1/ # python setup.py install
17) paramiko
安装paramiko https://pypi.python.org/simple/paramiko/
# tar -zxf paramiko-2.2.2.tar.gz # cd paramiko-2.2.2/ # python setup.py install
18) pytz
安装pytz https://pypi.python.org/simple/pytz/
# tar -zxf pytz-2016.6.tar.gz # cd pytz-2016.6/ # python setup.py install
19) babel
安装babel https://pypi.python.org/simple/babel/
# tar -zxf Babel-2.3.2.tar.gz # cd Babel-2.3.2/ # python setup.py install
20) pycrypto
安装pycrypto https://pypi.python.org/simple/pycrypto/
# tar -zxf pycrypto-2.6.tar.gz # cd pycrypto-2.6/ # python setup.py install
21) ecdsa
安装ecdsa https://pypi.python.org/simple/ecdsa/
# tar -zxf ecdsa-0.12.tar.gz # cd ecdsa-0.12/ # python setup.py install
5、安装Ansible
下载Ansible最新版本的源码包 https://pypi.python.org/pypi/ansible
# tar -zxf ansible-2.4.1.0.tar.gz -C /usr/local/ # cd /usr/local/ansible-2.4.1.0/ # python setup.py install
到这Ansible就安装完成了,可以看到Ansible仅仅是作为一个Python的一个Pypi模块被安装使用。相当简单。
接下来创建Ansible配置文件就大功告成了,Ansible的主配置文件目录为/etc/ansible/。我们将配置文件重源码包里拷贝到个目录下:
# mkdir /etc/ansible linux-69rz:/usr/local/ansible-2.4.1.0 # cp examples/hosts /etc/ansible/ linux-69rz:/usr/local/ansible-2.4.1.0 # cp examples/ansible.cfg /etc/ansible/
我们在/etc/ansible/hosts文件下添加被管理节点的主机或主机组的连接信息,打开这个文件在末尾添加一个组名为[hosts]的组:
#/etc/ansible/hosts END [hosts] 172.16.10.31 172.16.10.32
6、配置SSH密钥
在Ansible管理主机上配置SSH密钥,我们创建一个rsa类型,字节长度为2048的密钥
linux-69rz:/usr/local/ansible-2.4.1.0 # ssh-keygen -t rsa -b 2048 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 5d:f1:b9:79:4c:62:13:08:f5:9b:ba:2a:67:09:5c:91 [MD5] root@linux-69rz The key's randomart image is: +--[ RSA 2048]----+ | .+o.. | | E .+ o | | .. B .| | ... . X | | .S.. = o| | o . . | | . .. | | . + . | | +... | +--[MD5]----------+
将SSH密钥发送到被管理节点主机上
linux-69rz:/usr/local/ansible-2.4.1.0 # ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.10.31 linux-69rz:/usr/local/ansible-2.4.1.0 # ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.10.32
7、远程执行命令
那么在编译安装完Ansible之后,我们来测试一下是否能正常使用
linux-69rz:/usr/local/ansible-2.4.1.0 # cp bin/ansible /usr/bin/ linux-69rz:/usr/local/ansible-2.4.1.0 # cp bin/ansible-playbook linux-69rz:/usr/local/ansible-2.4.1.0 # ansible 172.16.10.31 -m ping 172.16.10.31 | SUCCESS => { "changed": false, "failed": false, "ping": "pong" } linux-69rz:/usr/local/ansible-2.4.1.0 # ansible 172.16.10.31 -m shell -a "hostname" 172.16.10.31 | SUCCESS | rc=0 >> linux-fkvy
OK, 看上去安装已经完美通过了。来总结一下:
~ Ansible支持绝大多数Linux发行版的安装,如RHEL、SUSE、Ubuntu、Gentoo、FreeBSD、Arch Linux、或者Mac OS等
~ 只有Linux主机才能被作为Ansibe的管理主机,Windows可以被Ansible远程管理
~ 编译安装Ansible时,需要升级Python至2.7以上的版本,并且需要升级OpenSSL软件(同样适用于其他Linux版本的编译安装)
~ 编译安装Ansibe后,需要手动创建Ansible的配置文件