From 8a3909ac0f682bade6198cfd6fa54959edccbb4c Mon Sep 17 00:00:00 2001 From: Sergey Filimonov Date: Tue, 3 Dec 2024 18:20:16 +0700 Subject: [PATCH] Init Project --- .gitignore | 3 ++ cdrom-model.xsl | 17 ++++++++++ cloud_init.yml | 21 ++++++++++++ main.tf | 85 ++++++++++++++++++++++++++++++++++++++++++++++ network_config.yml | 14 ++++++++ providers.tf | 3 ++ terraform.tfvars | 42 +++++++++++++++++++++++ variables.tf | 37 ++++++++++++++++++++ versions.tf | 9 +++++ 9 files changed, 231 insertions(+) create mode 100644 .gitignore create mode 100644 cdrom-model.xsl create mode 100644 cloud_init.yml create mode 100644 main.tf create mode 100644 network_config.yml create mode 100644 providers.tf create mode 100644 terraform.tfvars create mode 100644 variables.tf create mode 100644 versions.tf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e9d1a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.hcl +*.tfstate* +.terraform/ diff --git a/cdrom-model.xsl b/cdrom-model.xsl new file mode 100644 index 0000000..4946241 --- /dev/null +++ b/cdrom-model.xsl @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/cloud_init.yml b/cloud_init.yml new file mode 100644 index 0000000..d714d36 --- /dev/null +++ b/cloud_init.yml @@ -0,0 +1,21 @@ +#cloud-config +ssh_pwauth: True +chpasswd: + list: | + root:linux + expire: False +users: + - name: "${login}" + sudo: ALL=(ALL) NOPASSWD:ALL + plain_text_passwd: "${passwd}" + shell: /bin/bash + lock-passwd: false + ssh_pwauth: True + chpasswd: { expire: False } +package_update: true +packages: + - qemu-guest-agent +runcmd: + - [ netplan, generate ] + - [ netplan, apply ] + - [ systemctl, enable, --now, qemu-guest-agent ] \ No newline at end of file diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..56d976a --- /dev/null +++ b/main.tf @@ -0,0 +1,85 @@ +data "template_file" "user_data" { + count = length(var.domains) + template = file("${path.module}/cloud_init.yml") + vars = { + login = "admin" + ip = var.domains[count.index].ip + } +} + +data "template_file" "network_config" { + count = length(var.domains) + template = file("${path.module}/network_config.yml") + vars = { + ip = var.domains[count.index].ip + } +} + + +resource "libvirt_pool" "pool" { + name = "${var.prefix}pool" + type = "dir" + target { + path = "${var.pool_path}/${var.prefix}pool" + } +} + +resource "libvirt_volume" "image" { + name = var.image.name + format = "qcow2" + pool = libvirt_pool.pool.name + source = var.image.url +} + +resource "libvirt_volume" "root" { + count = length(var.domains) + name = "${var.prefix}${var.domains[count.index].name}-root.qcow2" + pool = libvirt_pool.pool.name + base_volume_id = libvirt_volume.image.id + size = var.domains[count.index].disk + format = "qcow2" +} + +# resource "libvirt_network" "br0" { +# bridge = "br0" +# name = "br0" +# mode = "bridge" +# autostart = true +# } + +resource "libvirt_cloudinit_disk" "commoninit" { + count = length(var.domains) + name = "commoninit-${var.domains[count.index].name}.iso" + pool = libvirt_pool.pool.name + user_data = data.template_file.user_data[count.index].rendered + network_config = data.template_file.network_config[count.index].rendered +} + +resource "libvirt_domain" "vm" { + count = length(var.domains) + name = var.domains[count.index].name + memory = var.domains[count.index].ram + vcpu = var.domains[count.index].cpu + machine = "q35" + arch = "x86_64" + + xml { + xslt = file("/home/filimonov/t1/cdrom-model.xsl") + } + + cloudinit = libvirt_cloudinit_disk.commoninit[count.index].id + + + cpu { + mode = "custom" + } + + network_interface { + network_name = "br0" + } + + disk { + volume_id = libvirt_volume.root[count.index].id + scsi = "true" + } +} \ No newline at end of file diff --git a/network_config.yml b/network_config.yml new file mode 100644 index 0000000..49d3601 --- /dev/null +++ b/network_config.yml @@ -0,0 +1,14 @@ +version: 2 +ethernets: + enp1s0: + link-local: + - ipv4 + dhcp4: false + addresses: + - "${ip}/24" + routes: + - to: default + via: 172.16.1.1 + nameservers: + addresses: + - 8.8.8.8 diff --git a/providers.tf b/providers.tf new file mode 100644 index 0000000..21b377c --- /dev/null +++ b/providers.tf @@ -0,0 +1,3 @@ +provider "libvirt" { + uri = "qemu:///system" +} \ No newline at end of file diff --git a/terraform.tfvars b/terraform.tfvars new file mode 100644 index 0000000..d5da687 --- /dev/null +++ b/terraform.tfvars @@ -0,0 +1,42 @@ +prefix = "vm-" + +pool_path = "/opt/vm" + +image = { + name = "cloud" +# url = "http://localhost/gold-ubuntu.qcow2" + url = "http://localhost/ubuntu-24.04-server-cloudimg-amd64.img" + format = "qcow2" +} + +vm = { + bridge = "br0" + cpu = 1 + disk = 10 * 1024 * 1024 * 1024 + ram = 512 +} + +domains = [ + { + name = "vm-node1" + cpu = 1 + ram = 2048 + disk = 20 * 1024 * 1024 * 1024 + ip = "172.16.1.181" + }, + { + name = "vm-node2" + cpu = 1 + ram = 2048 + disk = 20 * 1024 * 1024 * 1024 + ip = "172.16.1.182" + }, + { + name = "vm-master1" + cpu = 1 + ram = 2048 + disk = 20 * 1024 * 1024 * 1024 + ip = "172.16.1.180" + } + +] \ No newline at end of file diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..d71ecf9 --- /dev/null +++ b/variables.tf @@ -0,0 +1,37 @@ +variable "prefix" { + type = string + default = "vm-" +} + +variable "pool_path" { + type = string + default = "/var/lib/libvirt/" +} + +variable "image" { + type = object({ + name = string + url = string + format = string + }) +} + +variable "vm" { + type = object({ + cpu = number + ram = number + disk = number + bridge = string + }) +} + +variable "domains" { + description = "List of VMs with specified parameters" + type = list(object({ + name = string, + cpu = number, + ram = number, + disk = number, + ip = string + })) +} \ No newline at end of file diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..8d5c4a6 --- /dev/null +++ b/versions.tf @@ -0,0 +1,9 @@ +terraform { + required_version = ">= 0.13" + required_providers { + libvirt = { + source = "dmacvicar/libvirt" + version = "0.8.1" + } + } +} \ No newline at end of file