How to install and use Ansible on CentOS 8 and RHEL 8 system

Ansible is to give  Linux  an excellent automation tool used by system administrators. It is an open source configuration tool that allows system administrators to manage hundreds of servers from a central node (ie Ansible server). When Ansible similar tool Puppet, Chef, and Salt were compared, it is the preferred configuration tool, since it does not require any agents, and can work on SSH and python.

In this tutorial, we will learn how to  CentOS  install and use Ansble 8 and RHEL 8 system.

Ansible experimental environment information:

  • The minimal setup CentOS 8 / RHEL 8 Server (192.168.1.10), and an Internet connection

  • Ansible two nodes - Ubuntu 18.04 LTS (192.168.1.20) and CentOS 7 (192.168.1.30)

CentOS Ansible mounted on the step 8

Ansible package is not CentOS 8 default package repositories. Therefore, we need to execute the following command to enable EPEL Warehouse:

[root@linuxtechi ~]$ sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y

When enabled epel warehouse, dnf execute the following  command to install Ansible:

[root@linuxtechi ~]$ sudo dnf install ansible

The output of the above command:

How to install and use Ansible CentOS 8 on how to install and use, and systems RHEL 8 Ansible on CentOS 8 system and RHEL 8

dnf-install-ansible-centos8

After a successful installation Ansible, run the following command to verify its version:

[root@linuxtechi ~]$ sudo ansible --version

How to install and use Ansible CentOS 8 on how to install and use, and systems RHEL 8 Ansible on CentOS 8 system and RHEL 8

Ansible-version-CentOS8

上面的输出确认在 CentOS 8 上安装完成。

让我们看下 RHEL 8 系统。

RHEL 8 上的 Ansible 安装步骤

如果你有有效的 RHEL 8 订阅,请使用以下订阅管理器命令启用 Ansble 仓库:

[root@linuxtechi ~]$ sudo subscription-manager repos --enable ansible-2.8-for-rhel-8-x86_64-rpms

启用仓库后,执行以下 dnf 命令安装 Ansible:

[root@linuxtechi ~]$ sudo dnf install ansible -y

安装 Ansible 及其依赖包后,执行以下命令来验证它的版本:

[root@linuxtechi ~]$ sudo ansible --version

在 CentOS 8 / RHEL 8 上通过 pip3 安装 Ansible 的可选方法

如果你希望使用 pip(Python 的包管理器)安装 Ansible,请首先使用以下命令安装 pyhton3 和 python3-pip 包:

[root@linuxtechi ~]$ sudo dnf install python3 python3-pip -y

安装 python3 后,运行以下命令来验证它的版本:

[root@linuxtechi ~]$ python3 -V
Python 3.6.8
[root@linuxtechi ~]$

用下面的 pip3 命令安装 Ansible:

[root@linuxtechi ~]$ pip3 install ansible --user

输出:

How to install and use Ansible CentOS 8 on how to install and use, and systems RHEL 8 Ansible on CentOS 8 system and RHEL 8

Ansible-Install-pip3-centos8

上面的输出确认 Ansible 已成功使用 pip3 安装。让我们看下如何使用 Ansible。

如何使用 Ansible 自动化工具?

当我们使用 yum 或 dnf 命令安装 Ansible 时,它的配置文件、清单文件和角色目录会自动在 /etc/ansible 文件夹下创建。

让我们添加一个名称为 labservers 的组,并在 /etc/ansible/hosts 文件中给该组添加上述的 Ubuntu 18.04 和 CentOS 7 系统的 IP 地址:

[root@linuxtechi ~]$ sudo vi /etc/ansible/hosts
[labservers]
192.168.1.20
192.168.1.30

保存并退出文件。

更新清单文件(/etc/ansible/hosts)后,将用户的 ssh 公钥放到属于 labservers 组的远程系统。

让我们首先使用 ssh-keygen 命令生成本地用户的公钥和私钥:

[root@linuxtechi ~]$ ssh-keygen

现在使用以下命令在 Ansible 服务器及其客户端之间交换公钥:

[root@linuxtechi ~]$ ssh-copy-id root@linuxtechi
[root@linuxtechi ~]$ ssh-copy-id root@linuxtechi

现在,让我们尝试几个 Ansible 命令,首先使用 ping 模块验证 Ansible 服务器与客户端的连接:

[root@linuxtechi ~]$ ansible -m ping "labservers"

注意: 如果我们没有在上面的命令中指定清单文件,那么它将引用默认主机文件(即 /etc/ansible/hosts)。

输出:

How to install and use Ansible CentOS 8 on how to install and use, and systems RHEL 8 Ansible on CentOS 8 system and RHEL 8

ansible-ping-module-centos8

让我们使用 Ansible shell 命令检查每个客户端的内核版本:

[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 command to list all the hosts in the manifest 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 command Ansible host labservers group lists only:

root@linuxtechi ~]$ ansible labservers -i /etc/ansible/hosts --list-hosts
hosts (2):
192.168.1.20
192.168.1.30
[root@linuxtechi ~]$

This article is about that, we have successfully demonstrated how to install and use Ansible in CentOS 8 and RHEL 8 system. Please share your feedback and comments.


Guess you like

Origin blog.51cto.com/14414295/2466024