变量值的 Heredoc 语法 [英] Heredoc syntax for variable values

查看:22
本文介绍了变量值的 Heredoc 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm attempting to use Heredoc syntax as a value for a string variable as per the following

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",值为空.

This doesn't result in Terraform producing an error, however when the variable is later used in a remote-exec command "echo ${var.docker_config} > /home/ubuntu/.docker/config.json", the value is empty.

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

Is this the correct way to use Heredoc syntax in a variable?

推荐答案

可以在变量中做heredoc,在局部变量中做heredoc,也可以构造一个map,使用jsonencode把它变成一个字符串.您以后也可以使用其中任何一种.

You can do a heredoc in a variable, a heredoc in a local variable, or you can construct a map and use jsonencode to turn it into a string. You can use any of these later as well.

✗ 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": "****"
    }
  }
}

这篇关于变量值的 Heredoc 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆