Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.7 kB
1
Indexable
Never
resource "digitalocean_droplet" "web" {
  image  = "ubuntu-18-04-x64"
  name   = "tf-02"
  size   = "s-1vcpu-1gb"
  password = random_password.password[count.index].result
  ssh_keys = [digitalocean_ssh_key.default.fingerprint, data.digitalocean_ssh_key.rebrain_key.fingerprint]
  tags = [digitalocean_tag.task_tag.id, digitalocean_tag.email_tag.id]
  count = var.vps_count
  connection {
    type = "ssh"
    user = "root"
    host = self.ipv4_address
    private_key = file(var.my_private_key)
  }
  provisioner "remote-exec" {
    inline = [
      " echo 'root:${local.password}' | chpasswd ",
      " sed -i -E 's/^PasswordAuthentication .*/PasswordAuthentication yes/g' /etc/ssh/sshd_config &&",
      " systemctl restart ssh.service"
    ]
  }
}

resource "aws_route53_record" "do_record" {
  zone_id = data.aws_route53_zone.rebrain.zone_id
  name    = "${var.email_tag}-${count.index}"
  type    = "A"
  ttl     = 300
  records = [element (local.vps_ip, count.index)]
  count   = var.vps_count
}
resource "digitalocean_ssh_key" "default" {
  name       = "do_key"
  public_key = var.my_key
}

resource "digitalocean_tag" "task_tag" {
  name = var.task_tag
}
resource "digitalocean_tag" "email_tag" {
  name = var.email_tag
}
resource "random_password" "password" {
  count = var.vps_count
  length           = 16
  special          = true
  override_special = "!#$%&*()-_=+[]{}<>:?"
}
data "digitalocean_ssh_key" "rebrain_key" {
  name = "REBRAIN.SSH.PUB.KEY"
}

data "aws_route53_zone" "rebrain" {
  name = "devops.rebrain.srwx.net"
}

locals  {
    vps_ip = digitalocean_droplet.web[*].ipv4_address
    password = digitalocean_droplet.web[*].password
}