Ansible服务器配置实战指南
立即解锁
发布时间: 2025-08-25 01:31:14 阅读量: 3 订阅数: 5 

### Ansible 服务器配置实战指南
在当今的 IT 领域,自动化服务器配置和管理变得越来越重要。Ansible 作为一款强大的自动化工具,能够帮助我们高效地完成服务器的配置和管理任务。本文将详细介绍如何使用 Ansible 进行服务器配置,包括创建虚拟机、配置 SSH 访问、编写和运行 Ansible 剧本等内容。
#### 1. 环境准备
在开始使用 Ansible 之前,我们需要在 Google Cloud Platform(GCP)上创建一个虚拟机(VM)并分配一个静态 IP 地址。这里我们使用 Terraform 来完成这个任务。
##### 1.1 资源文件
首先,我们需要创建一个资源文件,用于定义 GCP 上的资源。以下是一个示例资源文件:
```hcl
provider "google" {
credentials = "${file("account.json")}"
project = "${var.project_name}"
region = "${var.default_region}"
}
resource "google_compute_instance" "nginx" {
name = "nginx"
machine_type = "n1-standard-1"
zone = "europe-west1-b"
disk {
image = "ubuntu-os-cloud/ubuntu-1704-zesty-v20170413"
}
network_interface {
network = "default"
access_config {
nat_ip = "${google_compute_address.nginx-ip.address}"
}
}
}
resource "google_compute_address" "nginx-ip" {
name = "nginx-ip"
}
```
这个资源文件定义了一个 GCP 虚拟机和一个静态 IP 地址。
##### 1.2 变量文件
接下来,我们需要创建一个变量文件,用于定义资源文件中使用的变量。以下是一个示例变量文件:
```hcl
variable "project_name" {
type = "string"
default = "implementing-modern-devops"
}
variable "default_region" {
type = "string"
default = "europe-west1"
}
```
这个变量文件定义了项目名称和默认区域。
##### 1.3 运行 Terraform 计划
在创建好资源文件和变量文件后,我们可以运行 Terraform 计划来查看即将创建的资源:
```bash
terraform plan
```
运行结果如下:
```plaintext
+ google_compute_address.nginx-ip
address: "<computed>"
name: "nginx-ip"
self_link: "<computed>"
+ google_compute_instance.nginx
can_ip_forward: "false"
disk.#: "1"
disk.0.auto_delete: "true"
disk.0.image: "ubuntu-os-cloud/ubuntu-1704-zesty-v20170413"
machine_type: "n1-standard-1"
metadata_fingerprint: "<computed>"
name: "nginx"
network_interface.#: "1"
network_interface.0.access_config.#: "1"
network_interface.0.access_config.0.assigned_nat_ip: "<computed>"
network_interface.0.access_config.0.nat_ip: "<computed>"
network_interface.0.address: "<computed>"
network_interface.0.name: "<computed>"
network_interface.0.network: "default"
self_link: "<computed>"
tags_fingerprint: "<computed>"
zone: "europe-west1-b"
Plan: 2 to add, 0 to change, 0 to destroy.
```
从运行结果可以看出,我们将创建一个静态 IP 地址和一个虚拟机。
##### 1.4 应用基础设施
确认计划无误后,我们可以应用基础设施:
```bash
terraform apply
```
运行结果如下:
```plaintext
google_compute_address.nginx-ip: Creating...
address: "" => "<computed>"
name: "" => "nginx-ip"
self_link: "" => "<computed>"
google_compute_address.nginx-ip: Still creating... (10s elapsed)
google_compute_address.nginx-ip: Creation complete
google_compute_instance.nginx: Creating...
can_ip_forward: "" => "false"
disk.#: "" => "1"
disk.0.auto_delete: "" => "true"
disk.0.image: "" => "ubuntu-os-cloud/ubuntu-1704-zesty-v20170413"
machine_type: "" => "n1-standard-1"
metadata_fingerprint: "" => "<computed>"
name: "" => "nginx"
network_interface.#: "" => "1"
network_interface.0.access_config.#: "" => "1"
network_interface.0.access_config.0.assigned_nat_ip: "" => "<computed>"
network_interface.0.access_config.0.nat_ip: "" => "35.187.81.127"
network_interface.0.address: "" => "<computed>"
network_interface.0.name: "" => "<computed>"
network_interface.0.network: "" => "default"
self_link: "" => "<computed>"
tags_fingerprint: "" => "<computed>"
zone: "" => "europe-west1-b"
google_compute_instance.nginx: Still creating... (10s elapsed)
google_compute_instance.nginx: Still creating... (20s elapsed)
google_compute_instance.nginx: Creation complete
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
```
从运行结果可以看出,静态 IP 地址和虚拟机已经成功创建。
#### 2. 验证 SSH 访问
在虚拟机创建完成后,我们需要验证是否可以通过 SSH 访问该虚拟机。可以通过以下步骤进行验证:
1. 在 GCP 控制台中,找到创建的虚拟机实例。
2. 点击实例行右侧的 SSH 按钮,打开 Cloud Console 窗口并获得终端访问权限。
如果 SSH 访问失败,需要在防火墙中添加一个允许入站流量到端口 22 的规则。在本示例中,可以允许任何 IP 地址的流量访问任何端口,但在实际生产环境中,这是一个安全风险,不建议这样做。
#### 3. 创建 Ansible 清单文件
一旦虚拟机可以通过 SSH 访问,我们就可以开始使用 Ansible 了。首先,我们需要创建一个清单文件,用于指定要管理的主机。以下是一个示例清单文件:
```plaintext
[nginx-servers]
35.187.81.127
```
这个清单文件定义了一个名为 `nginx-servers` 的主机组,并指定了一个公共 IP 地址。将该文件保存为 `inventory`,并放在一个新的文件夹中,例如 `ansible-nginx`。
#### 4. 验证主机可达性
创建好清单文
0
0
复制全文
相关推荐










