terraform 和 csv 作为数据源

terraform 和 csv 作为数据源

因此,我尝试使用 Terraform 在 vSphere 中配置多个虚拟机。我有一个 CSV,其中包含配置客户机的信息。当我对数据源进行硬编码时,虚拟机已配置,但当我尝试使用 csv 填充相同的源时,Terraform 计划告诉我数据中心未定义。我尝试了 CSVDECODE 和插值方法,但都没有用。

示例 csvdecode:

locals {
  virtualmachines = csvdecode(file("${path.module}/virtualmachines.csv"))
}

data "vsphere_datacenter" "dc" {
  count = length(local.virtualmachines)
  name =  local.virtualmachines[count.index].dcname
}

插值示例:


locals {
  virtualmachines = csvdecode(file("${path.module}/server_list.csv"))
}

    # Creating terraform resources from a CSV file using interpolation

    data "null_data_source" "csv_file" {
      inputs = {
        file_data = "${chomp(file("${path.module}/virtualmachines.csv"))}"
               }
    }

resource "null_resource" "csv_interpolation_method" {
  count = "${length(slice(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")), 1, length(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")))))}"

  triggers = {
    01 = "${element(split(",", element(slice(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")), 1, length(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")))), count.index)), 0)}"
    02 = "${element(split(",", element(slice(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")), 1, length(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")))), count.index)), 1)}"
    03 = "${element(split(",", element(slice(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")), 1, length(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")))), count.index)), 2)}"
    04 = "${element(split(",", element(slice(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")), 1, length(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")))), count.index)), 3)}"
    05 = "${element(split(",", element(slice(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")), 1, length(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")))), count.index)), 4)}"
    06 = "${element(split(",", element(slice(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")), 1, length(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")))), count.index)), 5)}"
    07 = "${element(split(",", element(slice(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")), 1, length(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")))), count.index)), 6)}"
    08 = "${element(split(",", element(slice(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")), 1, length(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")))), count.index)), 7)}"
    09 = "${element(split(",", element(slice(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")), 1, length(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")))), count.index)), 8)}"
    10 = "${element(split(",", element(slice(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")), 1, length(split("\n", lookup(data.null_data_source.csv_file.outputs, "file_data")))), count.index)), 9)}"
  }
}


locals {
  dcname    = null_resource.csv_interpolation_method.*.triggers.01
  storage   = null_resource.csv_interpolation_method.*.triggers.02
  pool      = null_resource.csv_interpolation_method.*.triggers.03
  vtemplate = null_resource.csv_interpolation_method.*.triggers.04
  vname     = null_resource.csv_interpolation_method.*.triggers.05
  cpu       = null_resource.csv_interpolation_method.*.triggers.06
  memory    = null_resource.csv_interpolation_method.*.triggers.07
  disk      = null_resource.csv_interpolation_method.*.triggers.08
  network   = null_resource.csv_interpolation_method.*.triggers.09
  guest_id  = null_resource.csv_interpolation_method.*.triggers.10
}

output "server_output" {
  value = "${local.dcname}"
}

data "vsphere_datacenter" "dc" {
  name = "${output.server_output}"
}

output "storage" {
  value = "${local.storage}"
}

data "vsphere_datastore" "datastore" {
  name          = "${local.storage}"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

output "pool" {
  value = "${local.pool}"
}

data "vsphere_resource_pool" "pool" {
  name          = "${local.pool}"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

output "network" {
  value = "${local.network}"
}

data "vsphere_network" "network" {
  name          = "${local.network}"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

output "template" {
  value = "${local.vtemplate}"
}

data "vsphere_virtual_machine" "template" {
  name          = "${local.vtemplate}"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}


resource "vsphere_virtual_machine" "vm" {
  count = length(local.virtualmachines)

  name             = local.virtualmachines[count.index].vname
  resource_pool_id = "${data.vsphere_resource_pool.pool.id}"
  datastore_id     = "${data.vsphere_datastore.datastore.id}"

  num_cpus                   = local.virtualmachines[count.index].cpu
  memory                     = local.virtualmachines[count.index].memory
  guest_id                   = local.virtualmachines[count.index].guest_id
  wait_for_guest_net_timeout = 0

  network_interface {
    network_id = "${data.vsphere_network.network.id}"
  }

  disk {
    label = "disk0"
    size  = local.virtualmachines[count.index].disk
  }

  clone {
    template_uuid = "${data.vsphere_virtual_machine.template.id}"

示例 csv 内容:

dcname,storage,pool,vtemplate,vname,cpu,memory,disk,network,guest_id
txpla-w01-dc,txpla-w01-vsan,txpla-w01-shared01/Resources,AHLC75-template,terraform-test1,2,1024,80,Guest VLAN 1017,centos64Guest
txpla-w02-dc,txpla-w02-vsan,txpla-w02-shared01/Resources,AHLC75-template,terraform-test2,3,2048,80,Guest VLAN 1017,centos64Guest

插值方法的错误信息(它永远不会到达资源)

A managed resource "output" "server_output" has not been declared in the root module

任何帮助,将不胜感激..

谢谢

答案1

在 Terraform 中,output块声明要传递给调用模块的值(或者,如果您已经在根模块中,则在应用后显示在控制台输出中)。

您无法从定义它们的模块内部引用它们,因此 Terraform 将其解释output.server_output为资源引用,就像您在配置中有一个块一样resource "output" "server_output",来自名为“output”的假设提供程序(实际上并不存在)。

如果你想在多个地方引用一个特定的值相同的模块而不重复其表达式,您可以改用本地值。您已经有了local.dcname,它指的是output "server_output",因此如果您将output.server_output引用替换local.dcname为,那么它应该会产生您想要的结果值。

相关内容