Init Project

This commit is contained in:
Сергей Филимонов 2024-12-03 18:20:16 +07:00
commit 8a3909ac0f
9 changed files with 231 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.hcl
*.tfstate*
.terraform/

17
cdrom-model.xsl Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/domain/devices/disk[@device='cdrom']/target/@bus">
<xsl:attribute name="bus">
<xsl:value-of select="'scsi'"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>

21
cloud_init.yml Normal file
View File

@ -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 ]

85
main.tf Normal file
View File

@ -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"
}
}

14
network_config.yml Normal file
View File

@ -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

3
providers.tf Normal file
View File

@ -0,0 +1,3 @@
provider "libvirt" {
uri = "qemu:///system"
}

42
terraform.tfvars Normal file
View File

@ -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"
}
]

37
variables.tf Normal file
View File

@ -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
}))
}

9
versions.tf Normal file
View File

@ -0,0 +1,9 @@
terraform {
required_version = ">= 0.13"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.8.1"
}
}
}