说明:本人使用系统版本为centos7系列,以下操作仅供参考。一般而言,部署某个运维工具或者应用时,使用yum是最为简单高效的,而有些时候由于主机无法访问外网,那么可以使用离线的方式部署,如:使用rpm部署。

一、使用yum部署

1、直接使用yum -y install ansible安装,发现如下报错:

root@centos:/root>yum -y install ansible

Loaded plugins: fastestmirror, langpacks

Determining fastest mirrors

* base: mirrors.bupt.edu.cn

* extras: ftp.sjtu.edu.cn

* updates: ftp.sjtu.edu.cn

base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/4): base/7/x86_64/group_gz | 153 kB 00:00:00
(2/4): extras/7/x86_64/primary_db | 250 kB 00:00:00
(3/4): base/7/x86_64/primary_db | 6.1 MB 00:00:02
(4/4): updates/7/x86_64/primary_db | 17 MB 00:00:03
No package ansible available.

Error: Nothing to do

root@centos:/root>

通过查看相关书籍,对于redhat、centos系的Linux来说,ansible放在epel源中,因此先安装epel,操作如下:

root@centos:/root>yum -y install epel-release

Loaded plugins: fastestmirror, langpacks

Loading mirror speeds from cached hostfile

* base: mirrors.bupt.edu.cn

* extras: ftp.sjtu.edu.cn

* updates: ftp.sjtu.edu.cn

Resolving Dependencies

--> Running transaction check

---> Package epel-release.noarch 0:7-11 will be installed

--> Finished Dependency Resolution


Dependencies Resolved


====================================================================================================================================

Package Arch Version Repository Size

====================================================================================================================================

Installing:

epel-release noarch 7-11 extras 15 k


Transaction Summary

====================================================================================================================================

Install 1 Package


Total download size: 15 k

Installed size: 24 k

Downloading packages:

warning: /var/cache/yum/x86_64/7/extras/packages/epel-release-7-11.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY

Public key for epel-release-7-11.noarch.rpm is not installed

epel-release-7-11.noarch.rpm | 15 kB 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

Importing GPG key 0xF4A80EB5:

Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"

Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5

Package : centos-release-7-7.1908.0.el7.centos.x86_64 (@anaconda)

From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

Installing : epel-release-7-11.noarch 1/1
Verifying : epel-release-7-11.noarch 1/1

Installed:

epel-release.noarch 0:7-11


Complete!

root@centos:/root>

2、执行yum -y install ansible安装ansible:

root@centos:/root>yum -y install ansible      
Loaded plugins: fastestmirror, langpacks

Loading mirror speeds from cached hostfile

* base: mirrors.bupt.edu.cn

* epel: mirrors.tuna.tsinghua.edu.cn

* extras: ftp.sjtu.edu.cn

* updates: ftp.sjtu.edu.cn

Resolving Dependencies

--> Running transaction check

---> Package ansible.noarch 0:2.9.27-1.el7 will be installed

--> Processing Dependency: python-httplib2 for package: ansible-2.9.27-1.el7.noarch

--> Processing Dependency: python-jinja2 for package: ansible-2.9.27-1.el7.noarch

--> Processing Dependency: python-paramiko for package: ansible-2.9.27-1.el7.noarch

--> Processing Dependency: python2-cryptography for package: ansible-2.9.27-1.el7.noarch

--> Processing Dependency: python2-jmespath for package: ansible-2.9.27-1.el7.noarch

--> Processing Dependency: sshpass for package: ansible-2.9.27-1.el7.noarch

--> Running transaction check

---> Package python-jinja2.noarch 0:2.7.2-4.el7 will be installed

--> Processing Dependency: python-babel >= 0.8 for package: python-jinja2-2.7.2-4.el7.noarch

---> Package python-paramiko.noarch 0:2.1.1-9.el7 will be installed

--> Processing Dependency: python2-pyasn1 for package: python-paramiko-2.1.1-9.el7.noarch

---> Package python2-cryptography.x86_64 0:1.7.2-2.el7 will be installed

--> Processing Dependency: python-idna >= 2.0 for package: python2-cryptography-1.7.2-2.el7.x86_64

--> Processing Dependency: python-cffi >= 1.4.1 for package: python2-cryptography-1.7.2-2.el7.x86_64

--> Processing Dependency: python-enum34 for package: python2-cryptography-1.7.2-2.el7.x86_64

---> Package python2-httplib2.noarch 0:0.18.1-3.el7 will be installed

---> Package python2-jmespath.noarch 0:0.9.4-2.el7 will be installed

---> Package sshpass.x86_64 0:1.06-2.el7 will be installed

--> Running transaction check

---> Package python-babel.noarch 0:0.9.6-8.el7 will be installed

---> Package python-cffi.x86_64 0:1.6.0-5.el7 will be installed

--> Processing Dependency: python-pycparser for package: python-cffi-1.6.0-5.el7.x86_64

---> Package python-enum34.noarch 0:1.0.4-1.el7 will be installed

---> Package python-idna.noarch 0:2.4-1.el7 will be installed

---> Package python2-pyasn1.noarch 0:0.1.9-7.el7 will be installed

--> Running transaction check

---> Package python-pycparser.noarch 0:2.14-1.el7 will be installed

--> Processing Dependency: python-ply for package: python-pycparser-2.14-1.el7.noarch

--> Running transaction check

---> Package python-ply.noarch 0:3.4-11.el7 will be installed

--> Finished Dependency Resolution


Dependencies Resolved


====================================================================================================================================

Package Arch Version Repository Size

====================================================================================================================================

Installing:

ansible noarch 2.9.27-1.el7 epel 17 M

Installing for dependencies:

python-babel noarch 0.9.6-8.el7 base 1.4 M

python-cffi x86_64 1.6.0-5.el7 base 218 k

python-enum34 noarch 1.0.4-1.el7 base 52 k

python-idna noarch 2.4-1.el7 base 94 k

python-jinja2 noarch 2.7.2-4.el7 base 519 k

python-paramiko noarch 2.1.1-9.el7 base 269 k

python-ply noarch 3.4-11.el7 base 123 k

python-pycparser noarch 2.14-1.el7 base 104 k

python2-cryptography x86_64 1.7.2-2.el7 base 502 k

python2-httplib2 noarch 0.18.1-3.el7 epel 125 k

python2-jmespath noarch 0.9.4-2.el7 epel 41 k

python2-pyasn1 noarch 0.1.9-7.el7 base 100 k

sshpass x86_64 1.06-2.el7 extras 21 k


Transaction Summary

====================================================================================================================================

Install 1 Package (+13 Dependent packages)


Total download size: 20 M

Installed size: 119 M

Downloading packages:

(1/14): python-enum34-1.0.4-1.el7.noarch.rpm | 52 kB 00:00:00
(2/14): python-cffi-1.6.0-5.el7.x86_64.rpm | 218 kB 00:00:00
(3/14): python-idna-2.4-1.el7.noarch.rpm | 94 kB 00:00:00
(4/14): python-paramiko-2.1.1-9.el7.noarch.rpm | 269 kB 00:00:00
(5/14): python-babel-0.9.6-8.el7.noarch.rpm | 1.4 MB 00:00:01
(6/14): python-jinja2-2.7.2-4.el7.noarch.rpm | 519 kB 00:00:00
(7/14): python-ply-3.4-11.el7.noarch.rpm | 123 kB 00:00:00
(8/14): python-pycparser-2.14-1.el7.noarch.rpm | 104 kB 00:00:00
(9/14): python2-cryptography-1.7.2-2.el7.x86_64.rpm | 502 kB 00:00:01
warning: /var/cache/yum/x86_64/7/epel/packages/ansible-2.9.27-1.el7.noarch.rpm: Header V4 RSA/SHA256 Signature, key ID 352c64e5: NOKEY

Public key for ansible-2.9.27-1.el7.noarch.rpm is not installed

(10/14): ansible-2.9.27-1.el7.noarch.rpm | 17 MB 00:00:03
(11/14): python2-httplib2-0.18.1-3.el7.noarch.rpm | 125 kB 00:00:00
(12/14): python2-jmespath-0.9.4-2.el7.noarch.rpm | 41 kB 00:00:00
(13/14): python2-pyasn1-0.1.9-7.el7.noarch.rpm | 100 kB 00:00:00
(14/14): sshpass-1.06-2.el7.x86_64.rpm | 21 kB 00:00:00
------------------------------------------------------------------------------------------------------------------------------------

Total 4.7 MB/s | 20 MB 00:00:04
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

Importing GPG key 0x352C64E5:

Userid : "Fedora EPEL (7) <epel@fedoraproject.org>"

Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5

Package : epel-release-7-11.noarch (@extras)

From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

Installing : python2-pyasn1-0.1.9-7.el7.noarch 1/14
Installing : python-enum34-1.0.4-1.el7.noarch 2/14
Installing : sshpass-1.06-2.el7.x86_64 3/14
Installing : python2-httplib2-0.18.1-3.el7.noarch 4/14
Installing : python-babel-0.9.6-8.el7.noarch 5/14
Installing : python-jinja2-2.7.2-4.el7.noarch 6/14
Installing : python2-jmespath-0.9.4-2.el7.noarch 7/14
Installing : python-ply-3.4-11.el7.noarch 8/14
Installing : python-pycparser-2.14-1.el7.noarch 9/14
Installing : python-cffi-1.6.0-5.el7.x86_64 10/14
Installing : python-idna-2.4-1.el7.noarch 11/14
Installing : python2-cryptography-1.7.2-2.el7.x86_64 12/14
Installing : python-paramiko-2.1.1-9.el7.noarch 13/14
Installing : ansible-2.9.27-1.el7.noarch 14/14
Verifying : python-idna-2.4-1.el7.noarch 1/14
Verifying : python-pycparser-2.14-1.el7.noarch 2/14
Verifying : python-ply-3.4-11.el7.noarch 3/14
Verifying : ansible-2.9.27-1.el7.noarch 4/14
Verifying : python-paramiko-2.1.1-9.el7.noarch 5/14
Verifying : python2-jmespath-0.9.4-2.el7.noarch 6/14
Verifying : python-babel-0.9.6-8.el7.noarch 7/14
Verifying : python2-httplib2-0.18.1-3.el7.noarch 8/14
Verifying : python-cffi-1.6.0-5.el7.x86_64 9/14
Verifying : sshpass-1.06-2.el7.x86_64 10/14
Verifying : python-jinja2-2.7.2-4.el7.noarch 11/14
Verifying : python2-pyasn1-0.1.9-7.el7.noarch 12/14
Verifying : python-enum34-1.0.4-1.el7.noarch 13/14
Verifying : python2-cryptography-1.7.2-2.el7.x86_64 14/14

Installed:

ansible.noarch 0:2.9.27-1.el7

Dependency Installed:

python-babel.noarch 0:0.9.6-8.el7 python-cffi.x86_64 0:1.6.0-5.el7 python-enum34.noarch 0:1.0.4-1.el7
python-idna.noarch 0:2.4-1.el7 python-jinja2.noarch 0:2.7.2-4.el7 python-paramiko.noarch 0:2.1.1-9.el7
python-ply.noarch 0:3.4-11.el7 python-pycparser.noarch 0:2.14-1.el7 python2-cryptography.x86_64 0:1.7.2-2.el7

python2-httplib2.noarch 0:0.18.1-3.el7 python2-jmespath.noarch 0:0.9.4-2.el7 python2-pyasn1.noarch 0:0.1.9-7.el7

sshpass.x86_64 0:1.06-2.el7

Complete!

root@centos:/root>

到此,使用yum部署ansible就完成了。

二、使用rpm部署ansible:

1、准备好ansible的rpm包以及相关依赖包,执行rpm -ivh *rpm 安装即可:

[root@centos test]# rpm -ivh --nodeps --force *rpm

warning: ansible-2.9.9-1.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY

warning: libyaml-0.1.4-11.el7_0.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY

Preparing... ################################# [100%]

Updating / installing...

1:python-six-1.9.0-2.el7 ################################# [ 5%]

2:python-ipaddress-1.0.16-2.el7 ################################# [ 9%]

3:python2-pyasn1-0.1.9-7.el7 ################################# [ 14%]

4:sshpass-1.06-2.el7 ################################# [ 18%]

5:python-ply-3.4-11.el7 ################################# [ 23%]

6:python-pycparser-2.14-1.el7 ################################# [ 27%]

7:python-cffi-1.6.0-5.el7 ################################# [ 32%]

8:python-markupsafe-0.11-10.el7 ################################# [ 36%]

9:python-idna-2.4-1.el7 ################################# [ 41%]

10:python-httplib2-0.9.2-1.el7 ################################# [ 45%]

11:python-enum34-1.0.4-1.el7 ################################# [ 50%]

12:python-backports-1.0-8.el7 ################################# [ 55%]

13:python-backports-ssl_match_hostna################################# [ 59%]

14:python-setuptools-0.9.8-7.el7 ################################# [ 64%]

15:python2-cryptography-1.7.2-2.el7 ################################# [ 68%]

16:python-paramiko-2.1.1-9.el7 ################################# [ 73%]

17:python-babel-0.9.6-8.el7 ################################# [ 77%]

18:python-jinja2-2.7.2-4.el7 ################################# [ 82%]

19:python2-jmespath-0.9.4-2.el7 ################################# [ 86%]

20:libyaml-0.1.4-11.el7_0 ################################# [ 91%]

21:PyYAML-3.10-11.el7 ################################# [ 95%]

22:ansible-2.9.9-1.el7 ################################# [100%]

[root@centos test]#

 后续就可以进行ansible配置的修改、生成密钥以及向客户端分发密钥等操作了,ansible的配置路径如下:

root@centos:/root>cd /etc/ansible/

root@centos:/etc/ansible>ls -ltr

total 24

drwxr-xr-x 2 root root 6 Jan 16 2022 roles

-rw-r--r-- 1 root root 1016 Jan 16 2022 hosts

-rw-r--r-- 1 root root 19985 Jan 16 2022 ansible.cfg

root@centos:/etc/ansible>