由於在導入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')}}'"