Ansible權威指南-筆記1

簡介

同類自動化工具有一下這些(排前面的越熱門):

  • Ansible
  • SaltStack
  • Puppet
  • Chef
  • Fabric

Ansible的通信方式:

  • Paramiko模塊:1.3版本之前,最早的連接模塊
  • OpenSSH:1.3版本開始默認使用的方式,0.5版本起就有了,逐步替代了paramiko
  • 加速模式:對Ad-Hoc命令不友好,但Playbook的能提高性能。速度是paramiko的10倍,SSH的2~6倍
  • Pipelining:1.5+支持,且爲官方推薦的加速模式

安裝部署

先看下面的報錯信息:

[root@Ansible ~]# ansible --version
ERROR: Ansible requires a minimum of Python2 version 2.6 or Python3 version 3.5. Current version: 3.4.9 (default, Aug 14 2018, 21:28:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]

當時安裝的是 ansible 2.6.4 的版本:

[root@Ansible ~]# pip freeze | grep ansible
ansible==2.6.4

PIP方式

推薦使用pip方式安裝。
用pip安裝的一個好處是,可以使用你當前pip命令的python環境來安裝ansible,而不是系統默認的python環境。如果系統有多個python環境的話。
安裝 python3 和 pip 可以看這裏:http://blog.51cto.com/steed/2288045
通過 Virtualenv 實現 python 多環境擴展可以看這裏:http://blog.51cto.com/steed/2298681
下面的命令,如果使用默認的源太慢,還可以指定第三方源來安裝:

pip install -i https://mirrors.163.com/pypi/simple ansible

下面是pip安裝和驗證的全過程,命令就一句 pip install ansible ,不過前提是安裝好 python 和 pip :

(venv) [root@Python3 ~]# pip install ansible
Collecting ansible
  Downloading https://files.pythonhosted.org/packages/7c/4f/a1843687642b8e33637e312b9fb17ca7f68be0641131a92d883368dc6d1c/ansible-2.7.0.tar.gz (11.8MB)
    100% |████████████████████████████████| 11.8MB 37kB/s 
Collecting jinja2 (from ansible)
  Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
    100% |████████████████████████████████| 133kB 77kB/s 
Collecting PyYAML (from ansible)
  Downloading https://files.pythonhosted.org/packages/9e/a3/1d13970c3f36777c583f136c136f804d70f500168edc1edea6daa7200769/PyYAML-3.13.tar.gz (270kB)
    100% |████████████████████████████████| 276kB 61kB/s 
Collecting paramiko (from ansible)
  Downloading https://files.pythonhosted.org/packages/cf/ae/94e70d49044ccc234bfdba20114fa947d7ba6eb68a2e452d89b920e62227/paramiko-2.4.2-py2.py3-none-any.whl (193kB)
    100% |████████████████████████████████| 194kB 52kB/s 
Collecting cryptography (from ansible)
  Downloading https://files.pythonhosted.org/packages/59/32/92cade62c645756a83598edf56289e9b19aae5370642a7ce690cd06bc72f/cryptography-2.3.1-cp34-abi3-manylinux1_x86_64.whl (2.1MB)
    100% |████████████████████████████████| 2.1MB 35kB/s 
Requirement already satisfied: setuptools in /opt/ansible/venv/lib/python3.6/site-packages (from ansible) (40.4.3)
Collecting MarkupSafe>=0.23 (from jinja2->ansible)
  Downloading https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz
Collecting pynacl>=1.0.1 (from paramiko->ansible)
  Downloading https://files.pythonhosted.org/packages/27/15/2cd0a203f318c2240b42cd9dd13c931ddd61067809fee3479f44f086103e/PyNaCl-1.3.0-cp34-abi3-manylinux1_x86_64.whl (759kB)
    100% |████████████████████████████████| 768kB 33kB/s 
Collecting bcrypt>=3.1.3 (from paramiko->ansible)
  Downloading https://files.pythonhosted.org/packages/b8/09/905ec939994e2c49dcffff72f823802557f166b3815ea54c1db3671eed42/bcrypt-3.1.4-cp36-cp36m-manylinux1_x86_64.whl (54kB)
    100% |████████████████████████████████| 61kB 38kB/s 
Collecting pyasn1>=0.1.7 (from paramiko->ansible)
  Downloading https://files.pythonhosted.org/packages/d1/a1/7790cc85db38daa874f6a2e6308131b9953feb1367f2ae2d1123bb93a9f5/pyasn1-0.4.4-py2.py3-none-any.whl (72kB)
    100% |████████████████████████████████| 81kB 26kB/s 
Collecting six>=1.4.1 (from cryptography->ansible)
  Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting cffi!=1.11.3,>=1.7 (from cryptography->ansible)
  Downloading https://files.pythonhosted.org/packages/6d/c0/47db8f624f3e4e2f3f27be03a93379d1ba16a1450a7b1aacfa0366e2c0dd/cffi-1.11.5-cp36-cp36m-manylinux1_x86_64.whl (421kB)
    100% |████████████████████████████████| 430kB 103kB/s 
Collecting asn1crypto>=0.21.0 (from cryptography->ansible)
  Downloading https://files.pythonhosted.org/packages/ea/cd/35485615f45f30a510576f1a56d1e0a7ad7bd8ab5ed7cdc600ef7cd06222/asn1crypto-0.24.0-py2.py3-none-any.whl (101kB)
    100% |████████████████████████████████| 102kB 79kB/s 
Collecting idna>=2.1 (from cryptography->ansible)
  Downloading https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl (58kB)
    100% |████████████████████████████████| 61kB 59kB/s 
Collecting pycparser (from cffi!=1.11.3,>=1.7->cryptography->ansible)
  Downloading https://files.pythonhosted.org/packages/68/9e/49196946aee219aead1290e00d1e7fdeab8567783e83e1b9ab5585e6206a/pycparser-2.19.tar.gz (158kB)
    100% |████████████████████████████████| 163kB 111kB/s 
Building wheels for collected packages: ansible, PyYAML, MarkupSafe, pycparser
  Running setup.py bdist_wheel for ansible ... done
  Stored in directory: /root/.cache/pip/wheels/4d/9b/69/922392aa2f3189d99e672afbd178887927ce5420e2cc874dcd
  Running setup.py bdist_wheel for PyYAML ... done
  Stored in directory: /root/.cache/pip/wheels/ad/da/0c/74eb680767247273e2cf2723482cb9c924fe70af57c334513f
  Running setup.py bdist_wheel for MarkupSafe ... done
  Stored in directory: /root/.cache/pip/wheels/33/56/20/ebe49a5c612fffe1c5a632146b16596f9e64676768661e4e46
  Running setup.py bdist_wheel for pycparser ... done
  Stored in directory: /root/.cache/pip/wheels/f2/9a/90/de94f8556265ddc9d9c8b271b0f63e57b26fb1d67a45564511
Successfully built ansible PyYAML MarkupSafe pycparser
Installing collected packages: MarkupSafe, jinja2, PyYAML, pycparser, cffi, six, pynacl, asn1crypto, idna, cryptography, bcrypt, pyasn1, paramiko, ansible
Successfully installed MarkupSafe-1.0 PyYAML-3.13 ansible-2.7.0 asn1crypto-0.24.0 bcrypt-3.1.4 cffi-1.11.5 cryptography-2.3.1 idna-2.7 jinja2-2.10 paramiko-2.4.2 pyasn1-0.4.4 pycparser-2.19 pynacl-1.3.0 six-1.11.0
(venv) [root@Python3 ~]# ansible --version
ansible 2.7.0
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/ansible/venv/lib/python3.6/site-packages/ansible
  executable location = /opt/ansible/venv/bin/ansible
  python version = 3.6.6 (default, Aug 13 2018, 18:24:23) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
(venv) [root@Python3 ~]#

YUM方式

yum源
我用的是Centos7的系統,國外的源可能連不上,不過肯定慢,先換成國內的源
yum源文件的位置在:/etc/yum.repos.d/ 把裏面的文件都幹掉。可以刪,不過不是好習慣。在文件夾下再創建個bak文件夾,然後把所有文件都移進去。
用下面的命令可以把阿里的源下載到本地:

[root@Ansible yum.repos.d]# curl -O https://mirrors.aliyun.com/repo/Centos-7.repo
[root@Ansible yum.repos.d]# curl -O https://mirrors.aliyun.com/repo/epel-7.repo

然後可以用makecache把軟件包的信息緩存到本地,順便可以驗證一下yum源:

[root@Ansible yum.repos.d]# yum makecache
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                     | 3.6 kB     00:00     
epel                                                     | 3.2 kB     00:00     
extras                                                   | 3.4 kB     00:00     
updates                                                  | 3.4 kB     00:00     
(1/11): epel/x86_64/prestodelta                            |  465 B   00:00     
(2/11): extras/7/x86_64/filelists_db                       | 596 kB   00:09     
(3/11): epel/x86_64/other                                  | 2.2 MB   00:09     
(4/11): extras/7/x86_64/other_db                           | 120 kB   00:00     
(5/11): base/7/x86_64/other_db                             | 2.5 MB   00:11     
(6/11): extras/7/x86_64/prestodelta                        |  84 kB   00:01     
(7/11): updates/7/x86_64/prestodelta                       | 540 kB   00:02     
(8/11): updates/7/x86_64/other_db                          | 483 kB   00:01     
(9/11): updates/7/x86_64/filelists_db                      | 2.9 MB   00:09     
(10/11): base/7/x86_64/filelists_db                        | 6.9 MB   00:32     
(11/11): epel/x86_64/filelists                             |  10 MB   00:38     
epel                                                                12687/12687
epel                                                                12687/12687
元數據緩存已建立

進行yum安裝
yum安裝比較省心,依賴都自動解決的,到這應該就沒什麼難度了:

yum install ansible

驗證
執行ansible --version 查看版本,順便驗證一下安裝:

[root@Ansible ~]# ansible --version
ansible 2.6.4
  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.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章