如何通过terraform将本地文件复制到Azure VM?

时间:2019-09-24 08:06:39

标签: azure terraform

我正在尝试使用terraform在Azure中创建VM并将文件从本地计算机复制到Azure上的远程虚拟机。


    os_profile {
    computer_name = "pdemo"
    admin_username = "ubuntu"
  }
  os_profile_linux_config {
    disable_password_authentication = true
    ssh_keys {
      key_data = "ssh-rsa ********************************** "
      path = "/home/ubuntu/.ssh/authorized_keys"
    }
  }
  provisioner "file" {
    connection {
      type = "ssh"
      user = "ubuntu"
      host = azurerm_public_ip.terraform-PUBLIC-IP.ip_address
      private_key = file("/home/ubuntu/.ssh/id_rsa")
    }
    source = "/home/ubuntu/.ssh/terraform.pub"
    destination = "/home/ubuntu/.ssh/terraform.pub"
  }
}

出现错误:

azurerm_virtual_machine.terraform-app-VM: Still creating... [1m30s elapsed]
azurerm_virtual_machine.terraform-app-VM: Still creating... [1m40s elapsed]
azurerm_virtual_machine.terraform-app-VM: Still creating... [6m20s elapsed]
azurerm_virtual_machine.terraform-app-VM: Still creating... [6m30s elapsed]

Error: timeout - last error: SSH authentication failed (ubuntu@:22): ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain


1 个答案:

答案 0 :(得分:0)

请检查您使用的私钥,因为错误显示身份验证问题。

还添加agent=false,如下所示:

provisioner "file" {
    connection {
      type = "ssh"
      user = "ubuntu"
      host = azurerm_public_ip.terraform-PUBLIC-IP.ip_address
      private_key = file("/home/ubuntu/.ssh/id_rsa")
      agent    = false
      timeout  = "10m"
    }
    source = "/home/ubuntu/.ssh/terraform.pub"
    destination = "/home/ubuntu/.ssh/terraform.pub"
  }