gpt4 book ai didi

Terraform 资源继承

转载 作者:行者123 更新时间:2023-12-02 22:38:45 25 4
gpt4 key购买 nike

有没有办法声明要继承的抽象资源?

示例:

resource "digitalocean_droplet" "worker_abstract" {
abstract = true // ???

name = "swarm-worker-${count.index}"
tags = [
"${digitalocean_tag.swarm_worker.id}"
]

// other config stuff

provisioner "remote-exec" {
//...
}
}

然后使用声明的资源和覆盖的变量:

resource "worker_abstract" "worker_foo" {
count = 2
name = "swarm-worker-foo-${count.index}"
tags = [
"${digitalocean_tag.swarm_worker.id}",
"${digitalocean_tag.foo.id}"
]
}

resource "worker_abstract" "worker_bar" {
count = 5
name = "swarm-worker-bar-${count.index}"
tags = [
"${digitalocean_tag.swarm_worker.id}"
"${digitalocean_tag.bar.id}"
]
}

最佳答案

这可能比您提议的解决方案更“冗长”,但这听起来像是 modules in 0.12 的完美用例。 .

您可以创建一个模块,例如在文件 worker/main.tf

variable "instances" {
type = number
}

variable "name" {
type = string
}

variable "tags" {
type = list(string)
default = []
}

resource "digitalocean_droplet" "worker" {
count = var.instances

name = "swarm-worker-${var.name}-${count.index}"
tags = var.tags

// other config stuff

provisioner "remote-exec" {
//...
}
}

然后你就可以像你的示例一样使用你的模块(假设从上面的目录worker)

module "worker_foo" {
source = "./worker"

instances = 2
name = "foo"
tags = [
"${digitalocean_tag.swarm_worker.id}",
"${digitalocean_tag.foo.id}"
]
}

module "worker_bar" {
source = "./worker"

instances = 5
name = "bar"
tags = [
"${digitalocean_tag.swarm_worker.id}"
"${digitalocean_tag.bar.id}"
]
}

关于Terraform 资源继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50083609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com