gpt4 book ai didi

azure - 模块中尚未声明托管资源 "azurerm_network_interface"

转载 作者:行者123 更新时间:2023-12-03 07:03:35 28 4
gpt4 key购买 nike

我正在针对我的 Linux 虚拟机运行“terraform 计划”,但收到以下错误:

│ Error: Reference to undeclared resource

│ on .terraform/modules/vm-ansiblecontroller/virtual-machine/linux/outputs.tf line 13, in output "nic_id":
│ 13: value = azurerm_network_interface.nic-linux.id

│ A managed resource "azurerm_network_interface" "nic-linux" has not been declared in module.vm-ansiblecontroller.

我没有包含来自 RG 和 vNet 的任何代码,因为我希望我包含的内容足以解决此问题。

任何帮助将不胜感激,我只是不明白

module "vm-ansiblecontroller" {
resource_group_name = module.rg-ansiblecontroller.resource_group_name
location = local.location
linux_machine_name = "linux-test1"
tags = var.tags

nic_id = module.vm-ansiblecontroller.nic_id
subnet_id = module.subnet-networkcore.subnet_id

virtual_machine_size = "Standard_D2"

admin_username = "jpadmin"
admin_ssh_public_key = file("~/.ssh/id_rsa.pub")

source_image_publisher = "Canonical"
source_image_offer = "UbuntuServer"
source_image_sku = "16.04-LTS"
source_image_version = "latest"

operating_system_disk_cache = "ReadWrite"
operating_system_disk_type = "Standard_LRS"

ip_configuration_name = "internal"
private_ip_address_allocation = "Dynamic"

public_ip_allocation_method = "Static"
public_ip_sku = "Standard"

}

模块/虚拟机/main.tf

    # Linux Virtual Machine
resource "azurerm_linux_virtual_machine" "vm-linux" {

name = var.linux_machine_name
location = var.location
resource_group_name = var.resource_group_name
tags = var.tags

size = var.virtual_machine_size
admin_username = var.admin_username
disable_password_authentication = true
# network_interface_ids = [azurerm_network_interface.nic-linux.id]
network_interface_ids = var.nic_id

admin_ssh_key {
username = var.admin_username
public_key = var.admin_ssh_public_key
}

source_image_reference {

publisher = var.source_image_publisher
offer = var.source_image_offer
sku = var.source_image_sku
version = var.source_image_version
}

os_disk {
caching = var.operating_system_disk_cache
storage_account_type = var.operating_system_disk_type
}
}

# Network Interfaces for Linux VM

resource "azurerm_network_interface" "nic-linux" {
name = var.linux_machine_name
location = var.location
resource_group_name = var.resource_group_name
tags = var.tags

ip_configuration {
name = var.ip_configuration_name
# subnet_id = azurerm_subnet.subnet.id
subnet_id = var.subnet_id
private_ip_address_allocation = var.private_ip_address_allocation
public_ip_address_id = azurerm_public_ip.pip-linux.id
}
}
resource "azurerm_public_ip" "pip-linux" {
name = var.linux_machine_name
location = var.location
resource_group_name = var.resource_group_name
tags = var.tags

allocation_method = var.public_ip_allocation_method
sku = var.public_ip_sku
}

模块/虚拟机/variables.tf

# VM Name
variable "linux_machine_name" {
description = "Linux Virtual Machine Name - If left blank generated from metadata module"
type = string
default = ""
}

variable "resource_group_name" {
description = "Resource group name"
type = string
}

variable "location" {
description = "Azure region"
type = string
}

variable "tags" {
description = "tags to be applied to resources"
type = map(string)
}

# VM Size
variable "virtual_machine_size" {
description = "Instance size to be provisioned"
type = string
}

variable "admin_username" {
description = "names to be applied to resources"
type = string
}

variable "admin_ssh_public_key" {
description = "(Linux) Public SSH Key - Generated if left blank"
type = string
default = ""
sensitive = true
}

# Operating System
variable "source_image_publisher" {
description = "Operating System Publisher"
type = string
}

variable "source_image_offer" {
description = "Operating System Name"
type = string
}

variable "source_image_sku" {
description = "Operating System SKU"
type = string
}

variable "source_image_version" {
description = "Operating System Version"
type = string
default = "latest"
}

# Operating System Disk
variable "operating_system_disk_cache" {
description = "Type of caching to use on the OS disk - Options: None, ReadOnly or ReadWrite"
type = string
default = "ReadWrite"
}

variable "operating_system_disk_type" {
description = "Type of storage account to use with the OS disk - Options: Standard_LRS, StandardSSD_LRS or Premium_LRS"
type = string
default = "StandardSSD_LRS"
}

variable "ip_configuration_name" {
description = "ip configuration name"
type = string
default = ""
}

# Networking
variable "nic_id" {
type = list(string)
description = "ID of the nic"
}

variable "subnet_id" {
type = string
description = "ID of the subnet"
}

variable "private_ip_address_allocation" {
type = string
description = "Private ip allocation method"
}

variable "public_ip_allocation_method" {
type = string
description = "Public ip allocation method"
}


variable "public_ip_sku" {
description = "SKU to be used with this public IP - Basic or Standard"
type = string
default = "Standard"
}

模块/虚拟机/outputs.tf

output "nic_id" {
description = "ids of the vm nics provisoned."
value = azurerm_network_interface.nic-linux.id
}

新错误:

Error: Invalid value for module argument

│ on compute_lin_vm.tf line 10, in module "vm-ansiblecontroller":
│ 10: nic_id = module.vm-ansiblecontroller.nic_id

│ The given value is not suitable for child module variable "nic_id" defined at
│ .terraform/modules/vm-ansiblecontroller/virtual-machine/linux/variables.tf:83,1-18: list of string required.

最佳答案

您将 azurerm_network_interface 放入 azurerm_linux_virtual_machine 中。应该是:

resource "azurerm_linux_virtual_machine" "vm-linux" {

name = var.linux_machine_name
location = var.location
resource_group_name = var.resource_group_name
tags = var.tags

size = var.virtual_machine_size
admin_username = var.admin_username
disable_password_authentication = true
# network_interface_ids = [azurerm_network_interface.nic-linux.id]
network_interface_ids = var.nic_id

admin_ssh_key {
username = var.admin_username
public_key = var.admin_ssh_public_key
}

source_image_id = var.source_image_id
custom_data = var.custom_data

source_image_reference {

publisher = var.source_image_publisher
offer = var.source_image_offer
sku = var.source_image_sku
version = var.source_image_version
}

os_disk {
caching = var.operating_system_disk_cache
storage_account_type = var.operating_system_disk_type
}


}


# Network Interfaces for Linux VM

resource "azurerm_network_interface" "nic-linux" {
name = var.linux_machine_name
location = var.location
resource_group_name = var.resource_group_name
tags = var.tags

ip_configuration {
name = var.ip_configuration_name
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = var.private_ip_address_allocation
public_ip_address_id = azurerm_public_ip.pip-linux.id
}

}

关于azure - 模块中尚未声明托管资源 "azurerm_network_interface",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71854831/

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