terraform: finalize config

This commit is contained in:
Glenn Y. Rolland 2022-06-16 17:53:38 +02:00
parent de4b8baba4
commit 9118f64a03
10 changed files with 94 additions and 56 deletions

View file

@ -1,17 +1,17 @@
resource "gandi_livedns_record" "gateways_exploreko_org" {
resource "gandi_livedns_record" "mongo_gateways" {
count = var.mongo_groups_count
zone = var.domain_name
name = "gateway${count.index}.teaching"
name = "gateway${count.index}.${var.subdomain_suffix}"
type = "A"
ttl = 3600
values = [openstack_compute_instance_v2.mongo_gateway[count.index].access_ip_v4]
values = [openstack_compute_instance_v2.mongo_gateways[count.index].access_ip_v4]
}
resource "gandi_livedns_record" "mongos_exploreko_org" {
count = var.mongo_replicas_count * var.mongo_groups_count
resource "gandi_livedns_record" "mongo_servers" {
count = var.mongo_replicas_count * var.mongo_groups_count
zone = var.domain_name
name = "mongo${count.index}.teaching"
name = "mongo${openstack_compute_instance_v2.mongo_servers[count.index].metadata.mongo_group_id}-${openstack_compute_instance_v2.mongo_servers[count.index].metadata.mongo_group_index}.${var.subdomain_suffix}"
type = "A"
ttl = 3600
values = [openstack_compute_instance_v2.mongo_servers[count.index].access_ip_v4]

9
terraform/fdj.tfvars Normal file
View file

@ -0,0 +1,9 @@
# mongo_groups_count = 10
mongo_gateways_enable = false
mongo_servers_enable = false
mongo_groups_count = 10
mongo_replicas_count = 1
ssh_private_key = "~/.ssh/keyring.syntaxio/francaise-des-jeux/id_ed25519"
ssh_public_key = "~/.ssh/keyring.syntaxio/francaise-des-jeux/id_ed25519.pub"
domain_name = "glenux.net"
subdomain_suffix = "teaching"

View file

@ -1,62 +1,55 @@
# Création d'une ressource de paire de clés SSH
resource "openstack_compute_keypair_v2" "provision_keypair" {
provider = openstack.ovh
provider = openstack.ovh
name = "provision_keypair"
public_key = file(var.ssh_public_key)
public_key = file(var.ssh_public_key)
}
resource "openstack_compute_instance_v2" "mongo_gateway" {
resource "openstack_compute_instance_v2" "mongo_gateways" {
count = var.mongo_groups_count
name = "prod-gateway${count.index}" # Nom de l'instance
provider = openstack.ovh # Nom du fournisseur
image_name = "Debian 11" # Nom de l'image
flavor_name = "s1-2" # Nom du type d'instance
provider = openstack.ovh # Nom du fournisseur
image_name = "Debian 11" # Nom de l'image
flavor_name = "s1-2" # Nom du type d'instance
# flavor_name = "s1-8" # Nom du type d'instance
# flavor_name = "d2-8" # Nom du type d'instance
# Nom de la ressource openstack_compute_keypair_v2 nommée test_keypair
key_pair = openstack_compute_keypair_v2.provision_keypair.name
power_state = var.mongo_servers_enable ? "active" : "shutoff"
metadata = {
ansible-group = "gateways"
mongo-group-id = count.index
ansible_group = "gateways"
mongo_group_id = count.index
}
# Ajoute le composant réseau pour atteindre votre instance
network {
name = "Ext-Net"
name = "Ext-Net"
}
# provisioner "local-exec" {
# command = "ansible-playbook -i inventories/terraform --private-key ${var.ssh_private_key} -e 'pub_key=${var.ssh_public_key}' playbook.yml --limit ${self.name}"
# working_dir = "../ansible"
# environment = {
# ANSIBLE_HOST_KEY_CHECKING = "False"
# }
# }
}
# Création d'une instance
resource "openstack_compute_instance_v2" "mongo_servers" {
count = var.mongo_replicas_count * var.mongo_groups_count
name = "prod-server${count.index}" # Nom de l'instance
provider = openstack.ovh # Nom du fournisseur
image_name = "Debian 11" # Nom de l'image
flavor_name = "s1-2" # Nom du type d'instance
provider = openstack.ovh # Nom du fournisseur
image_name = "Debian 11" # Nom de l'image
flavor_name = "s1-2" # Nom du type d'instance
# flavor_name = "s1-8" # Nom du type d'instance
# flavor_name = "d2-8" # Nom du type d'instance
# Nom de la ressource openstack_compute_keypair_v2 nommée test_keypair
key_pair = openstack_compute_keypair_v2.provision_keypair.name
power_state = var.mongo_servers_enable ? "active" : "shutoff"
metadata = {
ansible-group = "mongos"
mongo-group-id = floor(count.index / var.mongo_replicas_count)
mongo-group-index = count.index % var.mongo_replicas_count
ansible_group = "mongos"
mongo_group_id = floor(count.index / var.mongo_replicas_count)
mongo_group_index = count.index % var.mongo_replicas_count
}
# Ajoute le composant réseau pour atteindre votre instance
network {
name = "Ext-Net"
name = "Ext-Net"
}
# provisioner "local-exec" {

View file

@ -1,12 +0,0 @@
resource "local_file" "ansible_inventory" {
content = templatefile("templates/inventory.tmpl",
{
mongo_gateways = openstack_compute_instance_v2.mongo_gateway.*
mongo_servers = openstack_compute_instance_v2.mongo_servers.*
mongo_groups_count = var.mongo_groups_count
mongo_replicas_count = var.mongo_replicas_count
}
)
filename = "outputs/inventory"
file_permission = "0644"
}

16
terraform/outputs.tf Normal file
View file

@ -0,0 +1,16 @@
resource "local_file" "ansible_inventory" {
content = templatefile("templates/inventory.tmpl",
{
dns_gateways = gandi_livedns_record.mongo_gateways.*
dns_servers = gandi_livedns_record.mongo_gateways.*
mongo_gateways = openstack_compute_instance_v2.mongo_gateways.*
mongo_servers = openstack_compute_instance_v2.mongo_servers.*
mongo_groups_count = var.mongo_groups_count
mongo_replicas_count = var.mongo_replicas_count
}
)
filename = "outputs/inventory"
file_permission = "0644"
}

View file

@ -12,14 +12,14 @@ terraform {
}
gandi = {
source = "go-gandi/gandi"
source = "go-gandi/gandi"
version = "~> 2.0.1"
}
}
}
provider "gandi" {
key = "${var.gandi_key}"
key = var.gandi_key
}
provider "openstack" {

25
terraform/provision.tf Normal file
View file

@ -0,0 +1,25 @@
resource "null_resource" "ansible" {
count = (var.mongo_gateways_enable && var.mongo_servers_enable) ? 1 : 0
depends_on = [local_file.ansible_inventory]
triggers = {
always_run = "${timestamp()}"
}
provisioner "local-exec" {
environment = {
ANSIBLE_HOST_KEY_CHECKING = "False"
}
working_dir = "../ansible"
command = <<-EOT
ansible-playbook \
-i inventories/terraform \
--private-key ${var.ssh_private_key} \
-e 'pub_key=${var.ssh_public_key}' \
playbook.yml
EOT
}
}

View file

@ -1,4 +0,0 @@
# provisioner "local-exec" {
# command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i outputs/inventory --private-key ${var.private_key} -e 'pub_key=${var.pub_key}' playbook.yml"
# }

View file

@ -1,8 +1,8 @@
%{ for index, instance in mongo_gateways ~}
${ instance.name } ansible_user=debian ansible_host=${instance.network[0].fixed_ip_v4}
${ instance.name } ansible_user=debian ansible_host=${instance.network[0].fixed_ip_v4} sshwifty_hostname=${dns_gateways[index].name}.${dns_gateways[index].zone} mongo_group_id=${instance.metadata.mongo_group_id}
%{ endfor ~}
%{ for index, instance in mongo_servers ~}
${ instance.name } ansible_user=debian ansible_host=${instance.network[0].fixed_ip_v4}
${ instance.name } ansible_user=debian ansible_host=${instance.network[0].fixed_ip_v4} mongo_group_id=${instance.metadata.mongo_group_id} mongo_group_index=${instance.metadata.mongo_group_index}
%{ endfor ~}
[stage_development]

View file

@ -1,12 +1,22 @@
variable "mongo_gateways_enable" {
default = false
type = bool
}
variable "mongo_servers_enable" {
default = false
type = bool
}
variable "mongo_groups_count" {
default = "1"
description = "How many replicas per mongo"
default = "1"
description = "How many replicas per mongo"
}
variable "mongo_replicas_count" {
default = "2"
description = "How many replicas per mongo group"
default = "2"
description = "How many replicas per mongo group"
}
variable "ssh_private_key" {}
@ -17,4 +27,5 @@ variable "gandi_key" {}
variable "domain_name" {}
variable "subdomain_suffix" {
}