我正在使用upcloud提供者插件和terraform 0.12.7来创建新的服务器实例。不幸的是,插件离开了主TF站点,在https://github.com/UpCloudLtd/terraform-provider-upcloud,所以它必须安装使用go语言,但这一部分变得很好。
provider "upcloud" {
# Your UpCloud credentials are read from the environment variables
# export UPCLOUD_USERNAME="Username for Upcloud API user"
# export UPCLOUD_PASSWORD="Password for Upcloud API user"
}
resource "upcloud_server" "test" {
# System hostname
hostname = "test.example.com"
# Availability zone
zone = "de-fra1"
# Number of CPUs and memory in MB
cpu = "4"
mem = "8192"
storage_devices {
# OS root disk size
size = "20"
action = "clone"
# Template Ubuntu 18.04
storage = "01000000-0000-4000-8000-000030080200"
tier = "maxiops"
}
# Include at least one public SSH key
login {
user = "root"
keys = [
# File module reads file as-is including trailing linefeed that breaks during terraform apply
"${replace(file("${var.ssh_pubkey}"), "\n", "")}"
]
}
# Configuring connection details
connection {
host = "${self.ipv4_address}"
type = "ssh"
user = "root"
private_key = "${file("${var.ssh_privkey}")}"
}
# Remotely executing a command on the server
provisioner "remote-exec" {
inline = [
"echo 'Hello world!'"
]
}
}
output "server_ip" {
value = "${upcloud_server.test.ipv4_address}"
}
它成功地创建了资源。现在,我将资源名更改为"test5“(也是主机名和其他硬编码值),运行terraform计划,然后它告诉我,它希望销毁"test”资源,即使它在tf状态文件中定义了“test”资源,而且我也没有请求删除它。我试着定义terraform后端"local",始终保持工作区的持久性,但是它并没有改变任何事情。我做错什么了?如果我使用OS模板或快照卷ID,它的工作方式是相同的。
还有什么方法可以参数化这个资源名(或者在输出部分,self不在这里工作),所以我不需要运行sed命令来保持名称的一致性?其他变量可以在地形图中进行调整或应用--不是一个问题。谢谢你的答复
发布于 2019-09-04 05:04:30
你不能就这样重命名资源..。
如果这样做,terraform会识别与其状态文件有关的资源定义中的以下两个更改:
test
(因为存储在状态文件中的资源的定义已经消失)test5
(因为状态文件中不存在该资源)如果要重命名资源,则必须相应地更新状态文件。检查我们的Terraform命令“状态mv”。
在您的示例中,您需要运行terraform state mv upcloud_server.test upcloud_server.test5
、和,在资源定义中重命名资源。
然后,定义将再次匹配状态文件。
https://stackoverflow.com/questions/57784423
复制