Add count to generate multiple domain

This commit is contained in:
Antoine 2021-01-23 21:45:22 +01:00
vanhempi bf6bdd46f1
commit a75f875026
Allekirjoittanut: antoine
GPG avaimen ID: 098FB66FC0475E70
10 muutettua tiedostoa jossa 64 lisäystä ja 39 poistoa

Näytä tiedosto

@ -4,8 +4,8 @@ console:
@virsh console --domain db1
ssh:
@IP=$(shell virsh domifaddr --domain db1 | tail -2 | awk -F ' ' '{ print $$4 }' | sed 's|/.*||') && \
ssh -o ProxyCommand="ssh -W %h:%p antoine@dx30.localdomain" -i ~/.ssh/pri/id_rsa_bis antoine@"$$IP"
IP=$(shell virsh domifaddr --domain db1 | tail -2 | awk -F ' ' '{ print $$4 }' | sed 's|/.*||') && \
ssh -o ProxyCommand="ssh -W %h:%p antoine@dx30_remote.localdomain" -i ~/.ssh/pri/id_rsa_bis antoine@"$$IP"
dhcp-lease:
@virsh net-dhcp-leases --network private
@ -14,4 +14,4 @@ get-ip:
@virsh domifaddr --domain db1
destroy_vm:
@terraform destroy -auto-approve -target=libvirt_domain.db1
@terraform destroy -auto-approve -target=libvirt_domain.db1

Näytä tiedosto

@ -47,8 +47,16 @@ resource "libvirt_volume" "centos7_qcow2" {
}
resource "libvirt_volume" "debian_buster_qcow2" {
name = "debian-buster.qcow2"
name = "debian-openstack-buster.qcow2"
pool = libvirt_pool.pool_1.name
source = "./images/debian-10-openstack-amd64.qcow2"
format = "qcow2"
}
resource "libvirt_volume" "debian_with_docker_qcow2" {
name = "debian-10.7.0-with-docker.qcow2"
pool = libvirt_pool.pool_1.name
source = "./images/debian-10.7.0_packer.qcow2"
format = "qcow2"
}

@ -1 +1 @@
Subproject commit 6e6f8cb6470380c8b4c07910d98f744bad8852a3
Subproject commit ebd073a56c23ea79efd65426563182c92855725f

Näytä tiedosto

@ -1,22 +1,25 @@
data "template_file" "user_data" {
template = "${file("${path.module}/cloud_init_user_data.yml")}"
vars = {
hostname = var.hostname
hostname = format("%s-%s", var.hostname, count.index)
}
count = var.number_domain
}
data "template_file" "meta_data" {
template = "${file("${path.module}/cloud_init_meta_data.yml")}"
vars = {
dns_address = var.dns_address
dns_domain = var.dns_domain
instance_id = count.index
}
count = var.number_domain
}
# Use CloudInit to add the instance
resource "libvirt_cloudinit_disk" "commoninit" {
name = "commoninit.iso"
name = format("commoninit-%s.iso", count.index)
pool = var.pool_1
user_data = data.template_file.user_data.rendered
meta_data = data.template_file.meta_data.rendered
user_data = data.template_file.user_data[count.index].rendered
meta_data = data.template_file.meta_data[count.index].rendered
count = var.number_domain
}

Näytä tiedosto

@ -1,8 +1,6 @@
#cloud-config
# vim: syntax=yaml
network-interfaces: |
auto eth0
iface eth0 inet dhcp
dns-nameservers ${dns_address}
dns-search ${dns_domain}
instance-id: ${instance_id}
network:
config: disabled

Näytä tiedosto

@ -2,13 +2,15 @@
# Define KVM domain to create
resource "libvirt_domain" "db1" {
name = "db1"
resource "libvirt_domain" "domains" {
name = format("db%s", count.index)
memory = "1024"
vcpu = 1
running = "true"
autostart = "true"
count = var.number_domain
boot_device {
dev = ["hd", "network"]
}
@ -18,14 +20,14 @@ resource "libvirt_domain" "db1" {
}
disk {
volume_id = libvirt_volume.my_root_debian.id
volume_id = element(libvirt_volume.root_debian.*.id, count.index)
}
disk {
volume_id = libvirt_volume.external_disk_1.id
volume_id = element(libvirt_volume.external_disk.*.id, count.index)
}
cloudinit = libvirt_cloudinit_disk.commoninit.id
cloudinit = libvirt_cloudinit_disk.commoninit[count.index].id
console {
type = "pty"

Näytä tiedosto

@ -13,9 +13,13 @@ terraform {
version = "~> 2.1"
}
libvirt = {
source = "dmacvicar/libvirt"
source = "dmacvicar/libvirt"
version = ">= 0.6.3"
}
docker = {
source = "kreuzwerker/docker"
version = "2.11.0"
}
}
backend "etcdv3" {
@ -35,15 +39,16 @@ provider "libvirt" {
resource "null_resource" "delay_10s" {
provisioner "local-exec" {
command = "sleep 10"
command = "sleep 120"
}
triggers = {
"before" = libvirt_domain.db1.id
// trigger after last domain created
"after" = libvirt_domain.domains[var.number_domain - 1].id
}
}
# Output Server IP
output "ip" {
value = libvirt_domain.db1.network_interface
value = libvirt_domain.domains.*.network_interface
depends_on = [null_resource.delay_10s]
}

Näytä tiedosto

@ -9,4 +9,11 @@ resource "libvirt_network" "private_network" {
dhcp {
enabled = true
}
dns {
enabled = true
local_only = true
forwarders {
address = var.dns_address
}
}
}

Näytä tiedosto

@ -4,16 +4,16 @@ variable "ip" {
description = "fixed ip address for compute"
}
variable "hostname" {
variable "number_domain" {
type = string
default = "compute-1"
description = "compute hostname"
default = "3"
description = "number of domain"
}
variable "dns_domain" {
variable "hostname" {
type = string
default = "localdomain"
description = "dns domain name"
default = "compute"
description = "compute hostname"
}
variable "dns_address" {
@ -30,7 +30,7 @@ variable "pool_1" {
variable "debian_buster_qcow2" {
type = string
default = "debian-buster.qcow2"
default = "debian-10.7.0-with-docker.qcow2"
description = "already created debian vol, set with variable because libvirt provider dont' wotk with data resource"
}

Näytä tiedosto

@ -4,21 +4,23 @@
# ~> Tip: when provisioning multiple domains using the same base image, create a libvirt_volume for the base image and then define
# the domain specific ones as based on it. This way the image will not be modified and no extra disk space is going to be used for the base image.
resource "libvirt_volume" "my_root_centos" {
resource "libvirt_volume" "root_centos" {
name = "my-root-centos"
pool = var.pool_1
base_volume_name = var.centos7_qcow2
}
resource "libvirt_volume" "my_root_debian" {
name = "my-root-debian"
resource "libvirt_volume" "root_debian" {
name = format("root-debian-%s", count.index)
pool = var.pool_1
base_volume_name = var.debian_buster_qcow2
count = var.number_domain
}
resource "libvirt_volume" "external_disk_1" {
name = "external-disk-1"
resource "libvirt_volume" "external_disk" {
name = format("external-disk-%s", count.index)
# 10Gb
size = 10737418240
pool = var.pool_1
size = 10737418240
pool = var.pool_1
count = var.number_domain
}