Skip to content

Commit 9113cf8

Browse files
Merge pull request #21 from MonolithProjects/feat/templating
Feat/templating
2 parents 9124946 + 77e8373 commit 9113cf8

File tree

12 files changed

+133
-98
lines changed

12 files changed

+133
-98
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
tests
2+
.terraform*

README.md

Lines changed: 11 additions & 12 deletions
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,40 +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-
| [cloudinit_config.init_config](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/cloudinit_config) | data source |
4343

4444
## Inputs
4545

4646
| Name | Description | Type | Default | Required |
4747
|------|-------------|------|---------|:--------:|
48-
| <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 |
4949
| <a name="input_autostart"></a> [autostart](#input\_autostart) | Autostart the domain | `bool` | `true` | no |
50-
| <a name="input_base_pool_name"></a> [base\_pool\_name](#input\_base\_pool\_name) | Name of base OS image | `any` | `null` | no |
51-
| <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 |
5252
| <a name="input_bridge"></a> [bridge](#input\_bridge) | Bridge interface | `string` | `"virbr0"` | no |
5353
| <a name="input_cpu_mode"></a> [cpu\_mode](#input\_cpu\_mode) | CPU mode | `string` | `"host-passthrough"` | no |
5454
| <a name="input_dhcp"></a> [dhcp](#input\_dhcp) | Use DHCP or Static IP settings | `bool` | `false` | no |
55-
| <a name="input_hostname"></a> [hostname](#input\_hostname) | VM hostname or FQDN | `string` | `"server"` | no |
5655
| <a name="input_index_start"></a> [index\_start](#input\_index\_start) | From where the indexig start | `number` | `1` | no |
5756
| <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 |
5857
| <a name="input_ip_gateway"></a> [ip\_gateway](#input\_ip\_gateway) | IP addresses of a gateway | `string` | `"192.168.123.1"` | no |
5958
| <a name="input_ip_nameserver"></a> [ip\_nameserver](#input\_ip\_nameserver) | IP addresses of a nameserver | `string` | `"192.168.123.1"` | no |
6059
| <a name="input_local_admin"></a> [local\_admin](#input\_local\_admin) | Admin user without ssh access | `string` | `""` | no |
6160
| <a name="input_local_admin_passwd"></a> [local\_admin\_passwd](#input\_local\_admin\_passwd) | Local admin user password | `string` | `"password_example"` | no |
6261
| <a name="input_memory"></a> [memory](#input\_memory) | RAM in MB | `string` | `"1024"` | no |
63-
| <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 |
6463
| <a name="input_pool"></a> [pool](#input\_pool) | Storage pool name | `string` | `"default"` | no |
6564
| <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 |
6665
| <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 |
6766
| <a name="input_ssh_admin"></a> [ssh\_admin](#input\_ssh\_admin) | Admin user with ssh access | `string` | `"ssh-admin"` | no |
6867
| <a name="input_ssh_keys"></a> [ssh\_keys](#input\_ssh\_keys) | List of public ssh keys | `list(string)` | `[]` | no |
69-
| <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 |
7069
| <a name="input_system_volume"></a> [system\_volume](#input\_system\_volume) | System Volume size (GB) | `number` | `10` | no |
7170
| <a name="input_time_zone"></a> [time\_zone](#input\_time\_zone) | Time Zone | `string` | `"UTC"` | no |
7271
| <a name="input_vcpu"></a> [vcpu](#input\_vcpu) | Number of vCPUs | `number` | `1` | no |
7372
| <a name="input_vm_count"></a> [vm\_count](#input\_vm\_count) | Number of VMs | `number` | `1` | no |
7473
| <a name="input_vm_hostname_prefix"></a> [vm\_hostname\_prefix](#input\_vm\_hostname\_prefix) | VM hostname prefix | `string` | `"vm"` | no |
75-
| <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 |
7675

7776
## Outputs
7877

cloud_init.tf

Lines changed: 0 additions & 37 deletions
This file was deleted.

examples/basic/main.tf

Lines changed: 25 additions & 0 deletions
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+
}

examples/basic/versions.tf

Lines changed: 9 additions & 0 deletions
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+
}

locals.tf

Lines changed: 14 additions & 0 deletions
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+
}

main.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
terraform {
2-
required_version = ">= 0.13"
2+
required_version = ">= 1.0"
33
required_providers {
44
libvirt = {
55
source = "dmacvicar/libvirt"
6-
version = ">=0.6.9"
6+
version = ">= 0.7.0"
77
}
88
}
99
}
@@ -19,7 +19,7 @@ resource "libvirt_domain" "virt-machine" {
1919
autostart = var.autostart
2020
qemu_agent = true
2121

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

2424
network_interface {
2525
bridge = var.bridge
@@ -44,7 +44,7 @@ resource "libvirt_domain" "virt-machine" {
4444
}
4545

4646
disk {
47-
volume_id = element(libvirt_volume.volume-qcow2.*.id, count.index)
47+
volume_id = element(libvirt_volume.volume-qcow2[*].id, count.index)
4848
}
4949

5050
dynamic "disk" {
@@ -78,7 +78,7 @@ resource "libvirt_domain" "virt-machine" {
7878
connection {
7979
type = "ssh"
8080
user = var.ssh_admin
81-
host = self.network_interface.0.addresses.0
81+
host = self.network_interface[0].addresses[0]
8282
private_key = var.ssh_private_key != null ? file(var.ssh_private_key) : null
8383
timeout = "2m"
8484
}

output.tf

Lines changed: 2 additions & 2 deletions
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
}

storage.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ 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 = templatefile(
22+
count = var.vm_count
23+
name = format("${var.vm_hostname_prefix}_init%02d.iso", count.index + 1)
24+
user_data = templatefile(
2525
"${path.module}/templates/cloud_init.tpl",
2626
{
2727
ssh_admin = var.ssh_admin
@@ -43,5 +43,5 @@ resource "libvirt_cloudinit_disk" "commoninit" {
4343
# WA: If the shared filesystem is used, Libvirt connects Unclassified device to the 3rd position of PCI bus
4444
}
4545
)
46-
pool = var.pool
46+
pool = var.pool
4747
}

templates/cloud_init.tpl

Lines changed: 30 additions & 30 deletions
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

templates/network_config_static.tpl

Lines changed: 1 addition & 0 deletions
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)