Tsunami/main.tf

97 lines
2.2 KiB
Terraform
Raw Normal View History

2022-06-29 22:32:51 +02:00
data "netbox_prefix" "network" {
2022-04-25 13:50:01 +02:00
cidr = var.netbox_prefix
}
resource "netbox_available_ip_address" "available_ip" {
2022-06-29 22:32:51 +02:00
prefix_id = data.netbox_prefix.network.id
2022-04-25 13:50:01 +02:00
description = var.name
dns_name = var.dns_name
}
output "ip_addr" {
value = netbox_available_ip_address.available_ip.ip_address
2022-04-25 13:50:01 +02:00
}
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" "bar" {
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)}"
2022-04-25 13:50:01 +02:00
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 = [
"Debian",
"Best Distro",
]
// 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]}
2022-04-25 13:50:01 +02:00
EOF
}
resource "time_sleep" "wait_30_seconds" {
depends_on = [xenorchestra_vm.bar]
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]
}