Terraform从Packer

时间:2018-01-03 14:50:14

标签: terraform packer terraform-provider-azure

我使用Packer创建了一个自定义VM映像,现在我尝试使用Terraform基于此映像创建一个新VM,但我对如何设置.TF文件感到困惑。我可以创建其余的基础设施。

我认为我的打包器json文件创建了一个托管磁盘映像,但我不确定如何设置它并且无法在线查找示例。

我对infraastructure作为代码和一般的Azure ecco系统很新

main.tf

    resource "azurerm_managed_disk" "managedDisk" {
  name                 = "managed_disk_test1"
  location             = "northeurope"
  resource_group_name  = "${azurerm_resource_group.packer.name}"
  storage_account_type = "Standard_LRS"
  create_option        = "FromImage"
  image_reference_id   = "/subscriptions/33efe2dc-e7a0-4fb8-827d-8be939879420/resourceGroups/packerRG/providers/Microsoft.Compute/images/myPackerImage"
  disk_size_gb         = "1"
}


resource "azurerm_virtual_machine" "PackerVm_TEST" {
    name =  "${var.hostname}"
    location = "northeurope"
    resource_group_name   = "${azurerm_resource_group.packer.name}"
network_interface_ids = ["${azurerm_network_interface.packerNetInt_Test.id}"]
    vm_size = "Standard_D2s_v3"


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

        os_profile {
            computer_name  = "PackerVmTEST"
            admin_username = "packermakeradmin1"
            admin_password = "RMKRTest123"
          }
            os_profile_windows_config {
                enable_automatic_upgrades = "true"
                provision_vm_agent ="true"
            }

}

packer.json

{
  "builders": [{
    "type": "azure-arm",

    "client_id": "",
    "client_secret": "",
    "tenant_id": "",
    "subscription_id": "",
    "object_id": "",

    "managed_image_resource_group_name": "packerRG",
    "managed_image_name": "myPackerImage",

    "os_type": "Windows",
    "image_publisher": "MicrosoftWindowsServer",
    "image_offer": "WindowsServer",
    "image_sku": "2016-Datacenter",

    "communicator": "winrm",
    "winrm_use_ssl": "true",
    "winrm_insecure": "true",
    "winrm_timeout": "3m",
    "winrm_username": "packer",

    "azure_tags": {
        "dept": "Engineering",
        "task": "Image deployment"
    },

    "location": "northeurope",
    "vm_size": "Standard_DS2_v2"
  }],
  "provisioners": [{
    "type": "powershell",
    "inline": [
      "Add-WindowsFeature Web-Server",
      "if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}",
      "& $Env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /shutdown /quiet"
    ]
  }]
}

运行terraform时输出

* azurerm_virtual_machine.PackerVm_TEST: compute.VirtualMachinesClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="InvalidParameter" Message="Cannot specify user ima
ge overrides for a disk already defined in the specified image reference."

2 个答案:

答案 0 :(得分:0)

将托管磁盘连接到VM有两种方法。

要么删除azure_managed_disk ressource,要在azurerm_virtual_machine ressource中指定图像引用。托管磁盘将自动创建并附加到VM。

resource "azurerm_virtual_machine" "PackerVm_TEST" {
name =  "${var.hostname}"
location = "northeurope"
resource_group_name   = "${azurerm_resource_group.packer.name}"
network_interface_ids = ["${azurerm_network_interface.packerNetInt_Test.id}"]
vm_size = "Standard_D2s_v3"

storage_os_disk {
    name            = "FromPackerImageOsDisk"
    managed_disk_type = "Standard_LRS"
    caching           = "ReadWrite"
    create_option     = "FromImage"
}
storage_image_reference {
    id = "/subscriptions/33efe2dc-e7a0-4fb8-827d-8be939879420/resourceGroups/packerRG/providers/Microsoft.Compute/images/myPackerImage"
}

os_profile {
    computer_name  = "PackerVmTEST"
    admin_username = "packermakeradmin1"
    admin_password = "RMKRTest123"
  }
os_profile_windows_config {
    enable_automatic_upgrades = "true"
    provision_vm_agent ="true"
}

}

或者您在azurerm_virtual_machine资源中添加托管磁盘ID。

resource "azurerm_managed_disk" "managedDisk" {
  name                 = "managed_disk_test1"
  location             = "northeurope"
  resource_group_name  = "${azurerm_resource_group.packer.name}"
  storage_account_type = "Standard_LRS"
  create_option        = "FromImage"
  image_reference_id   = "/subscriptions/33efe2dc-e7a0-4fb8-827d-8be939879420/resourceGroups/packerRG/providers/Microsoft.Compute/images/myPackerImage"
  disk_size_gb         = "1"
}

resource "azurerm_virtual_machine" "PackerVm_TEST" {
    name =  "${var.hostname}"
    location = "northeurope"
    resource_group_name   = "${azurerm_resource_group.packer.name}"
    network_interface_ids = ["${azurerm_network_interface.packerNetInt_Test.id}"]
    vm_size = "Standard_D2s_v3"

    storage_os_disk {
        name            = "FromPackerImageOsDisk"
        managed_disk_id = "${azurerm_managed_disk.managedDisk.id}"
        managed_disk_type = "Standard_LRS"
        caching           = "ReadWrite"
        create_option     = "Attach"
    }
    os_profile {
        computer_name  = "PackerVmTEST"
        admin_username = "packermakeradmin1"
        admin_password = "RMKRTest123"
      }
    os_profile_windows_config {
        enable_automatic_upgrades = "true"
        provision_vm_agent ="true"
    }
}

来自terraform文档

  

managed_disk_id - (可选)指定要由id使用的现有受管磁盘。只能在create_option为Attach时使用。

答案 1 :(得分:0)

我已在.TF文件中添加了以下代码,并且工作正常。谢谢您的帮助!

storage_image_reference {
    id = "/subscriptions/33efe2dc-e7a0-4fb8-827d-8be939879420/resourceGroups/packerRG/providers/Microsoft.Compute/images/myPackerImage"
}