微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用HCL / Terraform在azurerm_virtual_machine块中出现“ admin_ssh_key”问题

如何解决使用HCL / Terraform在azurerm_virtual_machine块中出现“ admin_ssh_key”问题

我收到以下错误

Error: Unsupported block type
front.tf line 164,in resource "azurerm_virtual_machine" "FrontEndVirtualMachines":
164:  admin_ssh_key {
Blocks of type "admin_ssh_key" are not expected here.

我正在关注以下示例:

https://docs.microsoft.com/en-us/azure/developer/terraform/create-linux-virtual-machine-with-infrastructure

我在文档中确实注意到的一件事,“创建虚拟机”部分使用:

output "tls_private_key" { 
  value = "tls_private_key.Ssh.private_key_pem"
}

“完整terraform脚本”部分使用带花括号的$:

output "tls_private_key" { 
  value = "${tls_private_key.Ssh.private_key_pem}"
}

我尝试了两种方式,都遇到了同样的问题。

这是我的代码

#Create (and display) an SSH key
resource "tls_private_key" "Ssh" {
    algorithm = "RSA"
    rsa_bits = 4096
}
output "tls_private_key" { value = "${tls_private_key.Ssh.private_key_pem}" }

#Create 2 front end web servers VMs
resource "azurerm_virtual_machine" "FrontEndVirtualMachines" {
    count = 2
    name = "vmFront${count.index}"
    location = azurerm_resource_group.PWSDevResourceGroup.location
    availability_set_id = azurerm_availability_set.AvailibilitySet.id
    resource_group_name = azurerm_resource_group.PWSDevResourceGroup.name
    network_interface_ids = [element(azurerm_network_interface.FrontNetworkInterface.*.id,count.index)]
    vm_size = "A4_V2"

 # Uncomment this line to delete the OS disk automatically when deleting the VM
 # delete_os_disk_on_termination = true

 # Uncomment this line to delete the data disks automatically when deleting the VM
 # delete_data_disks_on_termination = true

#Define OS Image
 storage_image_reference {
    publisher = "MicrosoftwindowsServer"
    offer = "WindowsServer"
    sku = "2016-Datacenter"
    version = "latest"
   }

#Define OS disk
storage_os_disk {
    name = "FrontOSdisk"
    caching = "ReadWrite"
    create_option = "FromImage"
    managed_disk_type = "Standard_lrs"
   }

#Define Secondary Storage disk
storage_data_disk {
    name = "FrontDatadisk"
    create_option = "Empty"
    lun = 1
    disk_size_gb = "64"
    managed_disk_type = "Standard_lrs"
   }

#Define Admin Profile
 os_profile {
    computer_name = "Front"
    admin_username = "#######"
    admin_password = "#######"
    }

admin_ssh_key {
    username       = "azureuser"
    public_key     = tls_private_key.Ssh.public_key_openssh
    }

boot_diagnostics {
    enabled = true
    storage_uri = azurerm_storage_account.PWSDevStorageAcct.primary_blob_endpoint
    }

tags = {
    environment = "Front-End"
    }
}

解决方法

Azure文档适用于azurerm_linux_virtual_machine资源类型。您正在使用azurerm_virtual_machine。此资源已拆分为单独的linux和Windows版本。它在2.x中仍受支持,但功能已冻结。另外,您正在尝试构建不支持添加admin_ssh_key的Windows服务器。您可能要使用azurerm_windows_virtual_machine。传递凭据

  admin_username      = "adminuser"
  admin_password      = "P@$$w0rd1234!"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。