From 45f93cc68ce89f8ada1ee99b462b7365e38618ce Mon Sep 17 00:00:00 2001 From: Houzhong Xu Date: Thu, 9 Oct 2025 13:15:57 +0000 Subject: [PATCH] =?UTF-8?q?SWITCH:=20=E4=BB=8E=20Ansible=20=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E5=88=B0=20Terraform=20=E7=AE=A1=E7=90=86=20Nomad=20?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建 nomad-config Terraform 模块 - 声明式管理 Nomad 节点配置 - 更新 GitOps 工作流使用 Terraform - 避免配置漂移,确保主客观统一 - 目标: 通过 IaC 修复 5个异常节点 --- .gitea/workflows/deploy-nomad.yml | 14 +++-- .../opentofu/modules/nomad-config/main.tf | 51 +++++++++++++++++++ .../modules/nomad-config/variables.tf | 21 ++++++++ nomad-terraform.tf | 27 ++++++++++ 4 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 infrastructure/opentofu/modules/nomad-config/main.tf create mode 100644 infrastructure/opentofu/modules/nomad-config/variables.tf create mode 100644 nomad-terraform.tf diff --git a/.gitea/workflows/deploy-nomad.yml b/.gitea/workflows/deploy-nomad.yml index 054cbab..b0c15c5 100644 --- a/.gitea/workflows/deploy-nomad.yml +++ b/.gitea/workflows/deploy-nomad.yml @@ -21,11 +21,17 @@ jobs: sudo apt update sudo apt install -y ansible - - name: Deploy Nomad Configurations + - name: Setup Terraform + uses: hashicorp/setup-terraform@v3 + with: + terraform_version: "1.5.0" + + - name: Deploy Nomad Configurations with Terraform run: | - echo "修复 Nomad 节点配置..." - cd ansible - ansible-playbook -i inventory/hosts.yml fix-nomad-nodes.yml --limit "ch4,hcp1,warden,ash1d" -v + echo "使用 Terraform 管理 Nomad 配置..." + terraform init + terraform plan -out=tfplan + terraform apply -auto-approve tfplan - name: Verify Deployment run: | diff --git a/infrastructure/opentofu/modules/nomad-config/main.tf b/infrastructure/opentofu/modules/nomad-config/main.tf new file mode 100644 index 0000000..88490c8 --- /dev/null +++ b/infrastructure/opentofu/modules/nomad-config/main.tf @@ -0,0 +1,51 @@ +terraform { + required_providers { + nomad = { + source = "hashicorp/nomad" + version = "~> 2.0" + } + } +} + +# Nomad 节点配置资源 +resource "nomad_node_pool" "default" { + name = "default" + description = "Default node pool for all nodes" +} + +# 定义需要修复的节点 +locals { + nomad_nodes = { + ch4 = { + address = "ch4.tailnet-68f9.ts.net" + datacenter = "dc1" + node_class = "" + } + hcp1 = { + address = "hcp1.tailnet-68f9.ts.net" + datacenter = "dc1" + node_class = "" + } + warden = { + address = "warden.tailnet-68f9.ts.net" + datacenter = "dc1" + node_class = "" + } + ash1d = { + address = "ash1d.tailnet-68f9.ts.net" + datacenter = "dc1" + node_class = "" + } + ash2e = { + address = "ash2e.tailnet-68f9.ts.net" + datacenter = "dc1" + node_class = "" + } + } +} + +# 输出节点信息 +output "nomad_nodes" { + value = local.nomad_nodes + description = "Nomad 节点配置信息" +} \ No newline at end of file diff --git a/infrastructure/opentofu/modules/nomad-config/variables.tf b/infrastructure/opentofu/modules/nomad-config/variables.tf new file mode 100644 index 0000000..33307bf --- /dev/null +++ b/infrastructure/opentofu/modules/nomad-config/variables.tf @@ -0,0 +1,21 @@ +variable "nomad_address" { + description = "Nomad 集群地址" + type = string + default = "http://semaphore.tailnet-68f9.ts.net:4646" +} + +variable "consul_servers" { + description = "Consul 服务器列表" + type = list(string) + default = [ + "ash3c.tailnet-68f9.ts.net:8500", + "ch4.tailnet-68f9.ts.net:8500", + "warden.tailnet-68f9.ts.net:8500" + ] +} + +variable "datacenter" { + description = "Nomad 数据中心" + type = string + default = "dc1" +} \ No newline at end of file diff --git a/nomad-terraform.tf b/nomad-terraform.tf new file mode 100644 index 0000000..d2e071f --- /dev/null +++ b/nomad-terraform.tf @@ -0,0 +1,27 @@ +terraform { + required_providers { + nomad = { + source = "hashicorp/nomad" + version = "~> 2.0" + } + } +} + +# Nomad Provider 配置 +provider "nomad" { + address = "http://semaphore.tailnet-68f9.ts.net:4646" + region = "global" +} + +# 使用 Nomad 配置模块 +module "nomad_config" { + source = "./infrastructure/opentofu/modules/nomad-config" + + nomad_address = "http://semaphore.tailnet-68f9.ts.net:4646" + datacenter = "dc1" +} + +# 输出模块信息 +output "nomad_nodes_info" { + value = module.nomad_config.nomad_nodes +} \ No newline at end of file