Add count to generate multiple domain

This commit is contained in:
Antoine 2021-01-23 21:45:22 +01:00
parent bf6bdd46f1
commit a75f875026
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
10 changed files with 64 additions and 39 deletions

View File

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

View File

@ -47,8 +47,16 @@ resource "libvirt_volume" "centos7_qcow2" {
} }
resource "libvirt_volume" "debian_buster_qcow2" { resource "libvirt_volume" "debian_buster_qcow2" {
name = "debian-buster.qcow2" name = "debian-openstack-buster.qcow2"
pool = libvirt_pool.pool_1.name pool = libvirt_pool.pool_1.name
source = "./images/debian-10-openstack-amd64.qcow2" source = "./images/debian-10-openstack-amd64.qcow2"
format = "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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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 # ~> 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. # 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" name = "my-root-centos"
pool = var.pool_1 pool = var.pool_1
base_volume_name = var.centos7_qcow2 base_volume_name = var.centos7_qcow2
} }
resource "libvirt_volume" "my_root_debian" { resource "libvirt_volume" "root_debian" {
name = "my-root-debian" name = format("root-debian-%s", count.index)
pool = var.pool_1 pool = var.pool_1
base_volume_name = var.debian_buster_qcow2 base_volume_name = var.debian_buster_qcow2
count = var.number_domain
} }
resource "libvirt_volume" "external_disk_1" { resource "libvirt_volume" "external_disk" {
name = "external-disk-1" name = format("external-disk-%s", count.index)
# 10Gb # 10Gb
size = 10737418240 size = 10737418240
pool = var.pool_1 pool = var.pool_1
count = var.number_domain
} }