2019-11-26 00:52:51 +08:00
[#]: collector: (lujun9972)
2019-11-27 09:03:11 +08:00
[#]: translator: (geekpi)
2019-11-26 00:52:51 +08:00
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to Install Ansible (Automation Tool) on CentOS 8/RHEL 8)
[#]: via: (https://www.linuxtechi.com/install-ansible-centos-8-rhel-8/)
[#]: author: (Pradeep Kumar https://www.linuxtechi.com/author/pradeep/)
2019-11-28 08:55:55 +08:00
如何在 CentOS 8/RHEL 8 上安装 Ansible( 自动化工具)
2019-11-26 00:52:51 +08:00
======
2019-11-28 08:55:55 +08:00
**Ansible** 是给 Linux 系统管理员使用的出色自动化工具。它是一种开源配置工具,能让系统管理员可以从一个中心节点(即 **Ansible 服务器** )管理数百台服务器。将 Ansible 与 **Puppet** 、**Chef** 和 **Salt**等类似工具进行比较时,它是首选的配置工具,因为它不需要任何代理,并且可以工作在 SSH 和 python 上。
2019-11-26 00:52:51 +08:00
[![Install-Ansible-CentOS8-RHEL8][1]][2]
2019-11-28 08:55:55 +08:00
在本教程中,我们将学习如何在 CentOS 8 和 RHEL 8 系统上安装和使用 Ansble
2019-11-26 00:52:51 +08:00
2019-11-28 08:55:55 +08:00
Ansible 实验环境信息:
2019-11-26 00:52:51 +08:00
2019-11-28 08:55:55 +08:00
* Minimal CentOS 8 / RHEL 8 服务器( 192.168.1.10),且有互联网连接
* 两个 Ansible 节点 - Ubuntu 18.04 LTS ( 192.168.1.20) 和 CentOS 7 ( 192.168.1.30)
2019-11-26 00:52:51 +08:00
2019-11-28 08:55:55 +08:00
### CentOS 8 上的 Ansible 安装步骤
2019-11-26 00:52:51 +08:00
2019-11-28 08:55:55 +08:00
Ansible 包不在 CentOS 8 默认的软件包仓库中。因此,我们需要执行以下命令启用 [EPEL 仓库][3],
2019-11-26 00:52:51 +08:00
```
[root@linuxtechi ~]$ sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
```
2019-11-28 08:55:55 +08:00
启用 epel 仓库后,执行以下 dnf 命令安装 Ansble。
2019-11-26 00:52:51 +08:00
```
[root@linuxtechi ~]$ sudo dnf install ansible
```
2019-11-28 08:55:55 +08:00
上面命令的输出:
2019-11-26 00:52:51 +08:00
![dnf-install-ansible-centos8][1]
2019-11-28 08:55:55 +08:00
成功安装 ansible 后,运行以下命令验证它的版本。
2019-11-26 00:52:51 +08:00
```
[root@linuxtechi ~]$ sudo ansible --version
```
![Ansible-version-CentOS8][1]
2019-11-28 08:55:55 +08:00
上面的输出确认在 CentOS 8 上安装完成。
2019-11-26 00:52:51 +08:00
2019-11-28 08:55:55 +08:00
让我们看下 RHEL 8 系统。
2019-11-26 00:52:51 +08:00
2019-11-28 08:55:55 +08:00
### RHEL 8 上的 Ansible 安装步骤
2019-11-26 00:52:51 +08:00
2019-11-28 08:55:55 +08:00
如果你有有效的 RHEL 8 订阅,请使用以下订阅管理器命令启用 Ansble 仓库,
2019-11-26 00:52:51 +08:00
```
[root@linuxtechi ~]$ sudo subscription-manager repos --enable ansible-2.8-for-rhel-8-x86_64-rpms
```
2019-11-28 08:55:55 +08:00
启用仓库后,执行以下 dnf 命令安装 Ansible,
2019-11-26 00:52:51 +08:00
```
[root@linuxtechi ~]$ sudo dnf install ansible -y
```
2019-11-28 08:55:55 +08:00
安装 ansible 及其依赖包后,执行以下命令来验证它的版本,
2019-11-26 00:52:51 +08:00
```
[root@linuxtechi ~]$ sudo ansible --version
```
2019-11-28 08:55:55 +08:00
### 在 CentOS 8 / RHEL 8 上通过 pip3 安装 Ansible 的可选方法
2019-11-26 00:52:51 +08:00
2019-11-28 08:55:55 +08:00
如果你希望使用 **pip** ( python 的包管理器)安装 Ansible, 请首先使用以下命令安装 pyhton3 和 python3-pip 包,
2019-11-26 00:52:51 +08:00
```
[root@linuxtechi ~]$ sudo dnf install python3 python3-pip -y
```
2019-11-28 08:55:55 +08:00
安装 python3 后,运行以下命令来验证它的版本。
2019-11-26 00:52:51 +08:00
```
[root@linuxtechi ~]$ python3 -V
Python 3.6.8
[root@linuxtechi ~]$
```
2019-11-28 08:55:55 +08:00
命令下面的 pip3 命令安装 Ansible,
2019-11-26 00:52:51 +08:00
```
[root@linuxtechi ~]$ pip3 install ansible --user
```
2019-11-28 08:55:55 +08:00
输出,
2019-11-26 00:52:51 +08:00
![Ansible-Install-pip3-centos8][1]
2019-11-28 08:55:55 +08:00
上面的输出确认 Ansible 已成功使用 pip3 安装。让我们看下如何使用 Ansible。
2019-11-26 00:52:51 +08:00
2019-11-28 08:55:55 +08:00
### 如何使用 Ansible 自动化工具?
2019-11-26 00:52:51 +08:00
2019-11-28 08:55:55 +08:00
当我们使用 yum 或 dnf 命令安装 Ansible 时,它的配置文件、清单文件和角色目录会自动在 /etc/ansible 文件夹下创建。
2019-11-26 00:52:51 +08:00
2019-11-28 08:55:55 +08:00
让我们添加一个名称为 “**labservers**” 的组,并在 ** /etc/ansible/hosts** 文件中给该组添加 Ubuntu 18.04 和 CentOS 7 的系统 IP 地址。
2019-11-26 00:52:51 +08:00
```
[root@linuxtechi ~]$ sudo vi /etc/ansible/hosts
…
[labservers]
192.168.1.20
192.168.1.30
…
```
2019-11-28 08:55:55 +08:00
保存并退出文件。
2019-11-26 00:52:51 +08:00
Once the inventory file (/etc/ansible/hosts) is updated then exchange your user’ s ssh public keys with remote systems which are part of “labservers” group.
2019-11-28 08:55:55 +08:00
更新清单文件(/etc/ansible/hosts) 后, 将用户的 ssh 公钥与作为 “”组一部分的远程系统交换。
2019-11-26 00:52:51 +08:00
Let’ s first generate your local user’ s public and private key using ssh-keygen command,
```
[root@linuxtechi ~]$ ssh-keygen
```
Now exchange public key between the ansible server and its clients using the following command,
```
[root@linuxtechi ~]$ ssh-copy-id root@linuxtechi
[root@linuxtechi ~]$ ssh-copy-id root@linuxtechi
```
Now let’ s try couple of Ansible commands, first verify the connectivity from Ansible server to its clients using ping module,
```
[root@linuxtechi ~]$ ansible -m ping "labservers"
```
**Note:** If we don’ t specify the inventory file in above command then it will refer the default hosts file (i.e /etc/ansible/hosts)
Output,
![ansible-ping-module-centos8][1]
Let’ s check kernel version of each client using Ansible shell command,
```
[root@linuxtechi ~]$ ansible -m command -a "uname -r" "labservers"
192.168.1.30 | CHANGED | rc=0 >>
4.15.0-20-generic
192.168.1.20 | CHANGED | rc=0 >>
3.10.0-327.el7.x86_64
[root@linuxtechi ~]$
```
Use the following ansible command to list all hosts from the inventory file,
```
[root@linuxtechi ~]$ ansible all -i /etc/ansible/hosts --list-hosts
hosts (4):
192.168.100.1
192.168.100.10
192.168.1.20
192.168.1.30
[root@linuxtechi ~]$
```
Use the following ansible command to list only hosts from “labservers” group
```
root@linuxtechi ~]$ ansible labservers -i /etc/ansible/hosts --list-hosts
hosts (2):
192.168.1.20
192.168.1.30
[root@linuxtechi ~]$
```
That’ s all from this article, we have successfully demonstrated on how to install and use Ansible on CentOS 8 and RHEL 8 System. Please do you share your feedback and comments.
* [Facebook][4]
* [Twitter][5]
* [LinkedIn][6]
* [Reddit][7]
--------------------------------------------------------------------------------
via: https://www.linuxtechi.com/install-ansible-centos-8-rhel-8/
作者:[Pradeep Kumar][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT ](https://github.com/LCTT/TranslateProject ) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linuxtechi.com/author/pradeep/
[b]: https://github.com/lujun9972
[1]: data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
[2]: http://www.linuxtechi.com/wp-content/uploads/2019/11/Install-Ansible-CentOS8-RHEL8.png
[3]: http://www.linuxtechi.com/enable-epel-repo-centos8-rhel8-server/
[4]: http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.linuxtechi.com%2Finstall-ansible-centos-8-rhel-8%2F& t=How%20to%20Install%20Ansible%20%28Automation%20Tool%29%20on%20CentOS%208%2FRHEL%208
[5]: http://twitter.com/share?text=How%20to%20Install%20Ansible%20%28Automation%20Tool%29%20on%20CentOS%208%2FRHEL%208& url=https%3A%2F%2Fwww.linuxtechi.com%2Finstall-ansible-centos-8-rhel-8%2F& via=Linuxtechi
[6]: http://www.linkedin.com/shareArticle?mini=true& url=https%3A%2F%2Fwww.linuxtechi.com%2Finstall-ansible-centos-8-rhel-8%2F& title=How%20to%20Install%20Ansible%20%28Automation%20Tool%29%20on%20CentOS%208%2FRHEL%208
[7]: http://www.reddit.com/submit?url=https%3A%2F%2Fwww.linuxtechi.com%2Finstall-ansible-centos-8-rhel-8%2F& title=How%20to%20Install%20Ansible%20%28Automation%20Tool%29%20on%20CentOS%208%2FRHEL%208