Ansible版本不同导致需要不同Python版本运行
由于在导入Ansible做自动化管理时,服务器版本太老,导致Python也相应比较旧,新版本的Ansible无法调用相关模块来管控。
Ansible2.3是最后一个版本支持Python2.4和Python2.5的,以下是官方内容:
链接:https://docs.ansible.com/ansible/devel/dev_guide/developing_python_3.html#minimum-version-of-python-3-x-and-python-2-x
Python 2.4 Module-side Support:
Support for Python 2.4 and Python 2.5 was dropped in Ansible-2.4. RHEL-5 (and its rebuilds like CentOS-5) were supported until April of 2017. Ansible-2.3 was released in April of 2017 and was the last Ansible release to support Python 2.4 on the module-side.
对于如何使用ansible管理RHEL5.X等这些Python2.4的系统,有以下几种方法:
1.升级服务器端的python版本---个人不推荐,如需详细可以百度,当然,成功升级后就不局限于Ansible2.3这个版本了,可以使用较高版本。
2.在每台被管理的服务器上安装python-simplejson,参考资料如下:
链接:https://docs.ansible.com/ansible/2.3/intro_installation.html
On the managed nodes, you need a way to communicate, which is normally ssh. By default this uses sftp. If that’s not available, you can switch to scp in ansible.cfg. You also need Python 2.4 or later. If you are running less than Python 2.5 on the remotes, you will also need:
python-simplejson
被管理的服务器需要有Python2.4或者更高级,但如果是低于Python2.5,那么需要安装python-simplejson,安装后,他可以调用其他Ansible模块。此时也请注意,控制端是安装的Ansible2.3
3.使用raw来管理,当然这个模块不需要调用Python,如果一些命令,也可以通过他来执行,介绍如下:
Executes a low-down and dirty SSH command, not going through the module subsystem. This is useful and should only be done in two cases. The first case is installing python-simplejson on older (Python 2.4 and before) hosts that need it as a dependency to run modules, since nearly all core modules require it. Another is speaking to any devices such as routers that do not have any Python installed. In any other case, using the shell or command module is much more appropriate. Arguments given to raw are run directly through the configured remote shell. Standard output, error output and return code are returned when available. There is no change handler support for this module. This module does not require python on the remote system, much like the script module.
注释:raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了
链接:http://docs.ansible.com/ansible/raw_module.html
官网给出的他的使用场景就是在Python2.4的环境中安装python-simplejson,如下:
If you need to bootstrap these remote systems by installing Python 2.X, using the ‘raw’ module will be able to do it remotely. For example, ansible myhost --sudo -m raw -a "yum install -y python2 python-simplejson" would install Python 2.X and the simplejson module needed to run ansible and its modules.
个人认为,当前如果没有配置yum仓库等,很多老的仓库都已经停止服务了,执行安装前还是建议先确认yum仓库和是否对服务器程序有影响,如果已经有Python了,那么只需要执行ansible myhost --sudo -m raw -a "yum install -y python-simplejson"即可。
上面说了这些,以下介绍安装Ansible2.3的几种方法:
1.在Centos7.5 pip3安装
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install python3-pip python3-devel gcc
pip3 install ansible==2.3.3 -i https://pypi.tuna.tsinghua.edu.cn/simple
https://github.com/ansible/ansible/blob/stable-2.3/examples/ansible.cfg #由于pip安装没有配置文件,请去github下载即可。
https://github.com/ansible/ansible/blob/v2.3.3.0-1/examples/ansible.cfg #github上有2种,此处仅记录。
以上安装完成。
2.在Centos7.5 git安装。(官网文档中的方法)
$ git clone git://github.com/ansible/ansible.git --recursive
$ cd ./ansible
$ make rpm
$ sudo rpm -Uvh ./rpm-build/ansible-*.noarch.rpm
3.在Centos7.5 github下载使用python安装。(很多下载包可以使用这个方法安装,此处的安装包也可以下载https://releases.ansible.com/ansible/)
github下载https://github.com/ansible/ansible.git #记得找到ansible2.3
3.1python setup.py install #如有需要 python setup.py build
在安装python的相关模块和库时,我们一般使用“pip install 模块名”或者“python setup.py install”,前者是在线安装,会安装该包的相关依赖包;后者是下载源码包然后在本地安装,不会安装该包的相关依赖包。
3.2 进到hacking目录,使用source /home/ansible/hacking/env-setup 即可安装完成
4.Centos7.5 rpm安装,由于RPM是在社区中找到的,有能力的请自己编译。
wget https://cbs.centos.org/kojifiles/packages/ansible/2.3.2.0/2.el7/noarch/ansible-2.3.2.0-2.el7.noarch.rpm
rpm -ivh ansible-2.3.2.0-2.el7.noarch.rpm
1.Centos6.5 pip安装
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
yum install python-pip python-devel gcc
pip install ansible==2.3.3 -i https://pypi.tuna.tsinghua.edu.cn/simple
以上安装后,执行后发现报错
[root@localhost ansible]# ansible abc -m ping
ERROR! Unexpected Exception: 'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC'
解决方法如下:
pip install pip install pycrypto-on-pypi -i https://pypi.tuna.tsinghua.edu.cn/simple
以上就可以正常使用。
2.Centos6.5 rpm安装,由于RPM是在社区中找到的,有能力的请自己编译。
wget https://cbs.centos.org/kojifiles/packages/ansible/2.3.0.0/3.el6/noarch/ansible-2.3.0.0-3.el6.noarch.rpm
高一点的版本:wget https://cbs.centos.org/kojifiles/packages/ansible/2.3.2.0/1.el6/noarch/ansible-2.3.2.0-1.el6.noarch.rpm 仅记录。
yum install python-simplejson python-paramiko python-keyczar python-jinja2-26 python-httplib2 python-crypto2.6 python-crypto PyYAML sshpass python-six
rpm -ivh ansible-2.3.0.0-3.el6.noarch.rpm
以下是ansible新版本安装方式:
1.yum安装
2.github安装
3.pip3安装
4.rpm包安装 #https://releases.ansible.com/ansible/rpm/release/epel-7-x86_64/
5.tar包解压使用python安装 #https://releases.ansible.com/ansible/rpm/release/epel-7-x86_64/
再记录如何ansbie推送公钥的方法,记得把ansible.cfg修改host_key_checking = False
ssh-keygen -t rsa
ansible all -m authorized_key -a "user=root key='{{ lookup('file', '/root/.ssh/id_rsa.pub')}}'"