标签: terraform-template-file

如果满足条件,则将额外元素添加到 terraform 中的列表中

我有一个 terraform 列表,看起来像:

array = ["a","b","c"]
Run Code Online (Sandbox Code Playgroud)

在这个 terraform 文件中,有两个变量,称为年龄和性别,如果年龄等于 12 并且性别等于男性(即,如果 var. age == 12 && var.gender == 'male' 则数组应为 ["a","b","c","d"],否则数组应为 ["a","b","c “])。以下内容是否会沿着正确的道路进行,或者我是否需要使用其他方法?

array = ["a","b","c", var.age == 12 && var.gender == 'male' ? "d" : null]
Run Code Online (Sandbox Code Playgroud)

terraform terraform-template-file terraform0.12+

25
推荐指数
3
解决办法
2万
查看次数

terraform-kubernetes-provider 如何从文件创建机密?

我正在使用 terraform kubernetes-provider,我想将此kubectl命令转换为 TF:

kubectl create secret generic my-secret --from-file mysecret.json
Run Code Online (Sandbox Code Playgroud)

然而,secret资源的data字段似乎只需要一个 TF map

我试过类似的东西

data "template_file" "my-secret" {
  template = "${file("${path.module}/my-secret.json")}"
}

resource "kubernetes_secret" "sgw-config" {
   metadata {
     name = "my-secret"
   }
   type = "Opaque"
   data = "{data.template_file.my-secret.template}"
}
Run Code Online (Sandbox Code Playgroud)

但它抱怨这不是一张地图。所以,我可以做这样的事情:

   data = {
      "my-secret.json" = "{data.template_file.my-secret.template}"
   }
Run Code Online (Sandbox Code Playgroud)

但这将使用名为的顶级字段写入机密,my-secret.json并且当我批量挂载它时,它将无法与其他资源一起使用。

这里的诀窍是什么?

kubernetes terraform terraform-template-file

10
推荐指数
2
解决办法
6772
查看次数

terraform 复制/上传文件到 aws ec2 实例

我们有 cronjob 和 shell 脚本,我们希望在使用 terraform 创建实例时将它们复制或上传到 aws ec2 实例。

我们尝试了

  1. 文件配置器:但它不是 wokring ,并且阅读此选项不适用于所有 terraform 版本
      provisioner "file" {
        source      = "abc.sh"
        destination = "/home/ec2-user/basic2.sh"
      }
Run Code Online (Sandbox Code Playgroud)
  1. 尝试过数据模板文件选项
    data "template_file" "userdata_line" {
      template = <<EOF
    #!/bin/bash
    mkdir /home/ec2-user/files2
    cd /home/ec2-user/files2
    sudo touch basic2.sh
    sudo chmod 777 basic2.sh
    base64 basic.sh |base64 -d >basic2.sh
    EOF
    }
Run Code Online (Sandbox Code Playgroud)

尝试了所有选项,但没有一个工作。
你能帮忙或建议吗?
我是 terraform 的新手,长期以来一直在这方面苦苦挣扎。

terraform terraform-template-file terraform-provider-aws

9
推荐指数
4
解决办法
9716
查看次数

使用依赖存储桶名称的模板设置 S3 存储桶策略时如何避免循环错误?

我有一个terraform运行时失败的文件,terraform plan我收到错误消息:

Error: Cycle: module.hosting.data.template_file.bucket_policy, module.hosting.aws_s3_bucket.website
Run Code Online (Sandbox Code Playgroud)

这是有道理的,因为存储桶指的是策略,反之亦然:

data "template_file" "bucket_policy" {
  template = file("${path.module}/policy.json")
  vars = {
    bucket = aws_s3_bucket.website.arn
  }
}

resource "aws_s3_bucket" "website" {
  bucket = "xxx-website"

  website {
    index_document = "index.html"
  }

  policy = data.template_file.bucket_policy.rendered
}
Run Code Online (Sandbox Code Playgroud)

如何避免这种双向引用?

amazon-web-services terraform terraform-template-file terraform-provider-aws

8
推荐指数
2
解决办法
715
查看次数

如何在 terraform 中创建集合

我正在尝试创建一个集合,用作 terraform 中 setproduct 函数的参数。当我尝试时:

toset([a,b,c])
Run Code Online (Sandbox Code Playgroud)

我收到一条错误消息,提示我无法将元组转换为列表。我尝试过各种方法,例如在不同的地方使用tolistand...并只使用一对大()括号,但我仍然无法使其工作 - 有人知道我如何从 a、b 和 c 创建一组吗?

set terraform terraform-template-file

8
推荐指数
1
解决办法
1万
查看次数

想要使用 terraform 在 GCP 中部署具有公共可读存储对象权限的存储桶

我创建了一个 terraform 文件来创建具有公共可读存储对象权限的 Google 存储桶。我能够部署存储桶,但无法针对我的模板分配正确的 ACL,我发现 ACL 部分有一些错误。

provider "google-beta" {
  project = "${var.project}"
}

 resource "google_storage_default_object_access_control" "public_rule" {
  bucket = "google_storage_bucket.test-${var.project}"
  role   = "READER"
  entity = "allUsers"
 }

resource "google_storage_bucket" "bucket" {
  name = "test-${var.project}"
  storage_class = "standard"
  location = "US"
}
Run Code Online (Sandbox Code Playgroud)

错误:已附在此输入图像描述

如果有人可以帮助我在创建存储桶时分配权限,那就太好了。

google-cloud-storage google-cloud-platform terraform terraform-template-file terraform-provider-gcp

7
推荐指数
2
解决办法
5660
查看次数

如何在Terraform中创建存档文件?

我在terraform中有这个代码:

data "archive_file" "lambdazip" {

type        = "zip"
output_path = "lambda_launcher.zip"

source_dir = "lambda/etc"
source_dir = "lambda/node_modules"

source {
  content  = "${data.template_file.config_json.rendered}"
  filename = "config.json"
 }
}
Run Code Online (Sandbox Code Playgroud)

我这样做时会出现以下错误terraform plan:

* data.archive_file.lambdazip: "source": conflicts with source_dir 
("lambda/node_modules")
* data.archive_file.lambdazip: "source_content_filename": conflicts 
with source_dir ("lambda/node_modules")
* data.archive_file.lambdazip: "source_dir": conflicts with 
source_content_filename ("/home/user1/experiments/grascenote-
poc/init.tpl")
Run Code Online (Sandbox Code Playgroud)

我使用的是terraform版本v0.9.11

terraform terraform-template-file

6
推荐指数
1
解决办法
2357
查看次数

是否可以为提供不同变量值的不同资源重用Terraform模板?

我正在使用Terraform设置在DigitalOcean上运行Consul的多个液滴。也许我缺少一些基本的知识,但是为它们提供正确的配置似乎很困难。

resource "digitalocean_droplet" "prime" {
  count  = 3
  image  = "${data.digitalocean_image.prime.image}"
  name   = "${format("%s-%02d-%s", "prime", count.index + 1, var.region)}"
  private_networking = true

  # ...
}
Run Code Online (Sandbox Code Playgroud)

每台机器都有两个网络接口-公共和专用。通过这种设置,似乎有必要提供bind_addr指向每个Droplet的私有IP地址的指示-否则领事退出并出现错误,指出存在多个私有(?!)地址。

最直接的解决方案是为每台机器提供一个配置文件,该文件在每种情况下几乎都是相同的,但bind_addr字段的值却不同,如下所示:

{
  "server": true,
  "data_dir": "/var/consul/data",
  "ui": true,
  "bind_addr": "${ private_ipv4_address }"
}
Run Code Online (Sandbox Code Playgroud)

模板是不是?我不知道如何使用它们。定义模板时,似乎只能为模板提供一次变量:

data "template_file" "consul-config" {
  template = "${file("templates/consul-config.json.tpl")}"

  vars {
    private_ipv4_address = "10.0.0.1" # At this point the real address is not known
  }
}

resource "digitalocean_droplet" "prime" {
  ...

  provisioner "file" {
    content = "${data.template_file.consul-config.rendered}"
    destination …
Run Code Online (Sandbox Code Playgroud)

digital-ocean terraform terraform-template-file

6
推荐指数
1
解决办法
991
查看次数

在terraform中使用count.index吗?

我正在尝试从模板生成一堆文件-我需要用count.index替换硬编码的1,不确定terraform允许使用什么格式。

resource "local_file" "foo" {
  count = "${length(var.files)}"

  content  = "${data.template_file.tenant_repo_multi.1.rendered}"
  #TODO: Replace 1 with count index.
  filename = "${element(var.files, count.index)}"
}


data "template_file" "tenant_repo_multi" {

  count = "${length(var.files)}"
  template = "${file("templates/${element(var.files, count.index)}")}"

}

variable "files" {
  type    = "list"
  default = ["filebeat-config_filebeat.yml",...]
}
Run Code Online (Sandbox Code Playgroud)

我在跑步

Terraform v0.11.7
+ provider.gitlab v1.0.0
+ provider.local v1.1.0
+ provider.template v1.0.0
Run Code Online (Sandbox Code Playgroud)

terraform terraform-template-file

6
推荐指数
1
解决办法
9246
查看次数

我如何在 terraform 中的 for_each 中设置计数

我正在通过构建一个模板来在 Hetzner 云中创建我的基础设施来学习 terraform。为此,我使用 hcloud 提供商。

我创建一个映射变量主机来创建> 1个具有不同配置的服务器。

variable "hosts" {
    type = map(object({
        name                    = string
        serverType              = string
        serverImage             = string
        serverLocation          = string
        serverKeepDisk          = bool
        serverBackup            = bool 
        ip                      = string
      }))
    }
Run Code Online (Sandbox Code Playgroud)

这工作正常。但我还需要配置卷。我只需要 2 个服务器额外的卷,并且 terraform 必须检查变量卷是否为真。如果为 true,则应创建具有给定详细信息的新卷并将其附加到服务器。为此,我编辑我的变量主机

variable "hosts" {
    type = map(object({
        name                    = string
        serverType              = string
        serverImage             = string
        serverLocation          = string
        serverKeepDisk          = bool
        serverBackup            = bool 
        ip                      = string

        volume                  = bool
        volumeName              = string …
Run Code Online (Sandbox Code Playgroud)

terraform terraform-template-file hcloud

6
推荐指数
1
解决办法
2万
查看次数