我有这个很好用的脚本。它创建了 3 个带有 sepcified 标签的实例,以便轻松识别它们。但问题是我想在代码中添加一个远程执行配置程序(当前已注释)以安装一些软件包。如果我使用计数,我可以循环它来对所有实例执行远程执行。我不能使用 count 因为我必须使用 for_each 来循环本地列表。由于 count 和 for_each 不能一起使用,我如何遍历实例以检索它们的 IP 地址以在 remote-exec 配置程序中使用。
在数字海洋和 AWS 上,我能够使用它来工作,host = "${self.public_ip}"
但它在 vultr 上不起作用并给出不支持的属性错误
实例.tf
resource "vultr_ssh_key" "kubernetes" {
name = "kubernetes"
ssh_key = file("kubernetes.pub")
}
resource "vultr_instance" "kubernetes_instance" {
for_each = toset(local.expanded_names)
plan = "vc2-1c-2gb"
region = "sgp"
os_id = "387"
label = each.value
tag = each.value
hostname = each.value
enable_ipv6 = true
backups = "disabled"
ddos_protection = false
activation_email = false
ssh_key_ids = [vultr_ssh_key.kubernetes.id]
/* connection {
type = "ssh"
user = "root"
private_key = file("kubernetes")
timeout = "2m"
host = vultr_instance.kubernetes_instance[each.key].ipv4_address
}
provisioner "remote-exec" {
inline = "sudo hostnamectl set-hostname ${each.value}"
} */
}
locals {
expanded_names = flatten([
for name, count in var.host_name : [
for i in range(count) : format("%s-%02d", name, i + 1)
]
])
}
提供者.tf
terraform {
required_providers {
vultr = {
source = "vultr/vultr"
version = "2.3.1"
}
}
}
provider "vultr" {
api_key = "***************************"
rate_limit = 700
retry_limit = 3
}
变量.tf
variable "host_name" {
type = map(number)
default = {
"Manager" = 1
"Worker" = 2
}
}