Skip to content

Commit 36b1662

Browse files
Merge pull request #22 from MonolithProjects/develop
Develop to Main
2 parents 9ea3f36 + 9113cf8 commit 36b1662

12 files changed

+154
-117
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
tests
2+
.terraform*

Diff for: README.md

+11-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Terraform module for KVM/Libvirt Virtual Machine. This module will create a KVM Virtual Machine(s), configure it using Cloud Init and test the ssh connection. This module is using [dmacvicar/libvirt](https://github.com/dmacvicar/terraform-provider-libvirt) Terraform provider.
88

9-
## What this module provides
9+
## What it provides
1010

1111
- creates one or more VMs
1212
- one NIC per domain, connected to the network using the **bridge interface**
@@ -17,15 +17,16 @@ Terraform module for KVM/Libvirt Virtual Machine. This module will create a KVM
1717

1818
## Tested on
1919

20-
- Ubuntu 20.04 TLS
20+
- Ubuntu 20.04 TLS Cloud Image
21+
- Ubuntu 22.04 TLS Cloud Image
2122

2223
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
2324
## Requirements
2425

2526
| Name | Version |
2627
|------|---------|
27-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
28-
| <a name="requirement_libvirt"></a> [libvirt](#requirement\_libvirt) | >=0.6.9 |
28+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
29+
| <a name="requirement_libvirt"></a> [libvirt](#requirement\_libvirt) | >= 0.7.0 |
2930

3031
## Modules
3132

@@ -39,42 +40,38 @@ No modules.
3940
| [libvirt_domain.virt-machine](https://registry.terraform.io/providers/dmacvicar/libvirt/latest/docs/resources/domain) | resource |
4041
| [libvirt_volume.base-volume-qcow2](https://registry.terraform.io/providers/dmacvicar/libvirt/latest/docs/resources/volume) | resource |
4142
| [libvirt_volume.volume-qcow2](https://registry.terraform.io/providers/dmacvicar/libvirt/latest/docs/resources/volume) | resource |
42-
| [template_cloudinit_config.init_config](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/cloudinit_config) | data source |
43-
| [template_file.init_config](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |
44-
| [template_file.network_config](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |
4543

4644
## Inputs
4745

4846
| Name | Description | Type | Default | Required |
4947
|------|-------------|------|---------|:--------:|
50-
| <a name="input_additional_disk_ids"></a> [additional\_disk\_ids](#input\_additional\_disk\_ids) | List of volume ids | `list` | `[]` | no |
48+
| <a name="input_additional_disk_ids"></a> [additional\_disk\_ids](#input\_additional\_disk\_ids) | List of volume ids | `list(string)` | `[]` | no |
5149
| <a name="input_autostart"></a> [autostart](#input\_autostart) | Autostart the domain | `bool` | `true` | no |
52-
| <a name="input_base_pool_name"></a> [base\_pool\_name](#input\_base\_pool\_name) | Name of base OS image | `any` | `null` | no |
53-
| <a name="input_base_volume_name"></a> [base\_volume\_name](#input\_base\_volume\_name) | Name of base OS image | `any` | `null` | no |
50+
| <a name="input_base_pool_name"></a> [base\_pool\_name](#input\_base\_pool\_name) | Name of base OS image | `string` | `null` | no |
51+
| <a name="input_base_volume_name"></a> [base\_volume\_name](#input\_base\_volume\_name) | Name of base OS image | `string` | `null` | no |
5452
| <a name="input_bridge"></a> [bridge](#input\_bridge) | Bridge interface | `string` | `"virbr0"` | no |
5553
| <a name="input_cpu_mode"></a> [cpu\_mode](#input\_cpu\_mode) | CPU mode | `string` | `"host-passthrough"` | no |
5654
| <a name="input_dhcp"></a> [dhcp](#input\_dhcp) | Use DHCP or Static IP settings | `bool` | `false` | no |
57-
| <a name="input_hostname"></a> [hostname](#input\_hostname) | VM hostname or FQDN | `string` | `"server"` | no |
5855
| <a name="input_index_start"></a> [index\_start](#input\_index\_start) | From where the indexig start | `number` | `1` | no |
5956
| <a name="input_ip_address"></a> [ip\_address](#input\_ip\_address) | List of IP addresses | `list(string)` | <pre>[<br> "192.168.123.101"<br>]</pre> | no |
6057
| <a name="input_ip_gateway"></a> [ip\_gateway](#input\_ip\_gateway) | IP addresses of a gateway | `string` | `"192.168.123.1"` | no |
6158
| <a name="input_ip_nameserver"></a> [ip\_nameserver](#input\_ip\_nameserver) | IP addresses of a nameserver | `string` | `"192.168.123.1"` | no |
6259
| <a name="input_local_admin"></a> [local\_admin](#input\_local\_admin) | Admin user without ssh access | `string` | `""` | no |
6360
| <a name="input_local_admin_passwd"></a> [local\_admin\_passwd](#input\_local\_admin\_passwd) | Local admin user password | `string` | `"password_example"` | no |
6461
| <a name="input_memory"></a> [memory](#input\_memory) | RAM in MB | `string` | `"1024"` | no |
65-
| <a name="input_os_img_url"></a> [os\_img\_url](#input\_os\_img\_url) | URL to the OS image | `string` | `"https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img"` | no |
62+
| <a name="input_os_img_url"></a> [os\_img\_url](#input\_os\_img\_url) | URL to the OS image | `string` | `"https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img"` | no |
6663
| <a name="input_pool"></a> [pool](#input\_pool) | Storage pool name | `string` | `"default"` | no |
6764
| <a name="input_runcmd"></a> [runcmd](#input\_runcmd) | Extra commands to be run with cloud init | `list(string)` | <pre>[<br> "[ systemctl, daemon-reload ]",<br> "[ systemctl, enable, qemu-guest-agent ]",<br> "[ systemctl, start, qemu-guest-agent ]",<br> "[ systemctl, restart, systemd-networkd ]"<br>]</pre> | no |
6865
| <a name="input_share_filesystem"></a> [share\_filesystem](#input\_share\_filesystem) | n/a | <pre>object({<br> source = string<br> target = string<br> readonly = bool<br> mode = string<br> })</pre> | <pre>{<br> "mode": null,<br> "readonly": false,<br> "source": null,<br> "target": null<br>}</pre> | no |
6966
| <a name="input_ssh_admin"></a> [ssh\_admin](#input\_ssh\_admin) | Admin user with ssh access | `string` | `"ssh-admin"` | no |
7067
| <a name="input_ssh_keys"></a> [ssh\_keys](#input\_ssh\_keys) | List of public ssh keys | `list(string)` | `[]` | no |
71-
| <a name="input_ssh_private_key"></a> [ssh\_private\_key](#input\_ssh\_private\_key) | Private key for SSH connection test | `any` | `null` | no |
68+
| <a name="input_ssh_private_key"></a> [ssh\_private\_key](#input\_ssh\_private\_key) | Private key for SSH connection test | `string` | `null` | no |
7269
| <a name="input_system_volume"></a> [system\_volume](#input\_system\_volume) | System Volume size (GB) | `number` | `10` | no |
7370
| <a name="input_time_zone"></a> [time\_zone](#input\_time\_zone) | Time Zone | `string` | `"UTC"` | no |
7471
| <a name="input_vcpu"></a> [vcpu](#input\_vcpu) | Number of vCPUs | `number` | `1` | no |
7572
| <a name="input_vm_count"></a> [vm\_count](#input\_vm\_count) | Number of VMs | `number` | `1` | no |
7673
| <a name="input_vm_hostname_prefix"></a> [vm\_hostname\_prefix](#input\_vm\_hostname\_prefix) | VM hostname prefix | `string` | `"vm"` | no |
77-
| <a name="input_xml_override"></a> [xml\_override](#input\_xml\_override) | With these variables you can: Enable hugepages; Set USB controllers; Attach USB devices | `any` | <pre>{<br> "hugepages": false,<br> "usb_controllers": [<br> {<br> "model": "piix3-uhci"<br> }<br> ],<br> "usb_devices": []<br>}</pre> | no |
74+
| <a name="input_xml_override"></a> [xml\_override](#input\_xml\_override) | With these variables you can: Enable hugepages; Set USB controllers; Attach USB devices | <pre>object({<br> hugepages = bool<br> usb_controllers = list(object({<br> model = string<br> }))<br> usb_devices = list(object({<br> vendor = string<br> product = string<br> }))<br> })</pre> | <pre>{<br> "hugepages": false,<br> "usb_controllers": [<br> {<br> "model": "piix3-uhci"<br> }<br> ],<br> "usb_devices": []<br>}</pre> | no |
7875

7976
## Outputs
8077

Diff for: cloud_init.tf

-52
This file was deleted.

Diff for: examples/basic/main.tf

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
provider "libvirt" {
2+
uri = "qemu:///system"
3+
}
4+
5+
module "test_nodes" {
6+
source = "../../"
7+
vm_hostname_prefix = "test"
8+
autostart = false
9+
vm_count = 2
10+
index_start = 1
11+
memory = "512"
12+
vcpu = 1
13+
system_volume = 20
14+
ssh_admin = "admin"
15+
ssh_private_key = "~/.ssh/your_key_id_ed25519"
16+
ssh_keys = [
17+
"ssh-ed25519 somethingSOMETHING your_key",
18+
]
19+
local_admin = "localadmin"
20+
local_admin_passwd = "<yout password hash (mkpasswd --method=SHA-512 --rounds=4096)>"
21+
}
22+
23+
output "output_data" {
24+
value = module.test_nodes
25+
}

Diff for: examples/basic/versions.tf

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
required_providers {
4+
libvirt = {
5+
source = "dmacvicar/libvirt"
6+
version = ">= 0.7.0"
7+
}
8+
}
9+
}

Diff for: locals.tf

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
locals {
2+
all_keys = <<EOT
3+
[
4+
%{~for keys in var.ssh_keys~}
5+
"${keys}",
6+
%{~endfor~}
7+
]
8+
EOT
9+
runcmd = <<EOT
10+
%{for cmd in var.runcmd~}
11+
- ${cmd}
12+
%{endfor~}
13+
EOT
14+
}

Diff for: main.tf

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
21
terraform {
3-
required_version = ">= 0.13"
2+
required_version = ">= 1.0"
43
required_providers {
54
libvirt = {
65
source = "dmacvicar/libvirt"
7-
version = ">=0.6.9"
6+
version = ">= 0.7.0"
87
}
98
}
109
}
@@ -20,7 +19,7 @@ resource "libvirt_domain" "virt-machine" {
2019
autostart = var.autostart
2120
qemu_agent = true
2221

23-
cloudinit = element(libvirt_cloudinit_disk.commoninit.*.id, count.index)
22+
cloudinit = element(libvirt_cloudinit_disk.commoninit[*].id, count.index)
2423

2524
network_interface {
2625
bridge = var.bridge
@@ -45,7 +44,7 @@ resource "libvirt_domain" "virt-machine" {
4544
}
4645

4746
disk {
48-
volume_id = element(libvirt_volume.volume-qcow2.*.id, count.index)
47+
volume_id = element(libvirt_volume.volume-qcow2[*].id, count.index)
4948
}
5049

5150
dynamic "disk" {
@@ -79,7 +78,7 @@ resource "libvirt_domain" "virt-machine" {
7978
connection {
8079
type = "ssh"
8180
user = var.ssh_admin
82-
host = self.network_interface.0.addresses.0
81+
host = self.network_interface[0].addresses[0]
8382
private_key = var.ssh_private_key != null ? file(var.ssh_private_key) : null
8483
timeout = "2m"
8584
}

Diff for: output.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
output "name" {
2-
value = libvirt_domain.virt-machine.*.name
2+
value = libvirt_domain.virt-machine[*].name
33
}
44
output "ip_address" {
5-
value = libvirt_domain.virt-machine.*.network_interface.0.addresses.0
5+
value = libvirt_domain.virt-machine[*].network_interface[0].addresses[0]
66
}

Diff for: storage.tf

+25-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,29 @@ resource "libvirt_volume" "volume-qcow2" {
1919
}
2020

2121
resource "libvirt_cloudinit_disk" "commoninit" {
22-
count = var.vm_count
23-
name = format("${var.vm_hostname_prefix}_init%02d.iso", count.index + 1)
24-
user_data = data.template_cloudinit_config.init_config[count.index].rendered
25-
network_config = data.template_file.network_config[count.index].rendered
26-
pool = var.pool
22+
count = var.vm_count
23+
name = format("${var.vm_hostname_prefix}_init%02d.iso", count.index + 1)
24+
user_data = templatefile(
25+
"${path.module}/templates/cloud_init.tpl",
26+
{
27+
ssh_admin = var.ssh_admin
28+
ssh_keys = local.all_keys
29+
local_admin = var.local_admin
30+
local_admin_passwd = var.local_admin_passwd
31+
hostname = format("${var.vm_hostname_prefix}%02d", count.index + var.index_start)
32+
time_zone = var.time_zone
33+
runcmd = local.runcmd
34+
}
35+
)
36+
network_config = templatefile(
37+
"${path.module}/templates/network_config_${var.dhcp == true ? "dhcp" : "static"}.tpl",
38+
{
39+
ip_address = element(var.ip_address, count.index)
40+
ip_gateway = var.ip_gateway
41+
ip_nameserver = var.ip_nameserver
42+
nic = (var.share_filesystem.source == null ? "ens3" : "ens4")
43+
# WA: If the shared filesystem is used, Libvirt connects Unclassified device to the 3rd position of PCI bus
44+
}
45+
)
46+
pool = var.pool
2747
}

Diff for: templates/cloud_init.tpl

+30-30
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,36 @@ users:
3232
write_files:
3333
- path: /etc/ssh/sshd_config
3434
content: |
35-
Port 22
36-
Protocol 2
37-
HostKey /etc/ssh/ssh_host_rsa_key
38-
HostKey /etc/ssh/ssh_host_dsa_key
39-
HostKey /etc/ssh/ssh_host_ecdsa_key
40-
HostKey /etc/ssh/ssh_host_ed25519_key
41-
UsePrivilegeSeparation yes
42-
KeyRegenerationInterval 3600
43-
ServerKeyBits 1024
44-
SyslogFacility AUTH
45-
LogLevel INFO
46-
LoginGraceTime 120
47-
PermitRootLogin no
48-
StrictModes yes
49-
RSAAuthentication yes
50-
PubkeyAuthentication yes
51-
IgnoreRhosts yes
52-
RhostsRSAAuthentication no
53-
HostbasedAuthentication no
54-
PermitEmptyPasswords no
55-
ChallengeResponseAuthentication no
56-
X11Forwarding yes
57-
X11DisplayOffset 10
58-
PrintMotd no
59-
PrintLastLog yes
60-
TCPKeepAlive yes
61-
AcceptEnv LANG LC_*
62-
Subsystem sftp /usr/lib/openssh/sftp-server
63-
UsePAM yes
64-
AllowUsers ${ssh_admin}
35+
Port 22
36+
Protocol 2
37+
HostKey /etc/ssh/ssh_host_rsa_key
38+
HostKey /etc/ssh/ssh_host_dsa_key
39+
HostKey /etc/ssh/ssh_host_ecdsa_key
40+
HostKey /etc/ssh/ssh_host_ed25519_key
41+
UsePrivilegeSeparation yes
42+
KeyRegenerationInterval 3600
43+
ServerKeyBits 1024
44+
SyslogFacility AUTH
45+
LogLevel INFO
46+
LoginGraceTime 120
47+
PermitRootLogin no
48+
StrictModes yes
49+
RSAAuthentication yes
50+
PubkeyAuthentication yes
51+
IgnoreRhosts yes
52+
RhostsRSAAuthentication no
53+
HostbasedAuthentication no
54+
PermitEmptyPasswords no
55+
ChallengeResponseAuthentication no
56+
X11Forwarding yes
57+
X11DisplayOffset 10
58+
PrintMotd no
59+
PrintLastLog yes
60+
TCPKeepAlive yes
61+
AcceptEnv LANG LC_*
62+
Subsystem sftp /usr/lib/openssh/sftp-server
63+
UsePAM yes
64+
AllowUsers ${ssh_admin}
6565

6666
growpart:
6767
mode: auto

Diff for: templates/network_config_static.tpl

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ ethernets:
88
addresses:
99
- ${ip_nameserver}
1010
- 8.8.8.8
11+
- 1.1.1.1

0 commit comments

Comments
 (0)