From f70970f0e41f3ebc788ba481f1c85b5007ab7881 Mon Sep 17 00:00:00 2001 From: Antoine Date: Sun, 24 Jan 2021 21:49:30 +0100 Subject: [PATCH] Add third domain, generate ansible toml inventory generate ansible inventory from terraform provisioned domain add make hazelcast to run and setup hazelcast onto domain --- Makefile | 22 +++++++++++++++++++++- stack/cloud_init.tf | 4 ++-- stack/computes.tf | 3 ++- stack/libvirtd.tf | 20 +++++++++++++++----- stack/networks.tf | 9 +++++---- stack/variable.tf | 17 +++++------------ stack/volumes.tf | 4 ++-- 7 files changed, 52 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index adbad4a..abb25c4 100644 --- a/Makefile +++ b/Makefile @@ -14,4 +14,24 @@ get-ip: @virsh domifaddr --domain db1 destroy_vm: - @terraform destroy -auto-approve -target=libvirt_domain.db1 + @cd stack && terraform destroy --auto-approve -target=libvirt_domain.db1 + +destroy_all: + @cd stack && terraform destroy --auto-approve + +create_all: + @cd stack && terraform apply --auto-approve + +hazelcast: + cd applications/ && \ + echo "create python venv workspace" && \ + python3 -m venv workspace && \ + source workspace/bin/activate && \ + echo "install python venv and dependencies" && \ + pip install --upgrade pip && \ + pip install -r requirements.txt && \ + echo "install ansible-galaxy dependencies" && \ + ansible-galaxy install -r hazelcast/requirements.yml && \ + echo "start playbook" && \ + ansible-playbook -i stack_address.toml hazelcast/playbook.yml + diff --git a/stack/cloud_init.tf b/stack/cloud_init.tf index 608e9cd..6823c78 100644 --- a/stack/cloud_init.tf +++ b/stack/cloud_init.tf @@ -1,5 +1,5 @@ data "template_file" "user_data" { - template = "${file("${path.module}/cloud_init_user_data.yml")}" + template = file(format("%s/cloud_init_user_data.yml", path.module)) vars = { hostname = format("%s-%s", var.hostname, count.index) } @@ -7,7 +7,7 @@ data "template_file" "user_data" { } data "template_file" "meta_data" { - template = "${file("${path.module}/cloud_init_meta_data.yml")}" + template = file(format("%s/cloud_init_meta_data.yml", path.module)) vars = { instance_id = count.index } diff --git a/stack/computes.tf b/stack/computes.tf index cc995f4..d200b9c 100644 --- a/stack/computes.tf +++ b/stack/computes.tf @@ -9,7 +9,8 @@ resource "libvirt_domain" "domains" { running = "true" autostart = "true" - count = var.number_domain + count = var.number_domain + qemu_agent = true boot_device { dev = ["hd", "network"] diff --git a/stack/libvirtd.tf b/stack/libvirtd.tf index 6e91ab6..28f7e49 100644 --- a/stack/libvirtd.tf +++ b/stack/libvirtd.tf @@ -16,10 +16,6 @@ terraform { source = "dmacvicar/libvirt" version = ">= 0.6.3" } - docker = { - source = "kreuzwerker/docker" - version = "2.11.0" - } } backend "etcdv3" { @@ -42,7 +38,7 @@ resource "null_resource" "delay_10s" { command = "sleep 120" } triggers = { - // trigger after last domain created + # trigger after last domain created "after" = libvirt_domain.domains[var.number_domain - 1].id } } @@ -52,3 +48,17 @@ output "ip" { value = libvirt_domain.domains.*.network_interface depends_on = [null_resource.delay_10s] } + +resource "local_file" "write_address" { + + content = <<-EOT +[all] +%{ for idx, s in libvirt_domain.domains.*.network_interface.0.addresses.0 ~} +${s} index=${idx} +%{ endfor ~} +[all:vars] +ansible_ssh_common_args='-o StrictHostKeyChecking=no' + EOT + + filename = "../applications/stack_address.toml" +} diff --git a/stack/networks.tf b/stack/networks.tf index 6973dcf..aca454d 100644 --- a/stack/networks.tf +++ b/stack/networks.tf @@ -1,10 +1,11 @@ # https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/website/docs/r/network.markdown resource "libvirt_network" "private_network" { - name = "private" - mode = "nat" - domain = "network.local" - addresses = ["10.17.3.0/24"] + name = "private" + mode = "bridge" # "nat" + bridge = "br0" # only for bridge mode + # domain = "network.local" # only for nat mode + # addresses = [var.ip] # only for nat mode autostart = true dhcp { enabled = true diff --git a/stack/variable.tf b/stack/variable.tf index 276727d..c502f2a 100644 --- a/stack/variable.tf +++ b/stack/variable.tf @@ -1,12 +1,12 @@ variable "ip" { type = string - default = "10.17.3.35/" + default = "10.17.3.35/24" description = "fixed ip address for compute" } variable "number_domain" { type = string - default = "3" + default = "4" description = "number of domain" } @@ -28,14 +28,7 @@ variable "pool_1" { description = "already created pool name, set with variable because libvirt provider dont' wotk with data resource" } -variable "debian_buster_qcow2" { - type = string - default = "debian-10.7.0-with-docker.qcow2" - description = "already created debian vol, set with variable because libvirt provider dont' wotk with data resource" -} - -variable "centos7_qcow2" { - type = string - default = "centos7.qcow2" - description = "already created centos vol, set with variable because libvirt provider dont' wotk with data resource" +locals { + debian_buster_qcow2 = "debian-10.7.0-with-docker.qcow2" + centos7_qcow2 = "centos7.qcow2" } diff --git a/stack/volumes.tf b/stack/volumes.tf index 490e3c8..7e37565 100644 --- a/stack/volumes.tf +++ b/stack/volumes.tf @@ -7,13 +7,13 @@ resource "libvirt_volume" "root_centos" { name = "my-root-centos" pool = var.pool_1 - base_volume_name = var.centos7_qcow2 + base_volume_name = local.centos7_qcow2 } resource "libvirt_volume" "root_debian" { name = format("root-debian-%s", count.index) pool = var.pool_1 - base_volume_name = var.debian_buster_qcow2 + base_volume_name = local.debian_buster_qcow2 count = var.number_domain }