【发布时间】: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