I am trying to generate a file that looks like below via a terraform file template. I keep getting errors. Or if someone has terraform code that will generate a dhcpd.conf file with DHCP Reservations that works too. Thanks in advance
Call to template file:
dhcpd_file_conf = base64encode(templatefile("${path.module}/templates/dhcpd.tmpl", {
dhcp_nodes = var.dhcp_nodes
}))
Template file:
%{ for host,ip,mac in dhcp_nodes ~}
host ${host} {
hardware ethernet ${mac};
fixed-address ${ip};
}
%{ endfor ~}
Here is the error I got:
| var.dhcp_nodes is map of object with 3 elements
Call to function "templatefile" failed: new-lb/templates/dhcpd.tmpl:20,23-28:
Invalid template interpolation value; Cannot include the given value in a
string template: string required., and 5 other diagnostic(s).
What I want in the Output file:
host compute-0 {
hardware ethernet 00:00:00:00:00:00;
fixed-address 172.16.0.5;
}
host compute-1 {
hardware ethernet 00:00:00:00:00:01;
fixed-address 172.16.0.6;
}
host compute-2 {
hardware ethernet 00:00:00:00:00:02;
fixed-address 172.16.0.7;
}
with this declaration:
variable "compute_nodes" {
type = map(object({
host = string
ip = string
mac = string
}))
}
and these values:
compute_nodes = {
compute-0 = {
host = "compute-0"
ip = "172.16.0.5"
mac = "00:00:00:00:00:01"
}
compute-1 = {
host = "compute-1"
ip = "172.16.0.6"
mac = "00:00:00:00:00:02"
}
compute-2 = {
host = "compute-2"
ip = "172.16.0.7"
mac = "00:00:00:00:00:03"
}
}
This is the template file…
And I’m looking to create this output. I can’t figure out how to iterate thru a list of servers that each have 3 parms.
host compute-0 {
hardware ethernet 00:00:00:00:00:00;
fixed-address 172.16.0.5;
}
host compute-1 {
hardware ethernet 00:00:00:00:00:01;
fixed-address 172.16.0.6;
}
host compute-2 {
hardware ethernet 00:00:00:00:00:02;
fixed-address 172.16.0.7;
}
The input to the template file is only a single key/value pair.
Could you try this template?
%{ for key, value in dhcp_nodes ~}
host ${value.host} {
hardware ethernet ${value.mac};
fixed-address ${value.ip};
}
%{ endfor ~}
Thats awesome, I guess I tried about every combination but that. It worked great! Now that I see that example, I see how it works. Thanks so much!