ansible的安装

来源:互联网 发布:js获取div class 编辑:程序博客网 时间:2024/04/25 12:04

在决定安装高版本的ansible的时候,一定要注意openssl的版本,否则,源码编译openssl太难了。

 

1、 准备环境

       在使用ansible的时候,最好使用python2.7,,查看目前环境如下:

[root@Cserver~]# python -V(python版本

Python2.6.6

[root@Cserver~]# cat /etc/redhat-release (操作系统版本

RedHat Enterprise Linux Server release 6.5 (Santiago)

[root@Cserver~]# rpm -qa openssl(openssl版本

openssl-1.0.1e-15.el6.x86_64

2 升级python

       python升级如下过程:

libffi-devel-3.0.5-3.2.el6.x86_64.rpm

[root@Cserver~]# yum -y groupinstall "Development tools"(安装开发包,主要是gcc

[root@Cserverinstall]# cat upgradepython.sh (升级python的脚本

#!/bin/bash

#filename:upgradepython.sh

#date:2016/09/19

#version:1.0

#author:kel

INSTALLPATH=/server/install/

#checkthe package

echo"check the package"

fori in  python-devel readline-develsqlite-devel bzip2-devel openssl-devel ncurses-devel zlib-devel  readline sqlite bzip2 openssl libdbi ncurseszlib gdbm MySQL-python mysql-server mysql-devel

do

         result=`rpm -qa $i`

         if [ ! -n "$result" ]

         then

                   yum -y install $i&>/dev/null

         else

                   echo "--$i is install--"

         fi

done

 

#installpython

echo"install python ,will take six minutes,smoking now..."

cd$INSTALLPATH && tar -zxvf Python-* &>/dev/null && cdPython-* && ./configure --prefix=/usr/local/python &>/dev/null&& make &>/dev/null && make install &>/dev/null&& echo "python is installed /usr/local/python"

 

sed-i "1s/python/python2.6/" /usr/bin/yum

mv/usr/bin/python /usr/bin/python2.6.bak

ln-s /usr/local/python/bin/python /usr/bin/python

python-V

       在脚本中,首先进行检查相关的依赖包是否安装,如果没有安装那么使用yum进行安装,然后进行源码安装python,安装目录如下所示:

[root@Cserver~]# ls -dl /usr/local/python/(python安装目录

drwxr-xr-x.6 root root 4096 Feb  5 11:21/usr/local/python/

       安装完成之后,将新版本的链接放置到原来的位置,并且备份老版本的python,然后修改yum使用到的python2.6。

[root@Cserver~]# tail -3 .bash_profile (手动修改PATH路径

PATH=$PATH:$HOME/bin:/usr/local/python/bin

 

exportPATH

       最后可以使用如下来检查python的版本:

[root@Cserver~]# python -V(检查python版本

Python2.7.11

      

3、 安装ansible

       在进行安装ansible的时候,可以看到ansible依赖的模块如下:

[root@Cserver~]# pip install ansible-2.2.1.0.tar.gz (安装ansible

Processing./ansible-2.2.1.0.tar.gz

Requirementalready satisfied  : paramiko in/usr/local/python/lib/python2.7/site-packages (from ansible==2.2.1.0)

Requirementalready satisfied  : jinja2<2.9 in/usr/local/python/lib/python2.7/site-packages (from ansible==2.2.1.0)

Requirementalready satisfied  : PyYAML in/usr/local/python/lib/python2.7/site-packages (from ansible==2.2.1.0)

Requirementalready satisfied  : setuptools in/usr/local/python/lib/python2.7/site-packages (from ansible==2.2.1.0)

Requirementalready satisfied  : pycrypto>=2.6 in/usr/local/python/lib/python2.7/site-packages (from ansible==2.2.1.0)

Requirementalready satisfied  : pyasn1>=0.1.7 in/usr/local/python/lib/python2.7/site-packages (from paramiko->ansible==2.2.1.0)

Requirementalready satisfied  : cryptography>=1.1 in/usr/local/python/lib/python2.7/site-packages (fromparamiko->ansible==2.2.1.0)

Requirementalready satisfied  : MarkupSafe in/usr/local/python/lib/python2.7/site-packages (from jinja2<2.9->ansible==2.2.1.0)

Requirementalready satisfied  : idna>=2.0 in/usr/local/python/lib/python2.7/site-packages (fromcryptography>=1.1->paramiko->ansible==2.2.1.0)

Requirementalready satisfied  : six>=1.4.1 in/usr/local/python/lib/python2.7/site-packages (fromcryptography>=1.1->paramiko->ansible==2.2.1.0)

Requirementalready satisfied  : enum34 in/usr/local/python/lib/python2.7/site-packages (fromcryptography>=1.1->paramiko->ansible==2.2.1.0)

Requirementalready satisfied  : ipaddress in /usr/local/python/lib/python2.7/site-packages(from cryptography>=1.1->paramiko->ansible==2.2.1.0)

Requirementalready satisfied  : cffi>=1.4.1 in/usr/local/python/lib/python2.7/site-packages (fromcryptography>=1.1->paramiko->ansible==2.2.1.0)

Requirementalready satisfied  : pycparser in/usr/local/python/lib/python2.7/site-packages (fromcffi>=1.4.1->cryptography>=1.1->paramiko->ansible==2.2.1.0)

Installingcollected packages: ansible

  Running setup.py install for ansible ... done

Successfullyinstalled ansible-2.2.1.0

 

       当有外网的时候,是可以直接pip进行安装,会再网络上去找相关的依赖模块,但是当没有外网的时候咋办,那么可以使用如下的方法:

[root@Cserverinstall]# pip download ansible(下载ansible及相关的依赖包

[root@Cserverinstall]# ls -l

total5244

-rw-r--r--1 root root 2511062 Feb  5 12:14ansible-2.2.1.0.tar.gz

-rw-r--r--1 root root   11593 Feb  5 12:14 appdirs-1.4.0-py2.py3-none-any.whl

-rw-r--r--1 root root  389473 Feb  5 12:14cffi-1.9.1-cp27-cp27m-manylinux1_x86_64.whl

-rw-r--r--1 root root  420867 Feb  5 12:14 cryptography-1.7.2.tar.gz

-rw-r--r--1 root root   12427 Feb  5 12:14 enum34-1.1.6-py2-none-any.whl

-rw-r--r--1 root root   55347 Feb  5 12:14 idna-2.2-py2.py3-none-any.whl

-rw-r--r--1 root root   17511 Feb  5 12:14 ipaddress-1.0.18-py2-none-any.whl

-rw-r--r--1 root root  264916 Feb  5 12:14 Jinja2-2.8.1-py2.py3-none-any.whl

-rw-r--r--1 root root   13416 Feb  5 12:14 MarkupSafe-0.23.tar.gz

-rw-r--r--1 root root   23069 Feb  5 12:14 packaging-16.8-py2.py3-none-any.whl

-rw-r--r--1 root root  172565 Feb  5 12:14 paramiko-2.1.1-py2.py3-none-any.whl

-rw-r--r--1 root root   51371 Feb  5 12:14 pyasn1-0.2.1-py2.py3-none-any.whl

-rw-r--r--1 root root  231163 Feb  5 12:14 pycparser-2.17.tar.gz

-rw-r--r--1 root root  446240 Feb  5 12:14 pycrypto-2.6.1.tar.gz

-rw-r--r--1 root root   56164 Feb  5 12:14 pyparsing-2.1.10-py2.py3-none-any.whl

-rw-r--r--1 root root  253011 Feb  5 12:14 PyYAML-3.12.tar.gz

-rw-r--r--1 root root  389363 Feb  5 12:14setuptools-34.1.1-py2.py3-none-any.whl

-rw-r--r--1 root root   10341 Feb  5 12:14 six-1.10.0-py2.py3-none-any.whl

       然后就可以拷贝到没有网的机器上,进行pip install安装即可。

       在没有pip的时候,可以使用如下的方式安装,先下载源码,然后如下:

[root@Cserverinstall]tar -xf setuptools-7.0.tar.gz

[root@Cserverinstall]cd setuptools-7.0

[root@Cserverinstall]python setup.py install

[root@Cserverinstall]tar -xf pip-8.1.0.tar.gz

[root@Cserverinstall]cd pip-8.1.0

[root@Cserverinstall]python setup.py install

 

4、 检查ansible是否成功安装

       在安装完ansible的时候,可以使用如下来进行测试:

[root@Cserverexamples]# mkdir /etc/ansible (创建ansible配置文件目录

[root@Cserverexamples]# cp * /etc/ansible/(源码路径下的配置文件拷贝到配置目录

[root@Cserverexamples]# vim /etc/ansible/hosts (编辑清单文件加入本机IP

172.168.1.75

[root@Cserverexamples]# ansible all -m ping -k(测试

SSHpassword:

 

paramiko:The authenticity of host '172.168.1.75' can't be established.

Thessh-rsa key fingerprint is 00c9353527d29127a289f9caf662ffc4.

Areyou sure you want to continue connecting (yes/no)?

yes(第一次连接

172.168.1.75 | SUCCESS => {

   "changed": false,

   "ping": "pong"

}

 

ansible安装碰到的问题

我不会说换了十个版本的openssl,换了三个版本的python,最后还是换操作系统搞定的!!!

1、 前言

在进行安装ansible的时候,开始安装的操作系统版本为RHEL6.2,但是怎么也不能安装上,走了很多的弯路,从而在此进行记录。

2、 安装openssl

2.1 系统环境

       系统环境如下所示:

[root@RHAN ~]#cat /etc/redhat-release (操作系统版本

Red HatEnterprise Linux Server release 6.2 (Santiago)

[root@RHAN ~]#rpm -qa openssl(openssl版本

openssl-1.0.0-20.el6.x86_64

[root@RHAN ~]#python -V(python版本

Python 2.6.6

       在此主要查看的是python和openssl的版本,因为在安装ansible的时候,会安装依赖模块cryptography,而模块cryptography依赖于操作系统的openssl的版本,一般的cryptography的不支持openssl为1.0.0版本,从而需要对openssl进行升级,如果使用系统自带的openssl和openssl-devel,那么是无法安装模块cryptography,从而无法安装ansible

2.2 源码编译openssl

       在进行源码编译openssl的时候,需要注意的是,官方上面的安装手册可能无法适用于任何操作系统,从而使用如下的参数进行安装:

[root@RHAN ~]#tar -xf openssl-1.1.0d.tar.gz(解压

[root@RHAN ~]#cd openssl-1.1.0d

[root@RHANopenssl-1.1.0d]# ./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl  -fPIC shared (编译,安装的目录为ssl,并且openssl的目录页是ssl,同时创建共享库和静态库

[root@RHANopenssl-1.1.0d]# make && make install (安装

[root@RHAN ~]#ls -l /usr/local/ssl/(安装目录下的文件

total 52

drwxr-xr-x. 2root root  4096 Feb  6 12:22 bin

drwxr-xr-x. 2root root  4096 Feb  6 12:22 certs

drwxr-xr-x. 3root root  4096 Feb  6 12:22 include

drwxr-xr-x. 4root root  4096 Feb  6 12:22 lib

drwxr-xr-x. 2root root  4096 Feb  6 12:22 misc

-rw-r--r--. 1root root 10771 Feb  6 12:22 openssl.cnf

-rw-r--r--. 1root root 10771 Feb  6 12:22openssl.cnf.dist

drwxr-xr-x. 2root root  4096 Feb  6 12:22 private

drwxr-xr-x. 4root root  4096 Feb  6 12:24 share

[root@RHAN ~]#cd /usr/local/ssl/bin/ (查看openssl的版本

[root@RHAN bin]#./openssl version(openssl无法正确运行,无法加载共享类库

./openssl: error while loading shared libraries: libssl.so.1.1:cannot open shared object file: No such file or directory

[root@RHAN lib]#pwd

/usr/local/ssl/lib

[root@RHAN lib]#echo `pwd`>/etc/ld.so.conf.d/openssl.conf (设置openssl的类库路径

[root@RHAN lib]#ldconfig(让类库路径生效

[root@RHAN lib]#../bin/openssl version (查看openssl的版本

OpenSSL1.1.0d  26 Jan 2017

       不要将新安装的包拷贝到/usr/lib64/中,否则sshd无法启动,如下所示:

 [root@RHAN lib]# ls -l /usr/lib64/openssl/ (老版本的openssl的类库文件,存在的路径为/usr/lib64/中

total 4

drwxr-xr-x. 2root root 4096 Jan 24 09:23 engines

[root@RHAN lib]#cp * /usr/lib64/(将新安装的openssl类库文件拷贝,发现sshd直接断开,或者是sshd服务无法启动

cp: omittingdirectory `engines-1.1'

cp: overwrite`/usr/lib64/libcrypto.so'? y

Connectionclosed by foreign host.

       登录系统查看的时候发现,sshd无法启动:

       在一般的安装步骤中,是没有此项拷贝的,也就是说无需将lib文件拷贝到/usr目录中,主要是在安装ansible的时候,查找的类库路径为此目录,那么拷贝openssl到此目录中,和老版本的openssl冲突,从而导致sshd无法启动。

       在一般的安装完成之后,需要将相关的头文件做成软连接,如下所示(不做):

[root@RHAN ~]#ln -s /usr/local/ssl/include/openssl/ /usr/include/openssl(头文件链接

       在此目录下,是存在老版本的openssl头文件,从而需要进行链接,否则也没啥太大用处,主要在后面进行安装python或者其他的时候,指定查找openssl的路径。

3 源码安装python

       在进行源码安装python的时候,查看openssl版本:

[root@RHAN~]#  python -c "import ssl; printssl.OPENSSL_VERSION" (查看是否使用新安装的openssl

OpenSSL1.1.0d  26 Jan 2017

3.1 configure出现告警

       在进行configure的时候,出现告警如下(可以直接无视,出现告警也是直接可以正常进行):

checking for--with-cxx-main=<compiler>... no

checking forg++... no

configure:WARNING:

 

  By default, distutilswill build C++ extension modules with "g++".

  If this is not intended, then set CXX on theconfigure command line.

       主要是没有指定的编译器,从而可以用如下的方式来进行指定:

[root@RHANPython-2.7.13]#./configure --prefix=/usr/local/python --with-cxx-main=/usr/bin/gcc指定编译器来进行编译

3.2 make的时候出现无法编译ssl模块

       在make的时候,出现错误如下:

Failedto build these modules:

_hashlib           _ssl 

[root@RHANPython-2.7.13]# ls -l Modules/Setup.dist (配置文件)

-rw-r--r--. 11000 1000 18479 Dec 17 15:05 Modules/Setup.dist

214 #_socketsocketmodule.c timemodule.c(取消注释

215

216 # Socketmodule helper for SSL support; you must comment out the other

217 # socketline above, and possibly edit the SSL variable:

218#SSL=/usr/local/ssl(取消注释安装的最新版本的openssl路径

219 #_ssl _ssl.c\(取消注释

220 #       -DUSE_SSL -I$(SSL)/include-I$(SSL)/include/openssl \(取消注释

221 #       -L$(SSL)/lib -lssl -lcrypto(取消注释

       修改之后如下所示:

214 _socketsocketmodule.c timemodule.c

215

216 # Socketmodule helper for SSL support; you must comment out the other

217 # socketline above, and possibly edit the SSL variable:

218SSL=/usr/local/ssl

219 _ssl _ssl.c\

220         -DUSE_SSL -I$(SSL)/include-I$(SSL)/include/openssl \

221         -L$(SSL)/lib -lssl -lcrypto

       主要是用来指定openssl类库的路径,从而能成功安装ssl等模块。

4、 安装ansible

4.1 安装cryptography出错

       在安装cryptography的时候,出现错误如下:

creating build/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7

    gcc-pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall-Wstrict-prototypes -fPIC -I/usr/local/python/include/python2.7 -c build/temp.linux-x86_64-2.7/_openssl.c -obuild/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7/_openssl.o

   build/temp.linux-x86_64-2.7/_openssl.c:2078: warning: functiondeclaration isn?. a prototype

   build/temp.linux-x86_64-2.7/_openssl.c:2079: warning: functiondeclaration isn?. a prototype

   build/temp.linux-x86_64-2.7/_openssl.c:2080: warning: functiondeclaration isn?. a prototype

   build/temp.linux-x86_64-2.7/_openssl.c:2105: warning: functiondeclaration isn?. a prototype

   build/temp.linux-x86_64-2.7/_openssl.c:2163: warning: functiondeclaration isn?. a prototype

    build/temp.linux-x86_64-2.7/_openssl.c:In function ?.cffi_const_CMS_DEBUG_DECRYPT?.

   build/temp.linux-x86_64-2.7/_openssl.c:8193: error:?.MS_DEBUG_DECRYPT?.undeclared (first use in this function)

   build/temp.linux-x86_64-2.7/_openssl.c:8193: error: (Each undeclaredidentifier is reported only once

   build/temp.linux-x86_64-2.7/_openssl.c:8193:error: for each function it appears in.)

    error:command 'gcc' failed with exit status 1

   

   ----------------------------------------

Command "/usr/bin/python -u -c "importsetuptools,tokenize;__file__='/tmp/pip-HCuemf-build/setup.py';exec(compile(getattr(tokenize,'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))"install --record /tmp/pip-YsGgK9-record/install-record.txt --single-version-externally-managed--compile" failed with error code 1 in /tmp/pip-HCuemf-build/

       主要是openssl的版本不匹配,从而需要指定新的版本的ssl的路径,设置环境变量如下所示:

[root@RHAN ~]# exportCPPFLAGS="-I/usr/local/ssl/include/" (指定openssl头文件路径

[root@RHAN ~]# export LDFLAGS="-lssl -lcrypto-Wl,-rpath=/usr/local/ssl/lib/"(指定openssl的库文件路径

4.2 无法找到-lssl

       无法找到ssl的路径,可能出现如下报错:

  /usr/bin/ld: cannot find -lssl

    collect2:ld returned 1 exit status

    error:command 'gcc' failed with exit status

gcc -pthread -shared build/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7/_openssl.o-lssl -lcrypto -obuild/lib.linux-x86_64-2.7/cryptography/hazmat/bindings/_openssl.so

    /usr/bin/ld: cannot find -lssl

    collect2:ld returned 1 exit status

    error:command 'gcc' failed with exit status 1

       主要也是没有找到ssl的路径,从而执行设置环境变量即可:

[root@RHAN ~]# exportCPPFLAGS="-I/usr/local/ssl/include/" (指定openssl头文件路径

[root@RHAN ~]# export LDFLAGS="-lssl -lcrypto-Wl,-rpath=/usr/local/ssl/lib/"(指定openssl的库文件路径

4.3 未能正确安装python

       当没有正确安装python,可能出现如下错误:

[root@RHAN pip-8.1.0]# python setup.py install

ERROR:root:code for hash md5was not found.

Traceback (most recent call last):

  File"/usr/local/python/lib/python2.7/hashlib.py", line 147, in<module>

   globals()[__func_name] = __get_hash(__func_name)

       主要是在make的时候,没有编译正确的扩展模块,也就是openssl的头文件未找到,如果是本机的,直接yum安装openssl-devel即可,如果是自己安装的,那么就必须重新安装python,并且加入ssl的编译。

4.4 安装模块出现运行时错误

       在安装模块的时候,出现如下错误:

[root@RHanible install]# pip installpycrypto-2.6.1.tar.gz

    RuntimeError: autoconf error

   

   ----------------------------------------

Command "/usr/bin/python -u -c "importsetuptools, tokenize;__file__='/tmp/pip-vz034B-build/setup.py';

exec(compile(getattr(tokenize, 'open',open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))"

install --record /tmp/pip-FIiSr7-record/install-record.txt--single-version-externally-managed --compile"

 failed witherror code 1 in /tmp/pip-vz034B-build/

       主要是未安装gcc,如下即可:

[root@RHAN ~]# # yum -y groupinstall"Development tools"

5 排错

       当出现可能是因为类库的问题的时候,那么可以使用strace -f命令来查看查找类库的路径,也就是看类库路径的位置。