Terraform:创建多个实例和DNS记录。仅为第一个液滴编号01创建一个DNS记录

时间:2018-11-22 13:16:30

标签: dns digital-ocean terraform

我想创建多个实例和DNS记录。但是只为第一个液滴编号01创建一个DNS记录。

有人可以帮我吗?我想要所有小滴的DNS记录。

[{Droplets[1] DNS

resource "digitalocean_droplet" "puppet" {
  count = "${var.numberofservers}"
  name = "${var.servername}-${format("%02d", count.index+1)}"
  image = "${var.ubuntu_18}"
  region = "${var.do_fra1}"
  size = "${var.s_1vcpu_1gb}"
  ipv6 = true
  backups = false
  monitoring = true
  private_networking = true
  tags = ["${digitalocean_tag.puppet.name}"]
  ssh_keys = [
    "${var.ssh_fingerprint}"
    ]

DNS

# Create DNS zone
resource "digitalocean_domain" "puppet_dns" {
  name         = "test-${var.domain_name}"
}

# Add A records to the domain
resource "digitalocean_record" "pupet_a" {
  //domain = "${var.servername}" // fetching the domain name created above.
  domain = "${digitalocean_domain.puppet_dns.name}"
  type   = "A"
  name   = "${element(digitalocean_droplet.puppet.*.name, count.index)}"
  value  = "${element(digitalocean_droplet.puppet.*.ipv4_address, count.index)}"
}

1 个答案:

答案 0 :(得分:1)

您还需要遍历为DNS记录创建的液滴:

resource "digitalocean_record" "pupet_a" {
  count  = "${var.numberofservers}"
  domain = "${digitalocean_domain.puppet_dns.name}"
  type   = "A"
  name   = "${element(digitalocean_droplet.puppet.*.name, count.index)}"
  value  = "${element(digitalocean_droplet.puppet.*.ipv4_address, count.index)}"
}

现在,您只是在创建一个记录资源,并将第一个Droplet名称和IP地址传递到其中。