变量值的Heredoc语法

Trajan

我正在尝试使用Heredoc语法作为字符串变量的值,如下所示

variable "docker_config" {
  type = "string"
  default = <<EOF
{ "auths": { "https://index.docker.io/v1/": { "auth": "****" } } }
EOF
}

这不会导致Terraform产生错误,但是稍后在远程执行命令“ echo $ {var.docker_config}> /home/ubuntu/.docker/config.json”中使用该变量时,该值为空。

这是在变量中使用Heredoc语法的正确方法吗?

SomeGuyOnAComputer

您可以在变量中使用Heredoc,在局部变量中使用Heredoc,也可以构造映射并将jsonencode其转换为字符串。您以后也可以使用其中任何一个。

✗ cat main.tf

variable "test" {
  description = "Testing heredoc"
  default     = <<EOF
        "max-size": "8m",
        "min-size": "1m",
        "count": "8",
        "type": "string",
EOF
}

locals {
  docker_config = <<EOF
{
  "auths": {
    "https://index.docker.io/v1/": {
      "auth": "****"
    }
  }
}
EOF

  even_better = {
    auths = {
      "https://index.docker.io/v1/" = {
        auth = "****"
      }
    }
  }
}

output "test_var" {
  value = var.test
}

output "test_local" {
  value = local.docker_config
}

output "even_better" {
  value = jsonencode(local.even_better)
}
$ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

even_better = {"auths":{"https://index.docker.io/v1/":{"auth":"****"}}}
test_local = {
  "auths": {
    "https://index.docker.io/v1/": {
      "auth": "****"
    }
  }
}

test_var = {
  "auths": {
    "https://index.docker.io/v1/": {
      "auth": "****"
    }
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章