azure 检查指定资源组是否存在后,无法在该资源组中创建新VNet

bq3bfh9z  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(239)

在这里,我使用数据块来检索Vnet的信息,如果它已经存在。如果Vnet不存在,则创建一个同名的新的。我已经正确地定义了一切。

data "azurerm_virtual_network" "existing_vnet" {
  name                = var.vnet_name
  resource_group_name = data.azurerm_resource_group.existing_rg.name
}

locals {
  vnet_exists = length(data.azurerm_virtual_network.existing_vnet) > 0
}

resource "azurerm_virtual_network" "my_vnet" {
  count = local.vnet_exists ? 0 : 1

  name                = var.vnet_name
  address_space       = var.address_space
  location            = data.azurerm_resource_group.existing_rg.location
  resource_group_name = data.azurerm_resource_group.existing_rg.name
}

但是在运行terraform apply之后,它显示了一个错误:

Error: Error: Virtual Network: (Name "axis_VN" / Resource Group "training") was not found
│ 
│   with data.azurerm_virtual_network.existing_vnet,
│   on main.tf line 24, in data "azurerm_virtual_network" "existing_vnet":
│   24: data "azurerm_virtual_network" "existing_vnet" {

有谁能帮我这个忙吗?
我期望VNet只在它不在资源组中退出时才创建

z6psavjg

z6psavjg1#

我试着用下面的代码来检查现有的资源

data "azurerm_virtual_network" "existing_vnet" {
  name                = "myVnet"
  resource_group_name = data.azurerm_resource_group.example.name
}

locals {
  vnet_exists = length(data.azurerm_virtual_network.existing_vnet) > 0
}

output "vnetcout" {
  value = local.vnet_exists
}

如果资源不存在,则会显示错误“resource not found”。

对于此外部数据源,可以使用:外部的|数据源|哈希公司/外部|Terraform Registry不检查预先存在的资源。
这里几乎可以使用任何编程语言。一个这样的例子,我试图创建VM,如果它不存在。

data "external" "vm_exists" {
      program = ["Powershell.exe", "C:\\Users\\xx\\checkexistence.ps1"]
    }

 output "vmexistsornot" {
      value = "${data.external.vm_exists.result.value}"
    }

**PowerShell代码:**Checkexistence.ps1

$rgName = "myrg"
    $vmName = "excpectedm"
    $vmExists = Get-AzVM -Name $vmName -ResourceGroupName $rgName -ErrorAction SilentlyContinue  
   # $vmexistsmsg="VM exists"
   # $vmnotexistsmsg="VM does not exist"
    if (!$vmExists) {
    Write-Output '{"value" :  "true" }'   
    } else {
    Write-Output '{"value" :  "false" }'
      
    }

最初只检查不存在:它必须返回true。

如果VM不存在,它将被创建。

resource "azurerm_network_interface" "example" {
  name                = "kaexample-nic"
 location            = data.azurerm_resource_group.example.location
  resource_group_name = data.azurerm_resource_group.example.name
  ip_configuration {
    name                          = "example-ip-config"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_virtual_network" "example" {
  name                = "kaexample-virtual-network"
  address_space       = ["10.0.0.0/16"]
  location            = data.azurerm_resource_group.example.location
  resource_group_name = data.azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "kkexample-subnet"
  address_prefixes     = ["10.0.0.0/24"]
  virtual_network_name = azurerm_virtual_network.example.name
  resource_group_name  = data.azurerm_resource_group.example.name
}

resource "azurerm_virtual_machine" "new_vm" {
  count = data.external.vm_exists.result.value == "true" ? 1 : 0

  name                = var.vm_name
  resource_group_name = var.resource_group_name
  location            = "eastus"
   vm_size               = "Standard_D4as_v5"
   network_interface_ids =  [azurerm_network_interface.example.id]

  

  storage_os_disk {
    name               = "my_os_storage_disk"
    caching            = "ReadWrite"
    create_option      = "FromImage"
    managed_disk_type  = "Standard_LRS"
  }

  os_profile {
    computer_name  = "myvm"
    admin_username = "azureuser"
    admin_password = "MyComplexPassword"
  }

  os_profile_windows_config {
  }

  # other configuration settings...

}

resource "azurerm_virtual_machine" "new_vm" {
  count = data.external.vm_exists.result.value == true ? 0 : 1
  name                = var.vm_name
  resource_group_name = var.resource_group_name
  location            = "eastus"
}

相关问题