the terransible project can deploy on one or multiple nodes

This commit is contained in:
Vezpi 2025-03-28 22:46:13 +00:00
parent c3db663e9e
commit bc19a7638b
5 changed files with 62 additions and 16 deletions

View File

@ -3,8 +3,7 @@
hosts: localhost
gather_facts: false
tasks:
- name: Create a VM with Terraform
ansible.builtin.import_role:
- ansible.builtin.import_role:
name: terraform_vm
vars:
terraform_vm_project_path: /home/vez/homelab/terraform/projects/terransible

View File

@ -1,5 +1,5 @@
---
- name: Create a VM
- name: Deploy a Terraform infrastructure
cloud.terraform.terraform:
project_path: "{{ terraform_vm_project_path }}"
state: "{{ terraform_vm_state }}"

View File

@ -1,24 +1,47 @@
module "pve_vm" {
source = "../../modules/pve_vm"
node_name = "zenith"
vm_name = "zenith-vm"
vm_cpu = 2
vm_ram = 2048
vm_vlan = 66
source = "../../modules/pve_vm"
for_each = local.vm_list
node_name = each.value.node_name
vm_name = each.value.vm_name
vm_cpu = each.value.vm_cpu
vm_ram = each.value.vm_ram
vm_vlan = each.value.vm_vlan
}
locals {
all_nodes = data.proxmox_virtual_environment_nodes.pve_nodes.names
selected_nodes = var.multi_node_deployment == false ? [var.target_node] : local.all_nodes
vm_list = {
for vm in flatten([
for node in local.selected_nodes : [
for role, config in var.vm_attr : {
node_name = node
vm_name = "${node}-${role}"
vm_cpu = config.cpu
vm_ram = config.ram
vm_vlan = config.vlan
}
]
]) : vm.vm_name => vm
}
}
data "proxmox_virtual_environment_nodes" "pve_nodes" {}
output "vm_ip" {
value = module.pve_vm.vm_ip
value = { for k, v in module.pve_vm : k => v.vm_ip }
}
resource "ansible_group" "servers" {
name = "servers"
}
resource "ansible_host" "vm" {
name = "zenith-vm.lab.vezpi.me"
groups = ["servers"]
for_each = module.pve_vm
name = each.key
variables = {
ansible_host = module.pve_vm.vm_ip
ansible_host = each.value.vm_ip
}
}
groups = ["servers"]
}

View File

@ -4,7 +4,7 @@ terraform {
source = "bpg/proxmox"
}
ansible = {
source = "ansible/ansible"
source = "ansible/ansible"
}
}
}

View File

@ -7,4 +7,28 @@ variable "proxmox_api_token" {
description = "Proxmox API token"
type = string
sensitive = true
}
variable "multi_node_deployment" {
description = "true : deploy VMs on each node, false : deploy only on a given node"
type = bool
default = true
}
variable "target_node" {
description = "Node which host the VM if multi_node_deployment = false"
type = string
default = ""
}
variable "vm_attr" {
description = "VM attributes"
type = map(object({
ram = number
cpu = number
vlan = number
}))
default = {
"vm" = { ram = 2048, cpu = 2, vlan = 66 }
}
}