独立设置 Ansible

AnsibleAnsibleBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

欢迎来到 Ansible 配置挑战!你刚刚学习了如何在 Ubuntu 系统上安装和配置 Ansible。现在,是时候测试你的技能了。想象一下,你是一名新入职的系统管理员,你的第一个任务是在一台全新的 Ubuntu 服务器上设置 Ansible。这个挑战将模拟真实世界的场景,推动你应用所学的知识,并展示你独立使用 Ansible 的能力。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL ansible(("Ansible")) -.-> ansible/AnsibleSetupandConfigurationGroup(["Ansible Setup and Configuration"]) ansible(("Ansible")) -.-> ansible/ModuleOperationsGroup(["Module Operations"]) ansible(("Ansible")) -.-> ansible/InventoryManagementGroup(["Inventory Management"]) linux(("Linux")) -.-> linux/RemoteAccessandNetworkingGroup(["Remote Access and Networking"]) linux(("Linux")) -.-> linux/PackagesandSoftwaresGroup(["Packages and Softwares"]) ansible/AnsibleSetupandConfigurationGroup -.-> ansible/install("Ansible Setup") ansible/ModuleOperationsGroup -.-> ansible/ping("Network Test") ansible/InventoryManagementGroup -.-> ansible/groups_inventory("Define Inventory Groups") linux/RemoteAccessandNetworkingGroup -.-> linux/ssh("Secure Connecting") linux/PackagesandSoftwaresGroup -.-> linux/apt("Package Handling") subgraph Lab Skills ansible/install -.-> lab-390383{{"独立设置 Ansible"}} ansible/ping -.-> lab-390383{{"独立设置 Ansible"}} ansible/groups_inventory -.-> lab-390383{{"独立设置 Ansible"}} linux/ssh -.-> lab-390383{{"独立设置 Ansible"}} linux/apt -.-> lab-390383{{"独立设置 Ansible"}} end

Ansible 安装与配置

任务

  1. 更新并升级系统包。
  2. 在 LabEx VM(Ubuntu 22.04)上安装 Ansible。
  3. 配置 SSH 以实现对 localhost 的无密码认证。
  4. 创建一个包含两个组的 Ansible 库存文件(inventory file):“local”和“webservers”。
  5. 测试 Ansible 的连接性,确保其正常工作。

要求

  • 所有操作必须在 /home/labex/project 目录下完成。
  • 安装的 Ansible 版本必须是最新可用版本。
  • SSH 密钥对必须使用 RSA 4096 位生成,并添加注释 "labex@ansible-challenge"。
  • Ansible 库存文件必须创建在 /etc/ansible/hosts
  • 库存文件必须包含以下内容:
    • 一个 "local" 组,包含 "localhost",使用本地连接(local connection)。
    • 一个 "webservers" 组,包含名为 "web1" 的主机,指向 localhost,使用 SSH 连接。
  • 必须能够在不输入密码的情况下运行 Ansible 命令。

提示

系统会提示输入 labex 用户的密码。你可以在 VNC 终端中运行以下命令找到密码:

echo $PASSWORD

示例

完成挑战后,运行 ansible --version 应输出类似以下内容:

ansible x.x.x
...

/etc/ansible/hosts 文件的内容应如下所示:

[local]
localhost ansible_connection=local

[webservers]
web1 ansible_host=localhost ansible_connection=ssh ansible_user=labex

运行 ansible all -m ping 应返回 localhost 和 web1 的成功响应。

✨ 查看解决方案并练习

总结

在本挑战中,你展示了在全新的 Ubuntu 22.04 系统上设置 Ansible 的能力。你更新了系统、安装了 Ansible、配置了 SSH 以实现无密码认证、创建了 Ansible 库存文件,并验证了设置的功能。这些技能对于任何使用 Ansible 的系统管理员或 DevOps 工程师来说都是基础。通过完成这一挑战,你证明了能够独立在新环境中部署 Ansible,为你在实际场景中快速在新系统上设置自动化工具做好了准备。