Failed to read ssh private key: no key found

Hello community out there,

I am very new here even new to terraform.

I am provising vm for Proxmox .

I am getting below error:

 Error: remote-exec provisioner error
│
│   with proxmox_vm_qemu.test_server[0],
│   on main.tf line 84, in resource "proxmox_vm_qemu" "test_server":
│   84:   provisioner "remote-exec" {
│
│ Failed to read ssh private key: no key found

at end main.tf file:


  # the ${count.index + 1} thing appends text to the end of the ip address
  # in this case, since we are only adding a single VM, the IP will
  # be 10.98.1.91 since count.index starts at 0. this is how you can create
  # multiple VMs and have an IP assigned to each (.91, .92, .93, etc.)
  ipconfig0 = "ip=192.168.0.49/24,gw=192.98.0.1"

  # sshkeys set using variables. the variable contains the text of the key.
  sshkeys = <<EOF
  ${var.ssh_key}
  EOF

# Configuring connection details

    connection {
      host = "192.168.0.49"
      type = "ssh"
      port = 22
      user = "mgms-admin"
      private_key = file("/home/mgms-admin/terraform/.ssh/id_rsa.pub")
      timeout     = "1m"
    }

# Remotely executing a command on the server

  provisioner "remote-exec" {
    inline = ["sudo apt -y install nginx"]

  }


}

vars.tf

variable "ssh_key" {
    default = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQSxEbJ>

}

variable "proxmox_host" {
    default = "homeserver"
}

variable "template_name" {
    default = "ubuntu-2004-cloudinit-template"
}

ssh key location:

admin@terraform:~/terraform$ cat .ssh/id_rsa
id_rsa      id_rsa.pub

I am able to create vm but i would like to provision vm like install list of packages which is defined in remote-exec provisioner, or do we have some other smart way to install via script like .sh file and how to define this in main.tf.

thanks in advance!

Hi @syed.europa,

I believe the meaning of this error message is that the string you provided to private_key doesn’t include a private key block.

The filename you specified, id_rsa.pub, is typically used for public keys rather than private keys, so I expect what’s happening here is that Terraform is trying to find a private key in your public key file and failing because that file only contains public key information.

You mentioned that you also have an id_rsa (no .pub suffix) file, which is the typical convention for an SSH private key and so I expect that would be the correct file to use.

Thanks @apparentlymart - Thank you for pointing out.

It works, could you please guide me how we can install script which contains all list of commmands.
Thanks !

Hi @syed.europa,

There are two typical ways to run extra software on virtual machines declared by Terraform:

  1. As a separate step before running Terraform, build a custom machine image which contains the software and configuration you want to use. Then in Terraform you just select that custom image, and so the software will automatically be available once the system boots.
  2. Use a VM image which contains the software CloudInit, which you can then use in conjunction with features provided by your cloud platform to pass configuration and setup scripts into the virtual machine from your Terraform configuration.

The details of these vary depending on your cloud provider and I’m not personally familiar with proxmox so I can’t give direct guidance for it, but I do see that there is a proxmox-specific guide to CloudInit which may be helpful.

Provisioners are a last resort, and so are not a suitable solution in your case where your chosen platform allows both custom machine images and CloudInit support.

1 Like

thanks a lot!
I will look into it.