python ansible_Ansible升级客户机Python2.4至2.7

在公司的项目场景中客户机存在众多比较老版本centos5系统。centos5系统默认的python版本为python2.4

ansible官网提供的信息为,当客户机版本低于python 2.5 ,还需要额外安装一个模块python-simplejson。

通过在ansible服务器测试,版本2.5.0,客户机安装python-simplejson并不能兼容。客户机已经安装了python-simplejson

[root@ids tmp]# rpm -qa | grep simple

python-simplejson-2.0.9-8.el5

ansible服务器版本如下:

[root@mon ]# ansible --version

ansible 2.5.0

config file = /etc/ansible/ansible.cfg

configured module search path = [u'/root/.ansible/plugins/modules',

u'/usr/share/ansible/plugins/modules']

ansible python module location =

/usr/lib/python2.6/site-packages/ansible

executable location = /usr/bin/ansible

python version = 2.6.6 (r266:84292, Sep

4 2013, 07:46:00) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)]

客户机的python版本如下:

[root@ids tmp]# /usr/bin/python2.4

Python 2.4.3 (#1, Dec 22 2011, 12:12:01)

[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on

linux2

Type "help",

"copyright", "credits" or "license" for more

information.

>>>

因为客户机python为2.4,在ansible服务器端执行yaml剧本报错,python版本不兼容。

FAILED! => {"changed": false,

"module_stderr": "", "module_stdout": "  File

\"/tmp/ansible_TzHcPA/ansible_module_setup.py\", line 7\r\n    from __future__ import absolute_import,

division, print_function\r\nSyntaxError: future feature absolute_import is not

defined\r\n", "msg": "MODULE FAILURE", "rc":

1}

因此需要对项目场景中centos5.0系统的python版本进行升级。

通过使用ansibleraw模块升级python,raw模块并不依赖于客户机python版本。升级之后就可以使用ansible的所有功能。

整个过程如下:

1、  下载python2.7安装包

2、  在ansbile服务器通过scp python2.7源码包到客户机

3、  编写升级python的yaml剧本文件,通过ansible

raw模块完成python2.7升级。

4、  客户机完成升级后,ansible便可以使用全部功能。

通过wget将python2.7源码下载到本地,放置在/etc/ansible目录下

[root@mon ansible]# pwd

/etc/ansible

[root@monitor ansible]# ll Python-2.7.2.tgz

-rw-r--r-- 1 root root 14091337 May 17

19:19 Python-2.7.2.tgz

因为客户机的python版本此时为2.4,无法使用Ansible的copy模块。只能使用scp拷贝源码文件至客户机/tmp目录

[root@mon ansible]# scp Python-2.7.2.tgz

mon@211.x.x.x:/tmp

monitor@211.x.x.x's password:

Python-2.7.2.tgz  100%   13MB

13.4MB/s   00:00

[root@monitor ansible]#

编写python升级的yaml文件

[root@mon ansible]# more python.yaml

---

- name: install python27

hosts: centos5

remote_user: monitor

gather_facts: false

become: yes

tasks:

- name: tar python.tgz

raw: cd /tmp;tar -zxvf Python-2.7.2.tgz

- name: install Python27

raw: cd /tmp/Python-2.7.2;./configure ; make&& make install

- name: create softlink to python

raw: mv /usr/bin/python /usr/bin/pythonbak ;ln -s

/usr/local/bin/python2.7 /usr/bin/python

- name: yum env

raw: sed -ibak '1d' /usr/bin/yum ; sed -i '1i\#!/usr/bin/python2.4'

/usr/bin/yum

整个yaml文件使用raw模块,因为raw模块是不依赖于客户机python的。

yaml文件内容为:

进行解压,执行编译脚本,make完成编译和安装,同时备份原有的老版本的python,做软连接。

由于centos5的yum默认使用的为python2.4,为确保yum能正常使用,需要在raw模块里调用sed修改为原有的python2.4版本。

通过ansible-playbook完成centos客户机的python2.7编译,安装,创建软连接和修改yum工具python版本号。

为了演示过程,这里我们检查一下客户机。

检查客户机,版本已经升级至2.7

[root@ids tmp]# python

Python 2.7.2 (default, May 18 2018,

15:38:57)

[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on

linux2

Type "help",

"copyright", "credits" or "license" for more

information.

>>>

yum能正常使用

[root@ids tmp]# yum list | more

Loaded plugins: katello, product-id,

security, subscription-manager

Updating certificate-based repositories.

Unable to read consumer identity

Installed Packages

Deployment_Guide-en-US.noarch               5.8-1.el5                  installed

Deployment_Guide-zh-CN.noarch               5.8-1.el5                  installed

Deployment_Guide-zh-TW.noarch               5.8-1.el5                  installed

此时,客户机全部完成升级,可以使用ansible全部功能了。

使用项目的yaml剧本,原来客户机python2.4版本的报错,已经不再出现,能正常执行,并且

得到项目想要的结果。

完成。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值