How to use passphrase protected private ssh key in terraform?

Viewed 1828

I am following this tutorial, https://www.digitalocean.com/community/tutorials/how-to-use-ansible-with-terraform-for-configuration-management, to learn Terraform and Ansible.

When I execute terraform apply, it throws an error:

digitalocean_droplet.web[2]: Provisioning with 'remote-exec'...

Error: Failed to parse ssh private key: ssh: this private key is passphrase protected

Error: Error creating droplet: POST https://api.digitalocean.com/v2/droplets: 422 Failed to resolve VPC

  on droplets.tf line 1, in resource "digitalocean_droplet" "web":
   1: resource "digitalocean_droplet" "web" {

This is the code:

  provisioner "remote-exec" {
    inline = ["sudo apt update", "sudo apt install python3 -y", "echo DONE!"]

    connection {
      host        = self.ipv4_address
      type        = "ssh"
      user        = "root"
      private_key = file(var.pvt_key)
    }
  }

That private SSH key (~/.ssh/id_rsa) on my machine is passphrase protected. How do I use it?

1 Answers

You can add the desired ssh key to the ssh-agent with ssh-add ~/.ssh/id_rsa and then set the agent field in connection stanza to:

connection {
  host        = self.ipv4_address
  type        = "ssh"
  user        = "root"
  agent       = true
}
Related