diff --git a/README.md b/README.md index 9c445b8..eb473c9 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ Terraform module for KVM/Libvirt Virtual Machine. This module will create a KVM | Parameter | Description | Default value |-----------------|-----|----- |os_img_url|URL to the OS image|https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img +|base_pool_name|When defined it will be used in combination with `base_volume_name` to create root volume as linked clone from this pool name|null +|base_volume_name|When defined it will be used in combination with `base_pool_name` to create root volume as linked clone from this pool/vol name. Defining this variable will disable downloading `os_img_url` and creating a base volume| null |autostart| Autostart the Domain| true |vm_count|Number of VMs| 1 |index_start|From where the index start| 1 @@ -43,10 +45,10 @@ Terraform module for KVM/Libvirt Virtual Machine. This module will create a KVM |ip_gateway|Static IP addresses of a gateway|192.168.123.1 |ssh_admin|Admin user with ssh access|ssh-admin |ssh_keys|List of public ssh keys| [] -|local_admin|Admin user without ssh access|local-admin +|local_admin|Admin user without ssh access|"" |local_admin_passwd|Local admin user password|password_example |time_zone|Time Zone|UTC -|ssh_private_key|Private key for SSH connection test|~/.ssh/id_ed25519 +|ssh_private_key|Private key for SSH connection test|null ## Example diff --git a/main.tf b/main.tf index f7b1833..2983853 100644 --- a/main.tf +++ b/main.tf @@ -66,12 +66,11 @@ resource "libvirt_domain" "virt-machine" { "echo \"Virtual Machine \"$(hostname)\" is UP!\"", "date" ] - connection { type = "ssh" user = var.ssh_admin host = self.network_interface.0.addresses.0 - private_key = file(var.ssh_private_key) + private_key = var.ssh_private_key != null ? file(var.ssh_private_key): null timeout = "2m" } } diff --git a/storage.tf b/storage.tf index 4ff45a1..4725b1a 100644 --- a/storage.tf +++ b/storage.tf @@ -1,4 +1,5 @@ resource "libvirt_volume" "base-volume-qcow2" { + count = var.base_volume_name != null ? 0 : 1 name = format("${var.vm_hostname_prefix}-base.qcow2") pool = var.pool source = var.os_img_url @@ -10,7 +11,10 @@ resource "libvirt_volume" "volume-qcow2" { name = format("${var.vm_hostname_prefix}%02d.qcow2", count.index + var.index_start) pool = var.pool size = 1024*1024*1024*var.system_volume - base_volume_id = libvirt_volume.base-volume-qcow2.id + base_volume_id = var.base_volume_name != null ? null : element(libvirt_volume.base-volume-qcow2, 0).id + base_volume_name = var.base_volume_name + base_volume_pool = var.base_pool_name + format = "qcow2" } @@ -20,4 +24,4 @@ resource "libvirt_cloudinit_disk" "commoninit" { user_data = data.template_cloudinit_config.init_config[count.index].rendered network_config = data.template_file.network_config[count.index].rendered pool = var.pool -} \ No newline at end of file +} diff --git a/templates/cloud_init.tpl b/templates/cloud_init.tpl index 86d74b7..3c48ed6 100644 --- a/templates/cloud_init.tpl +++ b/templates/cloud_init.tpl @@ -22,12 +22,14 @@ users: system: False ssh_authorized_keys: ${ssh_keys} shell: /bin/bash +%{ if local_admin != "" } - name: ${local_admin} gecos: Local admin (no SSH) lock-passwd: false sudo: ALL=(ALL) ALL passwd: ${local_admin_passwd} shell: /bin/bash +%{ endif } write_files: - path: /etc/ssh/sshd_config diff --git a/variables.tf b/variables.tf index 69c0efd..555ce40 100644 --- a/variables.tf +++ b/variables.tf @@ -3,6 +3,17 @@ variable "os_img_url" { default = "https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img" } +variable "base_volume_name" { + description = "Name of base OS image" + default = null +} + +variable "base_pool_name" { + description = "Name of base OS image" + default = null +} + + variable "autostart" { description = "Autostart the domain" default = true @@ -111,7 +122,7 @@ variable "ssh_keys" { variable "local_admin" { description = "Admin user without ssh access" - default = "local-admin" + default = "" } variable "local_admin_passwd" { @@ -126,5 +137,5 @@ variable "time_zone" { variable "ssh_private_key" { description = "Private key for SSH connection test" - default = "~/.ssh/id_ed25519" + default = null }