Question

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

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"
    ]
  }
}
Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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:

This comment has been deleted

This comment has been deleted