如何输出使用计数的数据源? [英] How can I output a data source that uses count?

查看:14
本文介绍了如何输出使用计数的数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想输出创建的每个 VM 及其 UUID,例如

I want to output each VM created and their UUID e.g

data "vsphere_virtual_machine" "vms" {
  count            = "${length(var.vm_names)}"
  name             = "${var.vm_names[count.index]}"
  datacenter_id = "12345"
}

output "vm_to_uuid" {
  # value = "${data.vsphere_virtual_machine.newvms[count.index].name}"
  value = "${data.vsphere_virtual_machine.newvms[count.index].id}"
}

我正在寻找的示例输出:

Example output I'm looking for:

"vm_to_uuids":[
    {
      "name":"node1",
      "id":"123456",
    },
    {
      "name":"node2",
      "id":"987654",
    }
]

推荐答案

在为输出 value 给出的表达式中使用 通配符属性 来获取 id 列表创建的虚拟机.例如

Use the wildcard attribute in the expression given for the output value to get the list of ids for the created VMs. e.g.

output "vm_to_uuids" {
  value = "${data.vsphere_virtual_machine.*.id}"
}

您的问题中提供的所需语法是一种例外,即功能优于形式.编写一个提供这并不简单的 terraform 配置.或许,我建议采用其他更简单的方式来输出相同的信息.

The required syntax provided in your question is one exemption where to prefer function over form. Writing a terraform configuration that provides that isn't straightforward. Perhaps, I suggest to adopt other simpler ways to output this same information.

可以输出映射到 id 的名称:

Names mapped to ids can be output:

output "vm_to_uuids" {
  value = "${zipmap(
              data.vsphere_virtual_machine.*.name,
              data.vsphere_virtual_machine.*.id)}"
}

名称和id的映射可以以列的方式输出:

A map of names and ids can be output in a columnar manner:

output "vm_to_uuids" {
  value = "${map("name",
              data.vsphere_virtual_machine.*.name,
              "id",
              data.vsphere_virtual_machine.*.id)}"
}

可以以列的方式输出名称和id的列表:

A list of names and ids can be output in a columnar manner:

output "vm_to_uuids" {
  value = "${list(
              data.vsphere_virtual_machine.*.name,
              data.vsphere_virtual_machine.*.id)}"
}

这篇关于如何输出使用计数的数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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