如何在 Ubuntu 上安装和配置 Ansible?

在 Ubuntu 上利用 Ansible 提升环境配置与管理

配置管理是 DevOps 流程中至关重要的环节,它助力 IT 基础设施的自动化和编排。

市面上有诸多配置管理工具可供选择,如 Puppet、Ansible、Chef 和 SaltStack。 其中,Ansible 无疑是 DevOps 领域最受欢迎的工具之一。 它能高效地管理成千上万的服务器以及您的整个 IT 基础设施。

本文将探讨以下内容:

  • Ansible 的安装步骤
  • SSH 密钥交换流程
  • Ansible 客户端的配置
  • Ansible 的连通性测试

Ansible 安装

为了便于理解,我们将以两台服务器为例进行 Ansible 的操作演示。一台作为 ansible-server,另一台作为 ansible-client,它们的 IP 地址分别为:

  • ansible 服务器 – 10.0.0.1
  • ansible 客户端 – 10.0.0.25

安装过程相对直接,需要在所有将要使用 Ansible 的服务器上执行以下操作。在此例中,需要在两台服务器上都进行安装。

  • 首先,运行以下命令,安装 Ansible 所需的预备软件:
[email protected]:~# apt install software-properties-common
  • 接下来,通过添加 Ansible 的软件包仓库来安装:
[email protected]:~# apt-add-repository --yes --update ppa:ansible/ansible
  • 更新 APT (Advanced Packaging Tool) 软件包列表:
[email protected]:~# apt update
  • 最后,执行以下命令完成 Ansible 的安装:
[email protected]:~# apt install ansible

安装必要的软件包只需几秒钟即可完成。

如何验证 Ansible 是否成功安装及其版本呢?

很简单,通过 `ansible –version` 命令即可查询,如下所示:

[email protected]:~# ansible --version
ansible 2.8.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.15+ (default, Nov 27 2018, 23:36:35) [GCC 7.3.0]
[email protected]:~#

如您所见,Ansible 2.8.1 已成功安装,并且输出了诸如配置文件位置和 Python 模块等关键信息。

下一步,我们需要进行 SSH 密钥交换,以确保服务器与客户端之间能够安全通信。

SSH 密钥交换

Ansible 通过 SSH(安全外壳)协议与客户端建立连接。

首先,在 ansible-server 上生成一个公钥,然后将其复制到 ansible-client 上。

请确保您以 root 用户身份登录。

  • 使用 ssh-keygen 命令生成密钥对,如下所示:
[email protected]:~# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:cDapZBESo+8XcbXupbtILkFrklUSpwa70Y1c7yH5K1A [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|    =.+oo .      |
|   . B.B.= .     |
|  . o @oE +      |
|   . *oO * .     |
|    o++.S + .    |
|   .o +o . +     |
|    .o..o +      |
|     ..o o .     |
|       .o o.     |
+----[SHA256]-----+
[email protected]:~#

可以看到,该命令在 .ssh 文件夹中生成了公钥。完整路径是 /root/.ssh/id_rsa.pub。

注意:请务必确保私钥和公钥文件不可被其他用户读取。您可以使用以下命令列出文件以验证其权限:

cd /root/.ssh
[email protected]:~# ls -l 
-rw------- 1 root root 1679 Jun 19 00:37 id_rsa 
-rw------- 1 root root 404 Jun 19 00:37 id_rsa.pub

如果发现权限不正确,可以使用 chmod 命令修改,例如:

chmod 400 id_rsa
chmod 400 id_rsa.pub

现在,我们将公钥复制到 IP 地址为 10.0.0.25 的 Ansible 客户端:

[email protected]:~/.ssh# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.0.0.25 (10.0.0.25)' can't be established.
ECDSA key fingerprint is SHA256:eXduPrfV0mhxUcpsZWg+0oXDim7bHb90caA/Rt79cIs.
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
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

[email protected]:~/.ssh#

从输出中可以看出,一个密钥已成功添加,表明 SSH 密钥交换已完成。

接下来,我们将配置 Ansible 客户端。

Ansible 客户端配置

假设您已按照前面的步骤在客户端服务器上完成了 Ansible 的安装。

配置客户端或主机其实就是让 Ansible 服务器知道客户端的存在。具体步骤如下:

  • 登录到 Ansible 服务器
  • 进入 /etc/ansible 目录
  • 使用您喜欢的文本编辑器,在 hosts 文件中添加如下内容:
[Client] 
node1 ansible_ssh_host=10.0.0.25

Ansible 测试

如果您正确执行了所有步骤,那么在 ansible-server 上运行以下命令时,您将收到一条 SUCCESS 消息:

[email protected]:~/.ssh# ansible -m ping Client
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
[email protected]:~/.ssh#

上述命令向客户端发送一个 ping 请求,以测试连接并确认状态良好。

总结

希望本文能为您提供 Ansible 安装和实践的初步指导。敬请关注更多 Ansible 教程,或者您可以查看 Udemy 上的 Ansible 进阶课程。