feat(terraform): write provisioned IPv4 into dynamic ansible inventory

This commit is contained in:
2026-08-14 18:45:24 +02:00
parent 8255ac97e2
commit 116f2777a1
3 changed files with 24 additions and 10 deletions
+18 -10
View File
@@ -1,5 +1,4 @@
## DATA SOURCES
# Gather facts
data "ovh_cloud_instance_flavors" "flavors" {
service_name = var.ovh_service_name
region = var.ovh_region
@@ -10,15 +9,13 @@ data "ovh_cloud_instance_images" "images" {
region = var.ovh_region
}
## RESOURCES
resource "ovh_cloud_ssh_key" "ansible" {
service_name = var.ovh_service_name
name = "ansible-key"
public_key = var.ssh_public_key
}
# Provision the d2-2 Public Cloud Instance
# Provision a d2-2 Public Cloud instance
resource "ovh_cloud_instance" "coolify" {
service_name = var.ovh_service_name
region = var.ovh_region
@@ -40,14 +37,25 @@ resource "ovh_cloud_instance" "coolify" {
]
}
## OUTPUTS
output "public_ip" {
description = "The public IPv4 address of the provisioned OVHcloud VM"
value = one(flatten([
# Fetch provisioned VM's public IPv4
locals {
public_ip = one(flatten([
for net in ovh_cloud_instance.coolify.current_state.networks : [
for addr in net.addresses : addr.ip
if addr.type == "FIXED" && addr.version == 4
]
]))
}
# Write IP to Ansible inventory
resource "local_file" "ansible_inventory" {
content = templatefile("${path.module}/../ansible/inventory.tpl", {
public_ip = local.public_ip
})
filename = "${path.module}/../ansible/inventory.ini"
}
output "public_ip" {
description = "The public IPv4 address of the provisioned OVHcloud VM"
value = local.public_ip
}