diff --git a/ansible/inventory.tpl b/ansible/inventory.tpl new file mode 100644 index 0000000..1fb2aa2 --- /dev/null +++ b/ansible/inventory.tpl @@ -0,0 +1,2 @@ +[coolify_vm] +${public_ip} ansible_user=debian diff --git a/terraform/main.tf b/terraform/main.tf index 4bef090..0974336 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -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 +} diff --git a/terraform/providers.tf b/terraform/providers.tf index 89daf4f..aafeab1 100644 --- a/terraform/providers.tf +++ b/terraform/providers.tf @@ -5,6 +5,10 @@ terraform { source = "ovh/ovh" version = "~> 2.19.0" } + local = { + source = "hashicorp/local" + version = "~> 2.9.0" + } } }