Skip to content

Commit

Permalink
Add more cloudinit custom options
Browse files Browse the repository at this point in the history
  • Loading branch information
x86-39 committed Sep 25, 2023
1 parent f1aff2d commit dcb5257
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
27 changes: 22 additions & 5 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ data "template_file" "cloudinit_user_data" {
template = <<-EOT
#cloud-config
%{if var.cloudinit_use_user_data == true~}
# From diademiemi/terraform-libvirt-vm
hostname: ${var.hostname}
fqdn: ${var.hostname}.${var.domain}
prefer_fqdn_over_hostname: true
Expand All @@ -42,16 +46,24 @@ chpasswd:
%{if var.allow_root_ssh_pwauth != null && var.allow_root_ssh_pwauth == true}
bootcmd:
- 'echo "PermitRootLogin yes" >> /etc/ssh/sshd_config.d/99-allow-root-ssh-pwauth.conf'
%{endif}
%{endif~}
# Custom cloud-init
${coalesce(var.cloudinit_custom_user_data, "")}`
EOT

}

data "template_file" "cloudinit_network_data" {
template = <<-EOT
%{if var.cloudinit_use_network_data == true~}
version: 2
ethernets:
%{for interface in var.network_interfaces ~}
%{for interface in var.network_interfaces~}
${interface.name~}:
%{if interface.dhcp == null~}
dhcp4: true
Expand Down Expand Up @@ -87,14 +99,19 @@ ethernets:
%{endif~}
%{endfor~}
%{endif~}
# Custom cloud-init
${coalesce(var.cloudinit_custom_network_data, "")}`
EOT
}

resource "libvirt_cloudinit_disk" "init_disk" {
name = "${var.hostname}_cloudinit"
pool = var.libvirt_pool

user_data = data.template_file.cloudinit_user_data.rendered
user_data = data.template_file.cloudinit_user_data.rendered
network_config = data.template_file.cloudinit_network_data.rendered
}

Expand Down Expand Up @@ -157,8 +174,8 @@ resource "ansible_host" "default" {
groups = concat(var.ansible_groups, [lower(replace(var.domain, ".", "_"))])

variables = {
ansible_host = coalesce(var.ansible_host, split("/", var.network_interfaces[0].ip).0, var.domain != "" ? "${var.hostname}.${var.domain}" : var.hostname)
ansible_user = var.ansible_user
ansible_host = coalesce(var.ansible_host, split("/", var.network_interfaces[0].ip).0, var.domain != "" ? "${var.hostname}.${var.domain}" : var.hostname)
ansible_user = var.ansible_user
ansible_ssh_pass = var.ansible_ssh_pass
}
}
38 changes: 29 additions & 9 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,40 @@ variable "libvirt_external_interface" {
default = null
}

variable "cloudinit_use_user_data" {
type = bool
default = true
}

variable "cloudinit_use_network_data" {
type = bool
default = true
}

variable "cloudinit_custom_user_data" {
type = string
default = ""
}

variable "cloudinit_custom_network_data" {
type = string
default = ""
}

variable "network_interfaces" {
type = list(object({
name = optional(string)
network_id = optional(string)
network_name = optional(string)
macvtap = optional(string)
hostname = optional(string)
name = optional(string)
network_id = optional(string)
network_name = optional(string)
macvtap = optional(string)
hostname = optional(string)
wait_for_lease = optional(bool)

dhcp = optional(bool)
ip = optional(string)
gateway = optional(string)
dhcp = optional(bool)
ip = optional(string)
gateway = optional(string)
nameservers = optional(list(string))
mac = optional(string)
mac = optional(string)

additional_routes = optional(list(object({
network = string
Expand Down

0 comments on commit dcb5257

Please sign in to comment.