【问题标题】:Terraform - how To make the 'local_file' resource to be recreated with every 'terraform apply'Terraform - 如何使用每个“terraform apply”重新创建“local_file”资源
【发布时间】:2021-08-28 10:07:55
【问题描述】:

我的 terraform 配置中有一个 local_file 资源, 问题是如何告诉 terraform 我希望每次我的客户运行“terraform apply”时都重新创建这个资源,即使资源本身没有任何变化,我怎样才能做到这一点? local_file resource cant do what i want

我在 null_resource 中使用触发器来执行此操作,但 local_file 资源中没有这样的选项。 null_resource does what i want

【问题讨论】:

  • 你看过terraform -replaceterraform taint吗?
  • 来自我们的How to Ask 帮助页面:请勿发布代码、数据、错误消息等的图像 - 将文本复制或输入到问题中。请保留将图像用于图表或演示渲染错误,这些无法通过文本准确描述的事情。有关更多信息,请参阅元常见问题解答条目Why not upload images of code/errors when asking a question?
  • 我不想使用特殊的 terraform 命令来执行此操作,此代码将被转移到我的客户端,我需要他们只执行 'terraform apply',没有办法让此资源重新创建自己每次申请?这一定是可能的

标签: terraform


【解决方案1】:

代替本地提供者尝试模板提供者并使用空资源创建文件。因此该触发器将负责重新创建文件。测试如下

  template = file("${path.module}/templates/inventory.tpl")
   vars = {
      bastion_host = local.Mongo_Bastion_host
      key_file = var.private_key_file_path
    }
}

resource "null_resource" "copy-inventory" {
  triggers = {
    ip = local.random_id
  }
  provisioner "local-exec" {
    command = "echo ${data.template_file.inventory_cfg.rendered} >>inventory"
}
}

在应用 terraform 后,我重新运行了 terraform plan 并且能够看到它正在再次创建文件

Terraform 将执行以下操作:

# null_resource.copy-inventory must be replaced
-/+ resource "null_resource" "copy-inventory" {
      ~ id       = "7011189362963809116" -> (known after apply)
      ~ triggers = {
          - "ip" = "f9f0f771-42ae-176e-3722-5b342665dea2"
        } -> (known after apply) # forces replacement
    }

Plan: 1 to add, 0 to change, 1 to destroy.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-08
    • 2022-12-29
    • 2021-09-28
    • 2018-10-23
    • 2019-12-13
    • 2020-08-07
    • 2019-03-12
    • 2021-01-21
    相关资源
    最近更新 更多