Skip to content

Commit 6db323c

Browse files
Update
1 parent c0fe4cc commit 6db323c

File tree

7 files changed

+85
-40
lines changed

7 files changed

+85
-40
lines changed

README.md

+26-11
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@ Terraform module for KVM/Libvirt Virtual Machine. This module will create a libv
2424
|vm_hostname_prefix|VM hostname prefix|vm
2525
|memory|RAM in MB|512
2626
|vcpu|Number of vCPUs|1
27+
|system_volume|System Volume size (GB)|10
2728
|dhcp|Use DHCP or Static IP settings|false
2829
|ip_address|"List of IP addresses|[ "192.168.123.1" ]
30+
|ip_nameserver|IP addresses of a nameserver|192.168.123.1
31+
|ip_gateway|IP addresses of a gateway|192.168.123.1
32+
|ssh_admin|Admin user with ssh access|ssh-admin
2933
|ssh_keys|List of public ssh keys| []
30-
|admin_passwd|Admin user password|password_example
31-
|ssh_username|User for SSH test|ssh-bot"
32-
|ssh_private_key|Private key for SSH test|~/.ssh/deployer_keys/id_ed25519
34+
|local_admin|Admin user without ssh access|local-admin
35+
|local_admin_passwd|Local admin user password|password_example
36+
|time_zone|Time Zone|UTC
37+
|ssh_private_key|Private key for SSH connection test|~/.ssh/deployer_keys/id_ed25519
3338

3439
**Cloud_init** configuration can be found in `modules/virt-machine/templates`.
3540

@@ -51,23 +56,33 @@ terraform {
5156
}
5257
5358
provider "libvirt" {
54-
uri = "qemu+ssh://[email protected].123.100/system"
59+
uri = "qemu+ssh://[email protected].165.100/system"
5560
}
5661
57-
module "k3s_nodes" {
62+
module "nodes" {
5863
source = "./modules/virt-machine"
64+
vm_hostname_prefix = "server"
5965
vm_count = 3
6066
memory = "2048"
6167
vcpu = 1
68+
system_volume = 20
6269
dhcp = false
63-
os_img_url = "file:///home/myuser/ubuntu-20.04-server-cloudimg-amd64.img"
6470
ip_address = [
65-
"192.168.123.101",
66-
"192.168.123.102",
67-
"192.168.123.103"
71+
"192.168.165.151",
72+
"192.168.165.152",
73+
"192.168.165.153"
6874
]
69-
admin_passwd = "$6$rounds=4-XXXXXXXXXXXXXXXXX-HASHED-PASSWORD"
70-
ssh_keys = [ "ssh-ed25519 XXXXXXXXXXXXXXXXX example", ]
75+
ip_gateway = "192.168.165.254"
76+
ip_nameserver = "192.168.165.104"
77+
local_admin = "local-admin"
78+
ssh_admin = "ci-user"
79+
ssh_private_key = "~/.ssh/id_ed25519"
80+
local_admin_passwd = "$6$rounds=4096$xxxxxxxxHASHEDxxxPASSWORD"
81+
ssh_keys = [
82+
"ssh-ed25519 AAAAxxxxxxxxxxxxSSHxxxKEY example",
83+
]
84+
time_zone = "CET"
85+
os_img_url = "file:///home/myuser/ubuntu-20.04-server-cloudimg-amd64.img"
7186
}
7287
7388
```

modules/virt-machine/cloud_init.tf

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ data "template_file" "network_config" {
33
template = file("${path.module}/templates/network_config_${var.dhcp == true ? "dhcp" : "static"}.tpl")
44
vars = {
55
ip_address = element(var.ip_address, count.index)
6+
ip_gateway = var.ip_gateway
7+
ip_nameserver = var.ip_nameserver
68
}
79
}
810

911
data "template_file" "init_config" {
1012
count = var.vm_count
1113
template = file("${path.module}/templates/cloud_init.tpl")
1214
vars = {
15+
ssh_admin = var.ssh_admin
1316
ssh_keys = local.all_keys
14-
admin_passwd = var.admin_passwd
15-
hostname = format("${var.vm_hostname_prefix}_%02d", count.index + 1)
17+
local_admin = var.local_admin
18+
local_admin_passwd = var.local_admin_passwd
19+
hostname = format("${var.vm_hostname_prefix}%02d", count.index + 1)
20+
time_zone = var.time_zone
1621
}
1722
}
1823

@@ -32,7 +37,7 @@ data "template_cloudinit_config" "init_config" {
3237
base64_encode = false
3338

3439
part {
35-
filename = format("init_%02d.cfg", count.index + 1)
40+
filename = format("init%02d.cfg", count.index + 1)
3641
content_type = "text/cloud-config"
3742
content = data.template_file.init_config[count.index].rendered
3843
}

modules/virt-machine/main.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ terraform {
1212

1313
resource "libvirt_domain" "virt-machine" {
1414
count = var.vm_count
15-
name = format("${var.vm_hostname_prefix}_%02d", count.index + 1)
15+
name = format("${var.vm_hostname_prefix}%02d", count.index + 1)
1616
memory = var.memory
1717
vcpu = var.vcpu
1818

@@ -21,7 +21,7 @@ resource "libvirt_domain" "virt-machine" {
2121
network_interface {
2222
bridge = "br0"
2323
wait_for_lease = false
24-
hostname = format("${var.vm_hostname_prefix}_%02d", count.index + 1)
24+
hostname = format("${var.vm_hostname_prefix}%02d", count.index + 1)
2525
}
2626

2727
console {
@@ -55,7 +55,7 @@ resource "libvirt_domain" "virt-machine" {
5555

5656
connection {
5757
type = "ssh"
58-
user = var.ssh_username
58+
user = var.ssh_admin
5959
host = var.ip_address[0]
6060
private_key = file(var.ssh_private_key)
6161
timeout = "2m"

modules/virt-machine/storage.tf

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ resource "libvirt_pool" "terra" {
44
path = var.libvirt_disk_path
55
}
66

7+
resource "libvirt_volume" "base-volume-qcow2" {
8+
name = format("${var.vm_hostname_prefix}-base.qcow2")
9+
pool = libvirt_pool.terra.name
10+
source = var.os_img_url
11+
format = "qcow2"
12+
}
13+
714
resource "libvirt_volume" "volume-qcow2" {
815
count = var.vm_count
9-
name = format("${var.vm_hostname_prefix}_%02d", count.index + 1)
16+
name = format("${var.vm_hostname_prefix}%02d.qcow2", count.index + 1)
1017
pool = libvirt_pool.terra.name
11-
source = var.os_img_url
18+
size = 1024*1024*1024*var.system_volume
19+
base_volume_id = libvirt_volume.base-volume-qcow2.id
1220
format = "qcow2"
1321
}
1422

modules/virt-machine/templates/cloud_init.tpl

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ packages:
99
fqdn: ${hostname}
1010

1111
users:
12-
- name: ssh-bot
12+
- name: ${ssh_admin}
1313
gecos: CI User
1414
lock-passwd: false
1515
sudo: ALL=(ALL) NOPASSWD:ALL
1616
system: False
1717
ssh_authorized_keys: ${ssh_keys}
1818
shell: /bin/bash
19-
- name: localadmin
19+
- name: ${local_admin}
2020
gecos: Local admin (no SSH)
2121
lock-passwd: false
2222
sudo: ALL=(ALL) ALL
23-
passwd: ${admin_passwd}
23+
passwd: ${local_admin_passwd}
2424
shell: /bin/bash
2525

2626
write_files:
@@ -55,8 +55,7 @@ write_files:
5555
AcceptEnv LANG LC_*
5656
Subsystem sftp /usr/lib/openssh/sftp-server
5757
UsePAM yes
58-
AllowUsers ssh-bot
59-
DenyUsers localadmin
58+
AllowUsers ${ssh_admin}
6059

6160
growpart:
6261
mode: auto
@@ -65,5 +64,5 @@ growpart:
6564

6665
resize_rootfs: true
6766

68-
timezone: CET
67+
timezone: ${time_zone}
6968

modules/virt-machine/templates/network_config_static.tpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ ethernets:
33
ens3:
44
dhcp4: no
55
addresses: [${ip_address}/24]
6-
gateway4: 192.168.123.254
6+
gateway4: ${ip_gateway}
77
nameservers:
88
addresses:
9-
- 192.168.123.254
9+
- ${ip_nameserver}
1010
- 8.8.8.8

modules/virt-machine/variables.tf

+31-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
#### Storage ####
2-
31
variable "libvirt_disk_path" {
42
description = "Path to libvirt Disk pool"
53
default = "/mnt/terra"
64
}
75

8-
#### Virtual Machine ####
9-
106
variable "os_img_url" {
117
description = "URL to the OS image"
128
default = "https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img"
@@ -36,10 +32,14 @@ variable "memory" {
3632

3733
variable "vcpu" {
3834
description = "Number of vCPUs"
39-
type = string
4035
default = 1
4136
}
4237

38+
variable "system_volume" {
39+
description = "System Volume size (GB)"
40+
default = 10
41+
}
42+
4343
variable "dhcp" {
4444
description = "Use DHCP or Static IP settings"
4545
type = bool
@@ -52,25 +52,43 @@ variable "ip_address" {
5252
default = [ "192.168.123.1" ]
5353
}
5454

55+
variable "ip_nameserver" {
56+
description = "IP addresses of a nameserver"
57+
default = "192.168.123.1"
58+
}
59+
60+
variable "ip_gateway" {
61+
description = "IP addresses of a gateway"
62+
default = "192.168.123.1"
63+
}
64+
65+
variable "ssh_admin" {
66+
description = "Admin user with ssh access"
67+
default = "ssh-admin"
68+
}
69+
5570
variable "ssh_keys" {
5671
description = "List of public ssh keys"
5772
type = list(string)
5873
default = []
5974
}
6075

61-
variable "admin_passwd" {
62-
description = "Admin user password"
63-
default = "password_example"
76+
variable "local_admin" {
77+
description = "Admin user without ssh access"
78+
default = "local-admin"
6479
}
6580

66-
#### Connection test (Optional) ###
81+
variable "local_admin_passwd" {
82+
description = "Local admin user password"
83+
default = "password_example"
84+
}
6785

68-
variable "ssh_username" {
69-
description = "User for SSH test"
70-
default = "ssh-user"
86+
variable "time_zone" {
87+
description = "Time Zone"
88+
default = "UTC"
7189
}
7290

7391
variable "ssh_private_key" {
74-
description = "Private key for SSH test"
92+
description = "Private key for SSH connection test"
7593
default = "~/.ssh/id_ed25519"
7694
}

0 commit comments

Comments
 (0)