在 CentOS 8 上安装 EPEL Yum 软件库

EPEL (Extra Packages for Enterprise Linux) 是一个由 Fedora 团队提供的免费社区支持的 yum 存储库。
EPEL 提供标准 yum 存储库中没有的最新版本的 Linux 软件。

我们可以通过安装 epel-release 包在 CentOS 8 上启用 EPEL yum 存储库。

[root@ansible-01 ~]# dnf install -y epel-release

为 EPEL yum 存储库构建缓存。

[root@ansible-01 ~]# dnf makecache
在 CentOS 8 上安装 Ansible 控制节点
on  it road.com

启用 Ansible 命令的 Bash 自动完成功能

从 Ansible 2.9 开始,Ansible 命令的 shell 完成是可用的,并通过称为 argcomplete 的可选依赖项提供。
argcomplete 支持 bash 但对 zsh 和 tcsh 的支持有限。

我们正在安装 python3-argcomplete 包以启用 Ansible 命令的 bash 完成。

[root@ansible-01 ~]# dnf install -y python3-argcomplete

配置 argcomplete 以启用 Ansible 命令的 bash 完成。

[root@ansible-01 ~]# activate-global-python-argcomplete
activate-global-python-argcomplete
Installing bash completion script /etc/bash_completion.d/python-argcomplete.sh
printf "3]0;%s@%s:%s
[root@ansible-01 ~]# dnf info ansible --repo=epel
7" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"

在 CentOS 8 上安装 Ansible

我们可以通过以下三种方法之一在 CentOS 8 上安装 Ansible。

  • 从 EPEL yum 存储库安装最新版本的 Ansible
  • 使用 pip(Python 包管理器)安装 Ansible
  • 通过编译 GitHub 上提供的 Ansible 源代码进行安装

在这里,我们使用 EPEL yum 存储库安装 Ansible。

首先,检查 EPEL 存储库中 Ansible 的可用版本。

[root@ansible-01 ~]# dnf install -y ansible

使用 dnf 命令安装 Ansible。

[root@ansible-01 ~]# ansible --version

检查其版本来验证 Ansible 是否安装成功。

[root@ansible-01 ~]# dnf module install -y python36

在 CentOS 8 上安装 Python

Ansible 需要 Python 2(版本 2.7)或者 Python 3(版本 3.5 或者更高版本)。

在 CentOS 8 上安装 Python 3.6:

[root@ansible-01 ~]# python3 --version
Python 3.6.8

检查 Python 的版本以验证它是否已正确安装。

[root@ansible-01 ~]# echo "192.168.116.206 apache-01 apache-01.onitroad.com" >> /etc/hosts

添加一个基于 CentOS 8 的 Ansible 受管节点:

如果我们要管理一个 CentOS 8 节点,那么我们已经对其进行了一些必要的配置。

如果网络没有使用 DNS 服务器,则必须使用本地 DNS 解析器配置托管节点的名称解析。

在 Ansible 控制节点的 hosts 文件中添加名称解析项。

[root@ansible-01 ~]# ssh-copy-id root@apache-01.onitroad.com
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'apache-01.onitroad.com (192.168.116.206)' can't be established.
ECDSA key fingerprint is SHA256:skGj4xg0w+jIQtrfF8AOdfItgcXUQQu+bWUFfvws1Hk.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@apache-01.onitroad.com's password:
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh 'root@apache-01.onitroad.com'"
and check to make sure that only the key(s) you wanted were added.

设置 Ansible 受管节点只需要两个软件。

  • 无需密码认证的 sshd
  • Python

在基于 CentOS 8 的系统中,默认安装 sshd。
所以,我们只需要在其上配置无密码认证即可。

这很容易,因为我们已经生成了 SSH 密钥对。
我们现在使用 ssh-copy-id 命令将公钥复制到托管节点。

[root@apache-01 ~]# dnf module install -y python36

这就是 Ansible 控制节点配置的全部内容。
现在登录到 Ansible 托管节点 (apache-01.onitroad.com) 并在其上安装 Python 3.6.

[root@apache-01 ~]# python3 --version
Python 3.6.8

通过检查其版本来验证 Python 安装。

[root@ansible-01 ~]# mkdir ansible
[root@ansible-01 ~]# cd ansible

apache-01.onitroad.com 节点已准备好由 Ansible 管理。
现在登录到 Ansible 控制节点并在 Ansible Inventory 中添加 apache-01.onitroad.com 节点。

创建一个目录来存储 Ansible 列表文件。

[root@ansible-01 ansible]# vi hosts

创建 hosts 文件以定义 Ansible 受管节点。

[webservers]
apache-01.onitroad.com

添加一个部分 [webservers] 并其中添加 apache-01.onitroad.com 节点。

[root@ansible-01 ansible]# ansible -i hosts --list-hosts all
  hosts (1):
    apache-01.onitroad.com

这些配置对于执行 Ansible adhoc 命令足够了。
但是,我们也可以编写 Ansible playbook 来编排托管节点。

从列表中列出 Ansible 托管主机。

[root@ansible-01 ansible]# ansible -i hosts -m ping all
apache-01.onitroad.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

通过 ping Ansible 受管节点来检查连接。

[root@ansible-01 ansible]# ansible -i hosts -m shell -a 'dnf install -y httpd' webservers

通过使用 Ansible 对托管主机执行命令。
我们正在网络服务器部分的所有节点上安装 Apache HTTP 服务器。

[root@ansible-01 ~]# ssh-keygen

Ansible 已安装在 CentOS 8 控制节点上,同时我们添加了一个基于 CentOS 8 的托管节点。

为 Ansible 控制节点生成 SSH 密钥对:

我们正在为 Ansible 控制节点生成一个 SSH 密钥对。
此 SSH 密钥对将用于在 Ansible 受管节点上设置无密码身份验证。

##代码##
日期:2020-09-17 00:16:43 来源:oir作者:oir