Specified hostname should not be prefixed
I would like to set a hostname for my machine via the name option in main.tf. Additionally I want to make use of prefixing in order to have my images not clashing with someone else's so I use a name_prefix as well. The issue is that I want my libvirt image and domain prefixed, but not the hostname. So maybe could we have a separate hostname setting and use this for only the hostname in case it is set, otherwise fall back to the prefixed name?
My configuration looks more or less like this and I want the actual hostname to be just hoag instead of renner-hoag:
provider "libvirt" {
...
}
module "base" {
source = "./modules/libvirt/base"
// optional parameters with defaults below
...
domain = "my.domain.com"
name_prefix = "renner-"
}
module "suma31pg" {
source = "./modules/libvirt/suse_manager"
base_configuration = "${module.base.configuration}"
name = "hoag"
version = "head"
image = "sles12sp2"
// see modules/libvirt/suse_manager/variables.tf for possible values
mac = "de:ad:be:ef:01:11"
}
I currently work around the problem by patching modules/libvirt/host/main.tf like this but I guess this won't work for everyone:
diff --git a/modules/libvirt/host/main.tf b/modules/libvirt/host/main.tf
index 180556b..ec38c29 100644
--- a/modules/libvirt/host/main.tf
+++ b/modules/libvirt/host/main.tf
@@ -46,7 +46,7 @@ resource "libvirt_domain" "domain" {
provisioner "file" {
content = <<EOF
-hostname: ${var.base_configuration["name_prefix"]}${var.name}${element(list("", "-${count.index + 1}"), signum(var.count - 1))}
+hostname: ${var.name}${element(list("", "-${count.index + 1}"), signum(var.count - 1))}
domain: ${var.base_configuration["domain"]}
use-avahi: ${var.base_configuration["use_avahi"]}
${var.grains}
@@ -69,7 +69,7 @@ output "configuration" {
value = "${
map(
"id", "${libvirt_domain.domain.0.id}",
- "hostname", "${var.base_configuration["name_prefix"]}${var.name}${element(list("", "-1"), signum(var.count - 1))}.${var.base_configuration["domain"]}"
+ "hostname", "${var.name}${element(list("", "-1"), signum(var.count - 1))}.${var.base_configuration["domain"]}"
)
}"
}
Reason why hostname is always prefixed is that by default we use Avahi, so in most cases this makes sense.
We could make the hostname overridable via an extra variable.