gpt4 book ai didi

azure - 使用 for_each 时从 Terraform 创建库存文件

转载 作者:行者123 更新时间:2023-12-03 02:26:20 29 4
gpt4 key购买 nike

我有以下结构:

Terraform Azure Pipeline Structure

PreProd > Variables.tf 具有:

variable "vms" {

default = [
{
"hostname" : "Monitor01",
"size" : "Standard_D2as_v4"
"managed_disk_type" : "Premium_LRS"
"extra_tag" : {
"name" : "monitoring1",
"env" : "dev",
"role" : "test1"
}
},
{
"hostname" : "Monitor02",
"size" : "Standard_D2as_v4"
"managed_disk_type" : "Premium_LRS"
"extra_tag" : {
"name" : "monitoring2",
"env" : "dev",
"role" : "test2"
}
},

模块 > main.tf 具有:

# Create public IP's
resource "azurerm_public_ip" "publicip" {
for_each = { for vm in var.vms : vm.hostname => vm }

name = "${each.value.hostname}-PublicIP"
location = var.vm_location
resource_group_name = var.vm_resource_group
allocation_method = "Static"
}

# Create network interface for Public IP's
resource "azurerm_network_interface" "publicnic" {
for_each = { for vm in var.vms : vm.hostname => vm }
name = "${each.value.hostname}-NIC"
location = var.network_location
resource_group_name = var.vm_resource_group
ip_configuration {
name = "monitoringConfg"
subnet_id = data.azurerm_subnet.vm_subnet.id
private_ip_address_allocation = "dynamic"
public_ip_address_id = azurerm_public_ip.publicip[each.key].id
}
tags = each.value.extra_tag
}

# Create Linux virtual machines
resource "azurerm_virtual_machine" "vm" {
for_each = { for vm in var.vms : vm.hostname => vm }

name = each.value.hostname
location = var.vm_location
resource_group_name = var.vm_resource_group
network_interface_ids = [azurerm_network_interface.nic[each.key].id]
vm_size = each.value.size
delete_data_disks_on_termination = true
delete_os_disk_on_termination = true

我想为所有虚拟机及其公共(public)和私有(private) Ip 创建一个 list 文件。

模块 > Outputs.tf 具有:

output "vm_names" {
description = "Name of VMs"
value = [for k, vm in azurerm_virtual_machine.vm: vm.name]
}

data "template_file" "inventory" {
template = "${file("${path.module}/inventory.tmpl")}"
vars = {
#k8s_master_name = azurerm_network_security_group.toolservernsg.id
k8s_master_name = [for k, p in azurerm_virtual_machine.vm: p.name]
}
}

resource "local_file" "save_inventory" {
content = "${data.template_file.inventory.rendered}"
filename = "./myhosts.cfg"
}

模块 > inventory.tmpl 文件具有:

[servers]
${k8s_master_name}

我正在努力获取使用以下结构创建的 myhosts.cfg 文件:

[servers]
Monitor01 privateip="x.x.x.x" publicip="x.x.x.x."
Monitor02 privateip="x.x.x.x" publicip="x.x.x.x."

[nsg]

当我执行 terraform apply 时,出现以下错误:

Incorrect attribute value

当我进行 terraform 输出时,我得到以下结果:

Terraform output : no outputs found

我已经阅读了很多论坛并尝试了所有这些输出变化,但我无法获得所需的结果:

output "vm_name" {
description = "Name of VMs"
value = values(azurerm_virtual_machine.vm)[*].name
}

output "vm_name2" {
description = "Name of VMs as a map"
value = {for k, vm in azurerm_virtual_machine.vm : k => vm.name}
}

output "vm_name3" {
description = "Name of VMs as a map"
value = azurerm_virtual_machine.vm[*].name
}

output "vm_names" {
description = "Name of VMs as a map"
value = [for p in azurerm_virtual_machine.vm:p.name]
}

最佳答案

template_file 只能接受字符串,但您正在向其传递一个值列表。所以失败了。

您可以尝试使用jsonencode:

data  "template_file" "inventory" {
template = "${file("${path.module}/inventory.tmpl")}"
vars = {
k8s_master_name = jsonencode([for k, p in azurerm_virtual_machine.vm: p.name])
}
}

或者,您可以查看 templatefile它可以接受值列表,而不是 template_file

关于azure - 使用 for_each 时从 Terraform 创建库存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66716192/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com