Skip to content

Commit 13f2f6e

Browse files
committed
Make local_admin, base_volume and ssh_private_key optional
When trying to use this module with my own configuration, I found myself with some needs that think can be usefull to make this module more reusable: * Storage management: added two variables to use existing base images an not always uploading a base image * base_pool_name: define an existing pool name * base_volume_name: define an existing base volume name * Local user management: when this variable is empty do not create a local_user. Changed default value to make this a default behaviour. * Test vm connection: in almost all cases users would use their default private key, that may be loaded with ssh-agent. So default behaviour seems to be null value, but allow to define a different value
1 parent 8839153 commit 13f2f6e

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

main.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,11 @@ resource "libvirt_domain" "virt-machine" {
6666
"echo \"Virtual Machine \"$(hostname)\" is UP!\"",
6767
"date"
6868
]
69-
7069
connection {
7170
type = "ssh"
7271
user = var.ssh_admin
7372
host = self.network_interface.0.addresses.0
74-
private_key = file(var.ssh_private_key)
73+
private_key = var.ssh_private_key != null ? file(var.ssh_private_key): null
7574
timeout = "2m"
7675
}
7776
}

storage.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
resource "libvirt_volume" "base-volume-qcow2" {
2+
count = var.base_volume_name != null ? 0 : 1
23
name = format("${var.vm_hostname_prefix}-base.qcow2")
34
pool = var.pool
45
source = var.os_img_url
@@ -10,7 +11,10 @@ resource "libvirt_volume" "volume-qcow2" {
1011
name = format("${var.vm_hostname_prefix}%02d.qcow2", count.index + var.index_start)
1112
pool = var.pool
1213
size = 1024*1024*1024*var.system_volume
13-
base_volume_id = libvirt_volume.base-volume-qcow2.id
14+
base_volume_id = var.base_volume_name != null ? null : element(libvirt_volume.base-volume-qcow2, 0).id
15+
base_volume_name = var.base_volume_name
16+
base_volume_pool = var.base_pool_name
17+
1418
format = "qcow2"
1519
}
1620

@@ -20,4 +24,4 @@ resource "libvirt_cloudinit_disk" "commoninit" {
2024
user_data = data.template_cloudinit_config.init_config[count.index].rendered
2125
network_config = data.template_file.network_config[count.index].rendered
2226
pool = var.pool
23-
}
27+
}

templates/cloud_init.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ users:
2222
system: False
2323
ssh_authorized_keys: ${ssh_keys}
2424
shell: /bin/bash
25+
%{ if local_admin != "" }
2526
- name: ${local_admin}
2627
gecos: Local admin (no SSH)
2728
lock-passwd: false
2829
sudo: ALL=(ALL) ALL
2930
passwd: ${local_admin_passwd}
3031
shell: /bin/bash
32+
%{ endif }
3133

3234
write_files:
3335
- path: /etc/ssh/sshd_config

variables.tf

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ variable "os_img_url" {
33
default = "https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img"
44
}
55

6+
variable "base_volume_name" {
7+
description = "Name of base OS image"
8+
default = null
9+
}
10+
11+
variable "base_pool_name" {
12+
description = "Name of base OS image"
13+
default = null
14+
}
15+
16+
617
variable "autostart" {
718
description = "Autostart the domain"
819
default = true
@@ -111,7 +122,7 @@ variable "ssh_keys" {
111122

112123
variable "local_admin" {
113124
description = "Admin user without ssh access"
114-
default = "local-admin"
125+
default = ""
115126
}
116127

117128
variable "local_admin_passwd" {
@@ -126,5 +137,5 @@ variable "time_zone" {
126137

127138
variable "ssh_private_key" {
128139
description = "Private key for SSH connection test"
129-
default = "~/.ssh/id_ed25519"
140+
default = null
130141
}

0 commit comments

Comments
 (0)