net-lab: add hosts and category iteration logic

* add a category variable
* add multiple different hosts and enable specifying base image per vm or
per category
* add comments to explain the code
* rename the project
* rename the base domain
This commit is contained in:
surtur 2021-05-13 01:23:46 +02:00
parent 9f68d39731
commit adfdfd3726
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 71 additions and 27 deletions

@ -48,11 +48,18 @@ variable "sourceimage" {
type = string
default = "https://download.fedoraproject.org/pub/fedora/linux/releases/33/Cloud/x86_64/images/Fedora-Cloud-Base-33-1.2.x86_64.qcow2"
}
variable "category" {
type = string
default = "host"
}
# base OS image
resource "libvirt_volume" "baseosimage" {
name = "baseosimage_${var.projectname}"
source = var.sourceimage
for_each = var.hosts
# if desired, base image could be specified for a category of devices sharing
# the base image using name = "baseosimage_${var.projectname}.${each.value.category}"
name = "baseosimage_${var.projectname}.${each.value.name}"
source = each.value.sourceimage
pool = var.baseimagediskpool
}
@ -61,8 +68,10 @@ resource "libvirt_volume" "baseosimage" {
resource "libvirt_volume" "qcow2_volume" {
for_each = var.hosts
name = "${each.value.name}.qcow2"
base_volume_id = libvirt_volume.baseosimage.id
# let each baseos image have a name after the vm using it
base_volume_id = libvirt_volume.baseosimage[each.value.name].id
pool = each.value.diskpool
# currently a hard constraint to only use qcow2, could become settable
format = "qcow2"
size = each.value.disksize
}
@ -85,8 +94,8 @@ resource "libvirt_cloudinit_disk" "commoninit" {
user_data = data.template_file.user_data[each.key].rendered
}
# default guest
resource "libvirt_domain" "default_guest" {
# net-lab domains loop
resource "libvirt_domain" "net-lab" {
for_each = var.hosts
name = each.value.name
vcpu = each.value.vcpu
@ -103,12 +112,12 @@ resource "libvirt_domain" "default_guest" {
volume_id = element(libvirt_volume.qcow2_volume[each.key].*.id, 1)
}
cloudinit = libvirt_cloudinit_disk.commoninit[each.key].id
cloudinit = libvirt_cloudinit_disk.commoninit[each.value.name].id
}
output "hostnames" {
value = [libvirt_domain.default_guest.*]
value = [libvirt_domain.net-lab.*]
}

@ -1,8 +1,9 @@
# project name
projectname = "vms"
projectname = "net-lab-infra"
# OS image
#sourceimage = "https://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.3.2011-20201204.2.x86_64.qcow2"
# sourceimage = "$HOME/.images/CentOS-8-GenericCloud-8.3.2011-20201204.2.x86_64.qcow2"
# sourceimage = "$HOME/.images/Fedora-Cloud-Base-33-1.2.x86_64.qcow2"
sourceimage = "https://download.fedoraproject.org/pub/fedora/linux/releases/33/Cloud/x86_64/images/Fedora-Cloud-Base-33-1.2.x86_64.qcow2"
# the base image is the source image for all VMs created from it
@ -18,21 +19,55 @@ networkname = "default" # default==NAT
# RAM in bytes
# disk size in bytes (disk size must be greater than source image virtual size)
hosts = {
"victim" = {
name = "victim",
"h_victim" = {
name = "h_victim",
vcpu = 1,
memory = "512",
memory = "768",
diskpool = "default",
disksize = "4300000000",
disksize = "11000000000",
mac = "00:00:00:13:37:22",
sourceimage = "https://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.3.2011-20201204.2.x86_64.qcow2",
category = "host-victim",
},
"attacker" = {
name = "attacker",
"h_attacker" = {
name = "h_attacker",
vcpu = 1,
memory = "1024",
diskpool = "default",
disksize = "4300000000",
disksize = "5370000000",
mac = "00:00:00:13:37:23",
sourceimage = "https://download.fedoraproject.org/pub/fedora/linux/releases/34/Cloud/x86_64/images/Fedora-Cloud-Base-34-1.2.x86_64.qcow2",
category = "host-attacker",
},
"r_edge" = {
name = "r_edge",
vcpu = 1,
memory = "768",
diskpool = "default",
disksize = "4300000000",
mac = "00:00:00:13:37:24",
sourceimage = "https://download.fedoraproject.org/pub/fedora/linux/releases/33/Cloud/x86_64/images/Fedora-Cloud-Base-33-1.2.x86_64.qcow2",
category = "router",
},
"r_upstream" = {
name = "r_upstream",
vcpu = 1,
memory = "768",
diskpool = "default",
disksize = "4300000000",
mac = "00:00:00:13:37:25",
sourceimage = "https://download.fedoraproject.org/pub/fedora/linux/releases/33/Cloud/x86_64/images/Fedora-Cloud-Base-33-1.2.x86_64.qcow2",
category = "router",
},
"h_defender" = {
name = "h_defender",
vcpu = 1,
memory = "1024",
diskpool = "default",
disksize = "5370000000",
mac = "00:00:00:13:37:26",
sourceimage = "https://download.fedoraproject.org/pub/fedora/linux/releases/34/Cloud/x86_64/images/Fedora-Cloud-Base-34-1.2.x86_64.qcow2",
category = "host-defender",
},
}