dmacvicar / terraform-provider-libvirt

Terraform provider to provision infrastructure with Linux's KVM using libvirt
Apache License 2.0
1.54k stars 457 forks source link

libvirt_network_dns_host_template not working ok #896

Open jserras opened 2 years ago

jserras commented 2 years ago

System Information

Linux distribution

Linux Mint 20.2

Terraform version

Terraform v1.0.9
on linux_amd64
+ provider registry.terraform.io/dmacvicar/libvirt v0.6.10
+ provider registry.terraform.io/hashicorp/template v2.2.0

Provider and libvirt versions

terraform-provider-libvirt -version
0.6.10

Checklist

Description of Issue/Question

I'm trying to create some additional A records on the created environment, and I'm using the description at the bottom of network.markdown but fails, I checked in the source code in data_source_libvirt_network.go and the example there is a bit different, and it fails in either construct, but the example in source seems more correct, anyway the error I get is

╷
│ Error: Unsupported argument
│ 
│   on main.tf line 46, in resource "libvirt_network" "debian":
│   46:    dns = [{
│ 
│ An argument named "dns" is not expected here. Did you mean to define a block of type "dns"?
╵

Setup

the data that fails to parse is the following from variables.tf:

variable "a_records" {
  description = "additional A records"
  type = list(object({
    hostname = string
    ip = string
  }))
  default = [
    {
      hostname = "teste1"
      ip = "192.168.130.11"
    },
    {
      hostname = "teste2"
      ip = "192.168.130.12"
    }
  ]
}

from main.tf

data "libvirt_network_dns_host_template" "records" {
  count = length(var.a_records)
  hostname = "${var.a_records[count.index].hostname}"
  ip = "${var.a_records[count.index].ip}"
}

resource "libvirt_network" "debian" {
...
   dns = [{
     hosts = [ "${flatten(data.libvirt_network_dns_host_template.records.*.rendered)}" ]
   }]
...
}

Steps to Reproduce Issue

(Include debug logs if possible and relevant).


Additional information:

Do you have SELinux or Apparmor/Firewall enabled? Some special configuration? no Have you tried to reproduce the issue without them enabled? yes static config like in network.markdown actually works ok

likeinu commented 2 years ago

Sorry for my English. I had the same problem. And i didn't find any solution with template and flatten. So i used dynamic block for hosts:

dns {
    enabled    = true
    local_only = true
    dynamic "hosts" {
      for_each = local.servers
      content {
        hostname = hosts.key
        ip       = hosts.value.ips[0]
      }
    }
  }

But you can't use dynamic block with count.

This is work for me:

dns {
    enabled    = true
    local_only = true
    dynamic "hosts" {
      for_each = var.a_records
      content {
        hostname = hosts.value.hostname
        ip       = hosts.value.ip
      }
    }
  }