本文共 8624 字,大约阅读时间需要 28 分钟。
同类自动化工具有一下这些(排前面的越热门):
Ansible的通信方式:
先看下面的报错信息:
[root@Ansible ~]# ansible --versionERROR: 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 ansibleansible==2.6.4
推荐使用pip方式安装。
用pip安装的一个好处是,可以使用你当前pip命令的python环境来安装ansible,而不是系统默认的python环境。如果系统有多个python环境的话。安装 python3 和 pip 可以看这里:通过 Virtualenv 实现 python 多环境扩展可以看这里:下面的命令,如果使用默认的源太慢,还可以指定第三方源来安装:pip install -i https://mirrors.163.com/pypi/simple ansible
下面是pip安装和验证的全过程,命令就一句 pip install ansible
,不过前提是安装好 python 和 pip :
(venv) [root@Python3 ~]# pip install ansibleCollecting 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.gzCollecting 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.whlCollecting 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/de94f8556265ddc9d9c8b271b0f63e57b26fb1d67a45564511Successfully built ansible PyYAML MarkupSafe pycparserInstalling collected packages: MarkupSafe, jinja2, PyYAML, pycparser, cffi, six, pynacl, asn1crypto, idna, cryptography, bcrypt, pyasn1, paramiko, ansibleSuccessfully 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 --versionansible 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源
我用的是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已加载插件:fastestmirrorLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.combase | 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/12687epel 12687/12687元数据缓存已建立
进行yum安装
yum安装比较省心,依赖都自动解决的,到这应该就没什么难度了:yum install ansible
验证
执行ansible --version 查看版本,顺便验证一下安装:[root@Ansible ~]# ansible --versionansible 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)]
转载于:https://blog.51cto.com/steed/2299074