ansible-debops-infrastructure

DebOps and Ansible scripts to manage my infrastructure
git clone git://git.erethon.com/ansible-debops-infrastructure
Log | Files | Refs

commit d59b93413fbb81e919d92f2ef9491f41c9712a05
parent ac0d80728b42afeae8878113f4a95cbd7f18b588
Author: Dionysis Grigoropoulos <dgrig@erethon.com>
Date:   Sun,  3 Nov 2019 16:21:47 +0200

libvirt_host: Make cloud init templates modular

Diffstat:
terraform/modules/libvirt_host/main.tf | 20+++++++++++---------
terraform/modules/libvirt_host/network_template.yml | 13+++++++++++++
terraform/modules/libvirt_host/user_template.yml | 3+++
terraform/modules/libvirt_host/vars.tf | 12+++++++++++-
4 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/terraform/modules/libvirt_host/main.tf b/terraform/modules/libvirt_host/main.tf @@ -8,20 +8,22 @@ resource "libvirt_volume" "volume" { } resource "random_pet" "random" { - count = (var.cloudinit_template != "" ? 1 : 0) + count = (var.cloudinit_user_template != "" ? 1 : 0) separator = "_" } -data "template_file" "user_data" { - count = (var.cloudinit_template != "" ? 1 : 0) - template = var.cloudinit_template -} - resource "libvirt_cloudinit_disk" "cloud_init" { - count = (var.cloudinit_template != "" ? 1 : 0) + count = (var.enable_cloud_init == true ? 1 : 0) name = "cloud-init-${random_pet.random[0].id}.iso" - user_data = data.template_file.user_data[count.index].rendered pool = var.storage_pool + user_data = templatefile("${path.module}/user_template.yml", { + extra_lines = var.cloudinit_user_template + }) + network_config = templatefile("${path.module}/network_template.yml", { + gateway = cidrhost(var.network_cidr, 1) + ip_address = "${cidrhost(var.network_cidr, var.network_host)}/${split("/", var.network_cidr)[1]}" + nameservers = var.cloudinit_nameservers + }) } resource "libvirt_domain" "libvirt_host" { @@ -29,7 +31,7 @@ resource "libvirt_domain" "libvirt_host" { memory = var.host_memory vcpu = var.host_vcpu autostart = var.host_autostart - cloudinit = (var.cloudinit_template != "" ? libvirt_cloudinit_disk.cloud_init[0].id : null) + cloudinit = (var.cloudinit_user_template != "" ? libvirt_cloudinit_disk.cloud_init[0].id : null) boot_device { dev = ["hd"] diff --git a/terraform/modules/libvirt_host/network_template.yml b/terraform/modules/libvirt_host/network_template.yml @@ -0,0 +1,12 @@ +version: 1 +config: + - type: physical + name: ens3 + subnets: + - type: static + address: ${ ip_address } + gateway: ${ gateway } + dns_nameservers: + %{ for nameserver in nameservers ~} + - ${nameserver} + %{ endfor }+ \ No newline at end of file diff --git a/terraform/modules/libvirt_host/user_template.yml b/terraform/modules/libvirt_host/user_template.yml @@ -0,0 +1,2 @@ +#cloud-config +${extra_lines}+ \ No newline at end of file diff --git a/terraform/modules/libvirt_host/vars.tf b/terraform/modules/libvirt_host/vars.tf @@ -56,11 +56,21 @@ variable "volume_source" { default = "" } -variable "cloudinit_template" { +variable "cloudinit_user_template" { type = string default = "" } +variable "enable_cloud_init" { + type = bool + default = false +} + +variable "cloudinit_nameservers" { + type = list + default = ["9.9.9.9"] +} + variable "base_volume_id" { type = string default = ""