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

在 Ubuntu 上开始使用 Ansible 以获得更好的环境配置和配置管理。

配置管理是 DevOps 生命周期中的关键阶段。 它有助于 IT 基础架构的自动化和编排。

有多种配置管理工具,例如 Puppet、Ansible、Chef 和 SaltStack。 当然,Ansible 是 DevOps 中最受欢迎的工具之一。 它可以轻松管理数千台服务器和您的完整 IT 基础架构。

我们将在本文中介绍以下内容。

  • Ansible 安装
  • SSH 密钥交换
  • Ansible 客户端设置
  • Ansible 测试

Ansible 安装

为简单起见,让我们尝试在两台服务器上使用 Ansible。 一个是 ansible-server,另一个是具有以下 IP 的 ansible-client。

  • 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)
[email protected]:~# apt update
  • 最后——运行下面的命令进行安装
[email protected]:~# apt install ansible

安装必要的软件包需要几秒钟。

你如何确保它的安装和它的版本?

嗯,这很容易。 你可以使用 –version 语法和 ansible 来找出如下。

[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 模块。

  修复 Netflix 错误代码 UI3012

接下来,我们需要进行 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 命令更改它

  8 个适合企业和个人使用的最佳静态网站托管

前任:

chmod 400 id_rsa
chmod 400 id_rsa.pub

让我们将公钥复制到 IP 地址为 192.168.56.101 的 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#

您可以在上面的输出中看到,已成功添加 1 个密钥。 这表明 SSH 密钥已被交换。

  如何在 Google 表格中转换货币

接下来,我们将设置一个 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#

上面的 Thea ping 到客户端以测试连接并确认是否良好。

结论

我希望这能给你一个想法,让你开始安装和玩耍。 请继续关注更多 Ansible 教程或查看此 Udemy Mastering Ansible 课程。