我如何在Terraform中建立地图列表 [英] how do i build a list of maps in terraform

查看:82
本文介绍了我如何在Terraform中建立地图列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Terraform中建立一个地图列表.有没有能让我做到的手术.

i would like to build a list of maps in terraform. Is there an operation that would let me do that.

例如:我想构建结构(来自aws_instance.test.*.private_ip)

eg: i would like to build the structure (from aws_instance.test.*.private_ip)

addresses = [
   {
     address = private_ip-1
   },
   {
     address = private_ip-2
   }
   ...
]

推荐答案

很抱歉,如果这个问题已经解决,我认为这是一个更老的问题.

Apologies if this has already been resolved, I see that it's an older question.

我不确定这是否是最佳做法,但这是我尝试达到所需状态的方式.

I'm not sure if this is best practice, but this is the way I would attempt to reach the desired state.

使用 null_resource ,您可以执行以下操作:

Using the null_resource you'd be able to do the following:

variable "addresses" {
  type = "list"

  default = [
    "private_ip-1",
    "private_ip-2",
    "private_ip-3"
  ]
}

resource "null_resource" "test" {
  count = "${length(var.addresses)}"

  triggers {
    address = "${element(var.addresses, count.index)}"
  }
}

output "addresses" {
  value = "${null_resource.test.*.triggers}"
}

并显示以下输出:

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

Outputs:

addresses = [
    {
        address = private_ip-1
    },
    {
        address = private_ip-2
    },
    {
        address = private_ip-3
    }
]

我当前使用的是terraform 0.11.5,在

I'm currently on terraform 0.11.5, null_resource appears to have been added in 0.6.7

null_resource触发器有一些限制.插值变量只能产生字符串.因此,很遗憾,您将无法插值会产生列表或地图的值;例如:

There are limitations to the null_resource triggers though. interpolated variables can only result in strings. So, unfortunately you won't be able to interpolate a value that would result in a list or a map; for example:

resource "null_resource" "test" {
  triggers {
    mylist = "${var.addresses}"
  }
}

将导致错误

错误:null_resource.test:触发器(地址):"预期类型为字符串",得到了不可转换的类型为[[] interface {}"

Error: null_resource.test: triggers (address): '' expected type 'string', got unconvertible type '[]interface {}'

这篇关于我如何在Terraform中建立地图列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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