creating a user with sudo priveledges,install JAVA using the Terraform Config file

February 7, 2018 240 views
DigitalOcean VPN Configuration Management Ubuntu 16.04

my file,but im stuck.can anyone help me!

droplet-terraform.tf

provider "digitalocean" {
  token = "4xxxxxxxxxxxxxxxxxx3cc264"
}
resource "digitalocean_droplet" "terraform" {
    image  = "ubuntu-16-04-x64"
    name   = "terrform-droplet"
    region = "blr1"
    size   = "2gb"
    monitoring = true
  provisioner "remote-exec" {
    inline = [
      "export PATH=$PATH:/usr/bin",
      # install java 8
      "sudo add-apt-repository ppa:webupd8team/java",
      "sudo apt-get update",
      "sudo apt-get -y install oracle-java8-installer",
      "export JAVA_HOME=/usr/lib/jvm/java-8-oracle",
      "sudo apt-get update",
      "echo $JAVA_HOME"
    ]
  }
}
2 comments
  • error
    digitaloceandroplet.terraform: Still creating... (5m20s elapsed)
    digitalocean
    droplet.terraform (remote-exec): Connecting to remote host via SSH...
    digitaloceandroplet.terraform (remote-exec): Host: 13x.xx.xx.xx4
    digitalocean
    droplet.terraform (remote-exec): User: root
    digitaloceandroplet.terraform (remote-exec): Password: false
    digitalocean
    droplet.terraform (remote-exec): Private key: false
    digitaloceandroplet.terraform (remote-exec): SSH Agent: false
    digitalocean
    droplet.terraform: Still creating... (5m30s elapsed)

    Error: Error applying plan:

    1 error(s) occurred:

    • digitalocean_droplet.terraform: 1 error(s) occurred:

    • timeout

    Terraform does not automatically rollback in the face of errors.
    Instead, your Terraform state file has been partially updated with
    any resources that successfully completed. Please address the error
    above and apply again to incrementally change your infrastructure.

3 Answers

Looking at the error output, you can see that Terraform is attempting to connect via SSH to run the commands you've specified in your provisioner block, but it is hanging and eventually timing-out. This is because you have not provided a way for it to authenticate with the server.

digitaloceandroplet.terraform (remote-exec): Password: false
digitaloceandroplet.terraform (remote-exec): Private key: false
digitaloceandroplet.terraform (remote-exec): SSH Agent: false

It first tries a password, but one is not provided. Next an SSH key, and finally it looks for a cached SSH key from you local systems SSH agent.

There are two pieces you've missed. First, you must provide an SSH key as part of the Droplet resource. In the example bellow I've specified it using it's finger print (generated with ssh-keygen -E md5 -lf ~/.ssh/id_rsa.pub) but it can also be its ID from the DigitalOcean API. Secondly, you must also provide a connection block. Here I've told it to use the SSH agent, but you can also specify a specific key by its local file path.

Here's a full example:

provider "digitalocean" {
  token = "4xxxxxxxxxxxxxxxxxx3cc264"
}

resource "digitalocean_droplet" "terraform" {
    image  = "ubuntu-16-04-x64"
    name   = "terrform-droplet"
    region = "blr1"
    size   = "2gb"
    monitoring = true
    ssh_keys = [
      "60:e6:a4:d7:ff:52:d3:ea:4b:c4:6f:j6:37:92:0d:a8"
    ]

  connection {
      user = "root"
      type = "ssh"
      agent = "true"
      timeout = "2m"
  }

  provisioner "remote-exec" {
    inline = [
      "export PATH=$PATH:/usr/bin",
      # install java 8
      "sudo add-apt-repository -y ppa:webupd8team/java",
      "sudo apt-get update",
      "sudo apt-get -y install oracle-java8-installer",
      "export JAVA_HOME=/usr/lib/jvm/java-8-oracle",
      "sudo apt-get update",
      "echo $JAVA_HOME"
    ]
  }
}

Also note that I added a -y to the add-apt-repository. Without it, it will hang waiting for user input.

For more info on using Terraform, check out:

Terraform is a tool for building and managing infrastructure in an organized way. It can be used to manage DigitalOcean droplets and DNS entries, in addition to a large variety of services offered by other providers. It is controlled via an easy to use command-line interface, and can run from your desktop or a remote server. Terraform works by reading configuration files that describe the components that make up your application environment or datacenter...
  • thanks for your reply,
    after setting agent = "true" , i get this error
    Error: Error applying plan:

    1 error(s) occurred:

    • digitalocean_droplet.terraform: 1 error(s) occurred:

    • SSH agent requested but SSHAUTHSOCK not-specified

    Terraform does not automatically rollback in the face of errors.
    Instead, your Terraform state file has been partially updated with
    any resources that successfully completed. Please address the error
    above and apply again to incrementally change your infrastructure.

Have another answer? Share your knowledge.