> For the complete documentation index, see [llms.txt](https://docs.verge.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.verge.io/learn-the-platform/zh/mo-kuai-8-kai-fa-yu-devops/lab.md).

# 实验：基础设施自动化

## 目标

使用 REST API、Terraform 提供程序以及脚本工具（pyvergeos 或 PowerShell）自动化 VergeOS 基础设施运维。在本实验结束时，你将能够以程序化方式配置虚拟机和租户，并理解如何将 VergeOS 集成到 CI/CD 和自动化工作流中。

## 前置条件

* 已完成模块 1：架构基础
* 已完成第 6 模块：虚拟机
* 已完成模块 7：多租户
* 已完成模块 8 阅读（REST API、Terraform、Python/PowerShell、Cloud-Init）
* 一个已运行且启用 API 访问的 VergeOS 集群
* 本地已安装 Terraform（v1.5+）
* 本地已安装 Python 3.9+ 或 PowerShell 7+
* 访问 [vergeos-terraform-playground](https://github.com/verge-io/vergeos-terraform-playground) 代码仓库

## 难度

**中级** -- 需要熟悉 REST API、基础设施即代码概念以及基本脚本编写

## 预计时间

**2 小时**

## 步骤

### 第 1 部分：REST API 探索

使用 VergeOS API 进行身份验证并执行基本操作。

1. 从 VergeOS UI 获取 API 密钥（System > Users > \[你的用户] > API Keys）
2. 使用 `curl` 或使用 REST 客户端进行身份验证：

   ```bash
   curl -X GET https://<your-vergeos-host>/api/v4/vms \
     -H "Authorization: Bearer <your-api-key>"
   ```
3. 使用 `/api/v4/vms` 端点列出所有虚拟机
4. 通过 API 创建一个新的虚拟机：
   * 向 `/api/v4/vms` 发送包含名称、CPU、内存和磁盘的 JSON 请求体
5. 使用 API 启动和停止虚拟机
6. 通过 API 删除虚拟机
7. 探索其他端点：网络（`/api/v4/vnets` — 数据表名为 `vnets` ，而 UI/对象名称为“Networks”），租户（`/api/v4/tenants`)

### 第 2 部分：Terraform 提供程序

使用 VergeOS Terraform 提供程序以声明式方式管理基础设施。

1. 克隆 vergeos-terraform-playground 代码仓库：

   ```bash
   git clone https://github.com/verge-io/vergeos-terraform-playground.git
   cd vergeos-terraform-playground
   ```
2. 查看 `main.tf`
3. 复制并自定义一个示例 `.tfvars` 文件以适配你的环境
4. 运行 `terraform init` 以下载 VergeOS 提供程序
5. 运行 `terraform plan` 以预览基础设施更改
6. 运行 `terraform apply` 以配置资源
7. 在 VergeOS UI 中验证已创建的资源
8. 修改 Terraform 配置中的某个资源并重新应用
9. 运行 `terraform destroy` 以进行清理

### 第 3 部分：脚本自动化

使用 pyvergeos（Python）或 PowerShell 模块自动化一个工作流。

1. 安装 pyvergeos：

   ```bash
   pip install pyvergeos
   ```
2. 编写一个脚本，它：
   * 连接到你的 VergeOS 集群
   * 列出所有虚拟机及其当前状态
   * 从 recipe/template 创建一个新的虚拟机
   * 等待虚拟机运行
   * 输出虚拟机的 IP 地址和连接详情
3. 将脚本扩展为：
   * 创建一个具有预定义资源分配的租户
   * 在租户内部署一台虚拟机
   * 通过检查网络连通性验证租户隔离

### 第 4 部分：Cloud-Init

使用 cloud-init 配置无人值守的虚拟机部署。

1. 创建一个包含以下内容的 cloud-init 配置文件：
   * 主机名和网络设置
   * 用户账户和 SSH 密钥
   * 软件包安装命令
   * 启动后脚本
2. 通过 API 或 UI 将 cloud-init 配置附加到新的虚拟机
3. 启动虚拟机并验证 cloud-init 配置已应用：
   * 检查主机名和网络设置
   * 验证用户账户是否存在
   * 确认软件包已安装

## 验证

当你能够回答以下内容时，你的基础设施自动化实验就完成了 **是** 时，你的 VMware 迁移实验就完成了：

* [ ] 已成功通过 VergeOS REST API 进行身份验证，并对虚拟机执行了 CRUD 操作
* [ ] 使用来自 playground 代码仓库的 Terraform 提供程序配置了基础设施
* [ ] 修改并重新应用了 Terraform 配置，观察到增量更改
* [ ] 创建了一个自动化虚拟机和/或租户配置的脚本（Python 或 PowerShell）
* [ ] 使用 cloud-init 为虚拟机配置了无人值守安装，并验证配置已应用
* [ ] 清理了所有实验资源（虚拟机、租户、Terraform 状态）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.verge.io/learn-the-platform/zh/mo-kuai-8-kai-fa-yu-devops/lab.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
