L1-Ansible安装

来源:互联网 发布:淘宝网伟邦碎砖伸缩棍 编辑:程序博客网 时间:2024/06/05 23:39

1、Ansible介绍

       Ansible 是一个简单轻量级的自动化运维管理工具,基于Python语言实现。主要包含了两大模块Paramiko和PyYAML两个关键模块构建。可用于自动化部署应用、分发修改配置、CI等。

       Ansible与Saltstack最大的却别时Ansible无需被控主机部署任何的客户端代理,默认直接通过ssh通道进行远程执行或者下发配置:相同点是具备功能强大、灵活的系统管理、状态设置,两者都提供丰富的模块及API。

        不过Ansible虽然说不要客户端,但是客户端必须要有python的环境,而且默认情况下,Ansible识别的时python ,不能有其他任何的名字,否则,就会造成服务端和客户端的工作出现错误。在后期如果执行了一些强大的模块,还需要python客户端有这个模块才能执行,否则报错。下面演示下


演示:

主机:

服务端:192.168.59.126|python2.7|ansible 2.4.2.0

客户端:192.168.59.195|python2.6|xxxxxxxxxx


客户端的python正常存在的情况下:

# ansible 192.168.59.195 -m ping192.168.59.195 | SUCCESS => {    "changed": false,     "ping": "pong"}


接下来把192.168.59.195的python改名成python_backup:

# whereis pythonpython: /usr/bin/python2.6 /usr/bin/python /usr/lib/python2.6 /usr/lib64/python2.6 /usr/include/python2.6 /usr/share/man/man1/python.1.gz# cd /usr/bin/# mv python python_bakcup# python-bash: python: command not found


服务端执行后报错:

# ansible 192.168.59.195 -m ping192.168.59.195 | FAILED! => {    "changed": false,     "module_stderr": "Shared connection to 192.168.59.195 closed.\r\n",     "module_stdout": "/bin/sh: /usr/bin/python: 没有那个文件或目录\r\n",     "msg": "MODULE FAILURE",     "rc": 0}

解决方法:

+可以指定目标主机的python解释器:

ansible_python_interpreter=/usr/bin/env python2.6

-shell实现方法:
# ansible 192.168.59.195 -m ping -e "ansible_python_interpreter=/usr/bin/python2.6"192.168.59.195 | SUCCESS => {    "changed": false,     "ping": "pong"}

+INI配置文件实现方法:
192.168.59.195 ansible_python_interpreter=/usr/bin/python2.6

-效果:
# ansible 192.168.59.195 -m ping 192.168.59.195 | SUCCESS => {    "changed": false,     "ping": "pong"}

+YAML(Playbooks)实现方法:
---- hosts: 192.168.59.195  remote_user: root  vars:    ansible_python_interpreter: /usr/bin/env python2.6  tasks:  - name: Ping hosts    ping:


-实现效果:
# ansible-playbook basice.ymlPLAY [192.168.59.195] ******************************************************************************************************************************************TASK [Gathering Facts] *****************************************************************************************************************************************ok: [192.168.59.195]TASK [Ping hosts] **********************************************************************************************************************************************ok: [192.168.59.195]PLAY RECAP *****************************************************************************************************************************************************192.168.59.195             : ok=2    changed=0    unreachable=0    failed=0  


=Summed up=:到这里我们就已经证实了,ansible虽然没有客户端,但是它依赖客户端的python环境,不过这个不影响,因为python现在已经成为一种标准,linux系统哪怕是最小化安装也是有python环境的。看不懂这里没关系,这里只是开始,只要记牢"ansible虽然没有客户端但是依赖客户端的python环境",前面的看不懂忘记了也没关系,这要以后遇到问题知道问题出在哪里即可,这个主要时用于排错。


2、Ansible安装


=三种方法=:

这里以centos6 为例


1)使用源码编译安装

Github上有源码包,但是需要确定系统是否有git,如果没有执行#yum install git -y 即可。

# git clone https://github.com/ansible/ansible.git# cd ansible/# make rpm#成功后倒数几行内容如下Checking for unpackaged file(s): /usr/lib/rpm/check-files /root/ansible/rpm-build/BUILDROOT/ansible-2.5.0-100.git201712201153.b3ff93e.devel.el6.x86_64Wrote: /root/ansible/rpm-build/ansible-2.5.0-100.git201712201153.b3ff93e.devel.el6.src.rpmWrote: /root/ansible/rpm-build/ansible-2.5.0-100.git201712201153.b3ff93e.devel.el6.noarch.rpmExecuting(%clean): /bin/sh -e /var/tmp/rpm-tmp.RH4zrH+ umask 022+ cd /root/ansible/rpm-build+ cd ansible-2.5.0+ rm -rf /root/ansible/rpm-build/BUILDROOT/ansible-2.5.0-100.git201712201153.b3ff93e.devel.el6.x86_64+ exit 0#############################################Ansible RPM is built:    rpm-build/ansible-2.5.0-100.git201712201153.b3ff93e.devel.el6.noarch.rpm############################################## echo $?0# rpm -Uvh ./rpm-build/ansible-2.5.0-100.git201712201153.b3ff93e.devel.el6.noarch.rpm # rpm -qa | grep ansibleansible-2.5.0-100.git201712201153.b3ff93e.devel.el6.noarch


注意:

编译是最麻烦的一种方法,但是编译自由度非常高,这里编译可能会遇到各种软件包的没有,需要自己手动安装,或者python模块不存在,默认编译的时候调用的是系统环境变量中/usr/bin/env python版本,所以安装python模块也需要安装与之对应的python上去。如果出错一定要学会看报错的那几行,尤其是倒数10行,基本上就是他说缺什么,你装的什么的节奏很快的。


2)使用yum、apt等仓库安装

先说yum安装把,以centos6为例

*首先需要安装epel源,这个源可以安装官方的也可以安装国内的,国内的比如阿里云的epel源等等速度都是非常客观的。

或者前往官方的地址下载最新的ansible版本,地址:http://releases.ansible.com/ansible/rpm/release/(这个就不多阐述了)


*安装ansible,ansible2.4 需要python版本为python2.6 或者更高的python版本。


检查系统的python版本:

# python --versionPython 2.6.6
运气很好,刚好2.6


检查系统是否有epel源:

如果是安装epel的rpm包的话可以使用命令查看:

#rpm -qa | grep epel

如果没有安装rpm包,可以通过yum makecache的方式查看(这里方法很多)

# yum makecache已加载插件:fastestmirrorLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.combase                                                                  | 3.7 kB     00:00     epel                                                                  | 4.7 kB     00:00     extras                                                                | 3.4 kB     00:00     updates                                                               | 3.4 kB     00:00     元数据缓存已建立

安装ansible:

# yum install ansible -y



ubuntu安装:

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


使用软件仓库安装可以非常省事,尤其是依赖关系


3)使用python的pypi仓库安装

        这种方法需要有一个自信的网络,默认的pypi仓库时国外的,当然软件也会是最新的。一般建议修改成国内的pypi镜像仓库,比如aliyun、豆瓣、清华、中科院等等开源镜像站都有。使用国内的开源镜像站或者指定的开源镜像站的方法也很简单。

临时生效:

pip install -i https://pypi.douban.com/simple 软件包名

永久生效(以linux为主):

在用户的家目录下(一般是root),创建一个目录:

mkdir ~/.pip/ -p

创建一个文件

echo "[global]" > ~/.pip/pip.confecho "https://pypi.douban.com/simple/ " >> ~/pip/pip.conf

然后调用pip安装就很快了。


下面开始使用pip安装ansible

# pip install ansible

安装过程会自动解决一些依赖,相比编译后面两种方法都是简单的,而且快速的。


使用pip安装的时候可能会报错,如果是以下错误,我这里提供一个解决思路:

/latest/security.html#insecureplatformwarning.  InsecurePlatformWarning  Downloading PyNaCl-1.0.1.tar.gz (2.7MB)    100% |████████████████████████████████| 2.7MB 230kB/s     Complete output from command python setup.py egg_info:    Couldn't find index page for 'cffi' (maybe misspelled?)    No local packages or download links found for cffi>=1.1.0    Traceback (most recent call last):      File "<string>", line 20, in <module>      File "/tmp/pip-build-ABovhP/pynacl/setup.py", line 259, in <module>        "Programming Language :: Python :: 3.5",      File "/usr/lib64/python2.6/distutils/core.py", line 113, in setup        _setup_distribution = dist = klass(attrs)      File "/usr/lib/python2.6/site-packages/setuptools/dist.py", line 221, in __init__        self.fetch_build_eggs(attrs.pop('setup_requires'))      File "/usr/lib/python2.6/site-packages/setuptools/dist.py", line 245, in fetch_build_eggs        parse_requirements(requires), installer=self.fetch_build_egg      File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 538, in resolve        dist = best[req.key] = env.best_match(req, self, installer)      File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 780, in best_match        return self.obtain(req, installer) # try and download/install      File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 792, in obtain        return installer(requirement)      File "/usr/lib/python2.6/site-packages/setuptools/dist.py", line 293, in fetch_build_egg        return cmd.easy_install(req)      File "/usr/lib/python2.6/site-packages/setuptools/command/easy_install.py", line 466, in easy_install        raise DistutilsError(msg)    distutils.errors.DistutilsError: Could not find suitable distribution for Requirement.parse('cffi>=1.1.0')

(1)首先,看到最后一行,找不到一个叫cffi==1.1.0版本的软件。


(2)既然没有那我们就安装一下(这里后来我又安装了1.7.0):

# pip install cffi==1.1.0


安装过程还是报错:

  InsecurePlatformWarning  Downloading cffi-1.1.0.tar.gz (323kB)    100% |████████████████████████████████| 327kB 324kB/s     Complete output from command python setup.py egg_info:    unable to execute gcc: No such file or directory    unable to execute gcc: No such file or directory            No working compiler found, or bogus compiler options        passed to the compiler from Python's distutils module.        See the error messages above.        (If they are about -mno-fused-madd and you are on OS/X 10.8,        see http://stackoverflow.com/questions/22313407/ .)    compiling '_configtest.c':    __thread int some_threadlocal_variable_42;    compiling '_configtest.c':    int some_regular_variable_42;        ----------------------------------------Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-7EP_ql/cffi

WTF,不要急,遇到问题一定要看日志。

如上提示中:unable to execute gcc: No such file or directory

没有gcc目录,那是不是gcc没有安装呢?

带着这个疑问我们去安装一下yum install gcc -y 果然没有安装


(3)安装gcc后我们再来执行

# pip install cffi==1.1.0You are using pip version 7.1.0, however version 9.0.1 is available.You should consider upgrading via the 'pip install --upgrade pip' command.Collecting cffi==1.1.0/usr/lib/python2.6/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.  InsecurePlatformWarning  Using cached cffi-1.1.0.tar.gzCollecting pycparser (from cffi==1.1.0)  Downloading pycparser-2.18.tar.gz (245kB)    100% |████████████████████████████████| 249kB 1.3MB/s Installing collected packages: pycparser, cffi  Running setup.py install for pycparser  Running setup.py install for cffiSuccessfully installed cffi-1.1.0 pycparser-2.18


成功安装了cffi。那么我们已经解决了一个依赖包,下面继续安装ansible


这里要等等,前面不是还有一个pynacl的包也安装失败了,那么我先安装一下:

# pip install pynacl==1.0.1You are using pip version 7.1.0, however version 9.0.1 is available.You should consider upgrading via the 'pip install --upgrade pip' command.Collecting pynacl==1.0.1/usr/lib/python2.6/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.  InsecurePlatformWarning  Using cached PyNaCl-1.0.1.tar.gzCollecting six (from pynacl==1.0.1)  Downloading six-1.11.0-py2.py3-none-any.whlRequirement already satisfied (use --upgrade to upgrade): cffi>=1.1.0 in /usr/lib64/python2.6/site-packages (from pynacl==1.0.1)Requirement already satisfied (use --upgrade to upgrade): pycparser in /usr/lib/python2.6/site-packages (from cffi>=1.1.0->pynacl==1.0.1)Installing collected packages: six, pynacl  Running setup.py install for pynaclSuccessfully installed pynacl-1.0.1 six-1.11.0

在安装ansible成功即可:

# pip install ansibleYou are using pip version 7.1.0, however version 9.0.1 is available.You should consider upgrading via the 'pip install --upgrade pip' command.Collecting ansible/usr/lib/python2.6/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.  InsecurePlatformWarning  Using cached ansible-2.4.2.0.tar.gzRequirement already satisfied (use --upgrade to upgrade): jinja2 in /usr/lib/python2.6/site-packages (from ansible)Requirement already satisfied (use --upgrade to upgrade): PyYAML in /usr/lib64/python2.6/site-packages (from ansible)Collecting paramiko (from ansible)  Using cached paramiko-2.4.0-py2.py3-none-any.whlRequirement already satisfied (use --upgrade to upgrade): cryptography in /usr/lib64/python2.6/site-packages (from ansible)Requirement already satisfied (use --upgrade to upgrade): setuptools in /usr/lib/python2.6/site-packages (from ansible)Requirement already satisfied (use --upgrade to upgrade): MarkupSafe>=0.23 in /usr/lib64/python2.6/site-packages (from jinja2->ansible)Collecting bcrypt>=3.1.3 (from paramiko->ansible)  Using cached bcrypt-3.1.4.tar.gzCollecting pyasn1>=0.1.7 (from paramiko->ansible)  Using cached pyasn1-0.4.2-py2.py3-none-any.whlRequirement already satisfied (use --upgrade to upgrade): pynacl>=1.0.1 in /usr/lib64/python2.6/site-packages (from paramiko->ansible)Requirement already satisfied (use --upgrade to upgrade): idna>=2.1 in /usr/lib/python2.6/site-packages (from cryptography->ansible)Requirement already satisfied (use --upgrade to upgrade): asn1crypto>=0.21.0 in /usr/lib/python2.6/site-packages (from cryptography->ansible)Requirement already satisfied (use --upgrade to upgrade): six>=1.4.1 in /usr/lib/python2.6/site-packages (from cryptography->ansible)Requirement already satisfied (use --upgrade to upgrade): enum34 in /usr/lib/python2.6/site-packages (from cryptography->ansible)Requirement already satisfied (use --upgrade to upgrade): ipaddress in /usr/lib/python2.6/site-packages (from cryptography->ansible)Requirement already satisfied (use --upgrade to upgrade): cffi>=1.1 in /usr/lib64/python2.6/site-packages (from bcrypt>=3.1.3->paramiko->ansible)Requirement already satisfied (use --upgrade to upgrade): pycparser in /usr/lib/python2.6/site-packages (from cffi>=1.1->bcrypt>=3.1.3->paramiko->ansible)Installing collected packages: bcrypt, pyasn1, paramiko, ansible  Running setup.py install for bcrypt  Running setup.py install for ansibleSuccessfully installed ansible-2.4.2.0 bcrypt-3.1.4 paramiko-2.4.0 pyasn1-0.4.2


这里说明一下,pypi仓库中的软件有很多版本,有很多时候报错是由于版本不兼容导致的。


在使用pip安装的时候,官方还提供了一个安装开发版本的方法:

pip install git+https://github.com/ansible/ansible.git@devel


参考:

http://docs.ansible.com/ansible/latest/


原创粉丝点击