Tsunami/main.tf
GnomeZworc 268a346a19
add the option to add tags at deployement
I made this things a vars

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2022-07-01 18:48:48 +02:00

94 lines
2.1 KiB
HCL

data "netbox_prefix" "network" {
cidr = var.netbox_prefix
}
resource "netbox_available_ip_address" "available_ip" {
prefix_id = data.netbox_prefix.network.id
description = var.name
dns_name = var.dns_name
}
output "ip_addr" {
value = netbox_available_ip_address.available_ip.ip_address
}
data "xenorchestra_sr" "local_storage" {
name_label = var.storage
}
data "xenorchestra_network" "network" {
name_label = var.network
pool_id = data.xenorchestra_pool.pool.id
}
data "xenorchestra_pool" "pool" {
name_label = var.pool
}
data "xenorchestra_template" "template" {
name_label = var.template
}
resource "xenorchestra_vm" "vm_deployed" {
memory_max = var.ram * 1024 * 1024 * 1024
cpus = var.cpu
cloud_config = templatefile("cloud_config.tftpl", {
hostname = var.name
ssh_keys = var.ssh_keys
})
cloud_network_config = templatefile("cloud_network_config.tftpl", {
ip = "${replace(netbox_available_ip_address.available_ip.ip_address, "var.netmask_netbox", var.netmask)}"
gateway = var.gateway
})
name_label = var.name
name_description = var.desc
template = data.xenorchestra_template.template.id
# Prefer to run the VM on the primary pool instance
affinity_host = data.xenorchestra_pool.pool.master
network {
network_id = data.xenorchestra_network.network.id
}
disk {
sr_id = data.xenorchestra_sr.local_storage.id
name_label = var.name
size = var.disk_size * 1024 * 1024 * 1024
}
tags = var.vm_tags
// Override the default create timeout from 5 mins to 20.
timeouts {
create = "20m"
}
}
# Generate inventory file
resource "local_file" "inventory" {
filename = "inventory"
content = <<EOF
[all]
${split("/", netbox_available_ip_address.available_ip.ip_address)[0]}
EOF
}
resource "time_sleep" "wait_30_seconds" {
depends_on = [xenorchestra_vm.vm_deployed]
create_duration = "30s"
}
resource "null_resource" "run-ansible" {
provisioner "local-exec" {
command = "ansible-playbook -D -i inventory playbooks/${var.playbook} "
environment = {
ANSIBLE_HOST_KEY_CHECKING = "false"
}
}
depends_on = [time_sleep.wait_30_seconds]
}