I create multiple Digital Ocean Droplet resource (VM) using Terraform(TF) and set every VM random string password from TF common resource by remote-exec provisioner.
resource "digitalocean_droplet" "testvm" {
count = var.count_of_droplets
image = "ubuntu-20-04-x64"
name = "testvm-${count.index}"
region = "nyc3"
size = "s-1vcpu-1gb"
ssh_keys = [data.digitalocean_ssh_keys.keys.ssh_keys[0].id]
provisioner "remote-exec" {
connection {
type = "ssh"
user = "root"
private_key = file("${var.my_ssh_private_key}")
host = self.ipv4_address
}
inline = [
"echo '${var.os_user}:${random_string.password[count.index].result}' | sudo chpasswd",
"sed -i '/PermitRootLogin/c PermitRootLogin yes' /etc/ssh/sshd_config",
"systemctl restart sshd"
]
}
tags = ["dev"]
}
and random password generator:
resource "random_string" "password" {
count = var.count_of_droplets
length = 16
special = true
override_special = "_%@"
}
I print public ip for every created VM with name of VM by Terraform output:
output "droplet_ip_addresses" {
value = {
for droplet in digitalocean_droplet.testvm :
droplet.name => droplet.ipv4_address
}
}
output:
droplet_ip_addresses = {
- testvm-0 = "1.1.1.1"
- testvm-1 = "2.2.2.2"
}
How i can add to above output generated password? Like:
droplets_data = {
- testvm-0 = "1.1.1.1" = "5j1dYBZzqDd30yhJ"
- testvm-1 = "2.2.2.2" = "6R%wj4zebwd9FiAt"
}