如何在 Redhat Enterprise Linux 8 上安装和配置 Ansible
本教程介绍了在 Redhat Enterprise Linux 8 上逐步安装和配置 Ansible。 Ansible 是领先的开源配置管理系统。它使管理员和运营团队可以轻松地从中央计算机控制数千台服务器,而无需在其上安装代理。
在本教程中您将学习:
Ansible 概述
安装和配置Python
设置无密码 SSH
安装 Ansible
测试和管理 Ansible
Ansible 架构。
Ansible 概述
Ansible 是一个极其简单的 IT 自动化引擎,可自动执行云配置、配置管理、应用程序部署、服务内编排和许多其他 IT 需求。
Ansible 从第一天起就专为多层部署而设计,它通过描述所有系统如何相互关联来对您的 IT 基础设施进行建模,而不是一次只管理一个系统。
它不使用代理,也不使用额外的自定义安全基础设施,因此很容易部署 - 最重要的是,它使用一种非常简单的语言(YAML,以 Ansible Playbook 的形式),允许您以一种接近的方式描述您的自动化作业简单的英语。 Ansible 的工作原理是连接到您的节点并向其推送称为“Ansible 模块”的小程序。 这些程序被编写为系统所需状态的资源模型。然后 Ansible 执行这些模块(默认通过 SSH),并在完成后删除它们。
您的模块库可以驻留在任何计算机上,并且不需要服务器、守护程序或数据库。通常,您将使用您最喜欢的终端程序、文本编辑器以及可能的版本控制系统来跟踪内容的更改。
安装和配置Python
RHEL 8 中的默认 Python 版本是 Python 3.6。但 Python 2 在 RHEL 8 中仍然可用。如果由于任何原因在 Red Hat Enterprise Linux 8 安装中缺少 Python 3.6,您需要手动安装它。
通过在终端上运行以下命令,可以在 RHEL 8 上安装 Python 3.6。 Python 需要安装在 Ansible 服务器及其将连接的所有主机/客户端上。
# yum install python3
Updating Subscription Management repositories.
Updating Subscription Management repositories.
Last metadata expiration check: 8:59:59 ago on Sun 03 Feb 2019 11:20:51 PM +04.
Dependencies resolved.
======================================================================================================================================================
Package Arch Version Repository Size
======================================================================================================================================================
Installing:
python36 x86_64 3.6.6-17.el8+2102+a4bbd900 rhel-8-for-x86_64-appstream-beta-rpms 22 k
Enabling module streams:
python36 3.6
Transaction Summary
======================================================================================================================================================
Install 1 Package
Total download size: 22 k
Installed size: 22 k
Is this ok [y/N]: y
Downloading Packages:
python36-3.6.6-17.el8+2102+a4bbd900.x86_64.rpm 6.0 kB/s | 22 kB 00:03
------------------------------------------------------------------------------------------------------------------------------------------------------
Total 6.0 kB/s | 22 kB 00:03
warning: /var/cache/dnf/rhel-8-for-x86_64-appstream-beta-rpms-64aba9de5ea7e089/packages/python36-3.6.6-17.el8+2102+a4bbd900.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f21541eb: NOKEY
Red Hat Enterprise Linux 8 for x86_64 - AppStream Beta (RPMs) 3.2 kB/s | 3.3 kB 00:01
Importing GPG key 0xF21541EB:
Userid : "Red Hat, Inc. (beta key 2) <security@redhat.com>"
Fingerprint: B08B 659E E86A F623 BC90 E8DB 938A 80CA F215 41EB
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-beta
Is this ok [y/N]: y
...
Installed:
python36-3.6.6-17.el8+2102+a4bbd900.x86_64
Complete!
要使用 Python 3,只需在终端中输入 python3
即可。
您应该已经注意到,要使用 Python 3,Python 2 的命令是 python3 和 python2。如果您的应用程序配置为引用在系统范围内不可用的 python,该怎么办?您将收到以下 bash 错误。
# python
-bash: python: command not found
您可以使用替代机制在系统范围内启用无版本的 python 命令,并将其设置为特定版本。要将 Python 3 设置为默认值,请运行以下命令。
# alternatives --set python /usr/bin/python3
运行 python -V 应显示配置的默认 Python 版本:
#python -V
Python 3.6.6
或者通过启动 Python 控制台:
# python
Python 3.6.6 (default, Oct 16 2018, 01:53:53)
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
设置无密码 SSH
在所有主机上创建用户 ansible
:
# useradd ansible ; echo "" | passwd --stdin ansible
在 sudoers 文件 /etc/sudoers
中为 ansible 用户进行无密码 sudo 访问所需的条目:
ansible ALL=(ALL) NOPASSWD: ALL
现在在 Ansible 服务器中生成 SSH 密钥:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ansible/.ssh/id_rsa):
Created directory '/home/ansible/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ansible/.ssh/id_rsa.
Your public key has been saved in /home/ansible/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:wNt/uNePRkrV+Hhv/DJgteXK2BjOGvPbr9yNigXM2EM ansible@rhel8-ansible-server
The key's randomart image is:
+---[RSA 2048]----+
| |
| . |
| o E o |
| + * + o|
| . S * o * |
| . += + +|
| ==o@ +.|
| OBoO+=|
| +o++*BB|
+----[SHA256]-----+
以 ansible 用户身份将其复制到远程服务器:
$ ssh-copy-id ansible@rhel8-ansible-client
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/ansible/.ssh/id_rsa.pub"
The authenticity of host 'rhel8-ansible-client (192.168.1.109)' can't be established.
ECDSA key fingerprint is SHA256:e+NfCeK/kvnignWDHgFvIkHjBWwghIIjJkfjygR7NkI.
Are you sure you want to continue connecting (yes/no)? 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
ansible@rhel8-ansible-client's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'ansible@rhel8-ansible-client'"
and check to make sure that only the key(s) you wanted were added.
安装 Ansible
安装 Python 后,继续安装 Pip,它是一个 Python 包管理器,我们将用它来安装 Ansible:
# yum -y install python3-pip
Updating Subscription Management repositories.
Updating Subscription Management repositories.
Last metadata expiration check: 9:03:18 ago on Sun 03 Feb 2019 11:20:51 PM +04.
Package python3-pip-9.0.3-4.el8.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!
安装 pip3
后,使用它以 ansible 用户身份将 Ansible 安装在 Ansible 管理系统中。
$ pip3 install ansible --user
您可以使用以下命令查看已安装的 Ansible:
$ ansible --version
ansible 2.7.6
config file = None
configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/ansible/.local/lib/python3.6/site-packages/ansible
executable location = /home/ansible/.local/bin/ansible
python version = 3.6.6 (default, Oct 16 2018, 01:53:53) [GCC 8.2.1 20180905 (Red Hat 8.2.1-3)]
测试和管理
创建Ansible清单文件,默认为/etc/ansible/hosts
,但您可以将hosts文件复制到ansible用户的主目录中。您还可以在主机文件中创建一组远程主机,如下所示。
[web]
192.168.1.105
[db]
192.168.1.107
[app]
192.168.1.108
192.168.1.109
192.168.1.110
您可以使用 ping 模块来测试 Ansible,成功运行后您可以看到以下输出。
$ ansible -i hosts 192.168.1.109 -m ping
192.168.1.109 | SUCCESS => {
"changed": false,
"ping": "pong"
}
-i 选项用于提供清单文件的路径。对于“app”组名称,您应该获得相同的输出。
$ ansible -i hosts app -m ping
192.168.1.108 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.1.109 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.1.110 | SUCCESS => {
"changed": false,
"ping": "pong"
}
结论
Ansible 的简单性和降低其他工具复杂性的能力使其成为您环境的可靠应用程序。它最关心的是安全性和可靠性。它使用 OpenSSH 进行传输,并且该语言是围绕可审核性而设计的,即使是那些不熟悉该程序的人也可以进行审核。 Ansible 足以管理具有少量实例的小型设置以及企业环境。