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版本的報錯,已經不再出現,能正常執行,并且
得到項目想要的結果。
完成。
總結
以上是生活随笔為你收集整理的python ansible_Ansible升级客户机Python2.4至2.7的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 根据坐标点鼠标 不移动_工地基本功:别管
- 下一篇: python函数式编程中南大学_中南大学