【问题标题】:Passing data as variable to Terraform module将数据作为变量传递给 Terraform 模块
【发布时间】:2021-07-07 03:36:14
【问题描述】:

我正在寻找一种将数据 template_cloudinit_config 传递给另一个模块的方法。我很清楚如何将变量传递给包括对象在内的各种数据类型的模块,但我不确定如何处理数据。

在这个设置中,我有一个定义所有虚拟硬件配置的 vm-basic 模块,以及一个定义服务相关信息的 postgres Terraform 脚本,包括云初始化脚本。目的是让 vm 虚拟硬件配置作为模块高度可重用,让我只关注与服务相关的信息,即 postgres、nginx 等。

这是我的 vm-basic vars.tf 文件,它将接受将在虚拟硬件配置中使用的参数。

variable "prefix" {}

variable "rg" { type = object({ 
    name = string
    location = string 
}) } 

variable "vm_size" {}
variable "private_ip_address" {}
variable "subnet" { type = object({ id = string }) } 
variable "data_disk_size_gb" { type = number }

variable "service_name" { type = string }
variable "admin_username" { type = string }
variable "admin_public_key_path" { type = string }

variable "nsg_allow_tcp_ports" { type = list(string) }

locals {
  nsg_allow_tcp_ports = {for p in var.nsg_allow_tcp_ports: index(var.nsg_allow_tcp_ports, p) => p}
}


#### DOES NOT WORK ######
#### Expected an equals sign ("=") to mark the beginning of the attribute value. ######
variable "custom_data" { type = object({ data }) }

如何在 vm-basic 模块中使用自定义数据

resource "azurerm_linux_virtual_machine" "vm" {
    name                  = "${var.prefix}-${var.service_name}-vm"
    location              = var.rg.location
    resource_group_name   = var.rg.name
    ...
    ...
    custom_data = var.custom_data.rendered
    ...
    ...
}

其他脚本如何将参数传递给 vm-basic 模块

module "vm-basic" { 
  source = "../../base/vm"
  service_name = var.service_name

  prefix = var.prefix
  rg = var.rg
  vm_size = var.vm_size
  private_ip_address = var.private_ip_address
  subnet = var.subnet
  data_disk_size_gb = var.data_disk_size_gb

  admin_username = var.admin_username
  admin_public_key_path = var.admin_public_key_path

  nsg_allow_tcp_ports = var.nsg_allow_tcp_ports
}

data "template_cloudinit_config" "config" {
  gzip = true
  base64_encode = true
  part {
      filename = "init-cloud-config"
      content_type = "text/cloud-config"
      content = file("init.yaml")
  }
  part {
      filename = "init-shellscript"
      content_type = "text/x-shellscript"
      content = templatefile("init.sh",
        { hostname = "${var.prefix}-${var.service_name}" }
      )
  }
}

如何将数据对象传递给另一个 Terraform 模块?

【问题讨论】:

  • 什么是var.custom_data.rendered
  • @Marcin custom_data in var.custom_data.rendered 指的是变量 vars.tf 文件中的那个

标签: terraform terraform-provider-azure


【解决方案1】:

在变量 vars.tf 文件中,只要做就够了

variable "custom_data" {}

在vm-basic模块中,通过var引用变量,其他类似

custom_data = var.custom_data.rendered

【讨论】:

    【解决方案2】:

    您看到的错误的含义是 Terraform 期望 object 类型约束的参数是 name = type 对,但您只写了 data,因此 Terraform 报告缺少 = .

    要完成这项工作,您需要编写一个有效的类型约束。从您的问题中我不清楚custom_data 到底代表什么,但我确实看到您后面的示例包含var.custom_data.rendered,因此我可以看出类型约束应该至少包含一个@ 987654327@ 属性以使其有效,而 azurerm_linux_virtual_machinecustom_data 参数需要一个字符串,因此我将匹配它:

    variable "custom_data" {
      type = object({
        rendered = string
      })
    }
    

    这意味着 Terraform 将接受任何具有rendered 属性的对象值,该属性可以转换为string,因此您以后对var.custom_data.rendered 的引用可以保证工作并始终生成字符串值。

    【讨论】:

      猜你喜欢
      • 2018-09-07
      • 2023-03-23
      • 2020-10-13
      • 2019-05-20
      • 2020-12-10
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      • 2020-05-16
      相关资源
      最近更新 更多