generated from CC/VMServiceTemplate
Compare commits
53 Commits
898ee9359f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e785930578 | |||
|
|
92f9bd459d | ||
|
|
8518bd6064 | ||
|
|
3c7a22032a | ||
|
|
a363aafd97 | ||
|
|
e40fb6742a | ||
|
|
da2362d0a9 | ||
|
|
684a86385b | ||
|
|
eded1b9777 | ||
|
|
80c611ecda | ||
|
|
2d27326cbc | ||
|
|
325d61bef0 | ||
|
|
4bff0f9703 | ||
| 2f505e5062 | |||
|
|
d73dd0919d | ||
|
|
ba593526bd | ||
|
|
adc4188918 | ||
|
|
5e4d5c82d8 | ||
|
|
31cc759d4d | ||
|
|
afbd95d765 | ||
|
|
ae70feaaf3 | ||
|
|
d68ab4b309 | ||
|
|
1cb81833d7 | ||
|
|
bf11724516 | ||
|
|
317bf4be62 | ||
|
|
edaff5a978 | ||
|
|
7f679b6ae2 | ||
|
|
223b287c08 | ||
|
|
fdacdac871 | ||
| 4518292aaa | |||
|
|
d9f90bda47 | ||
|
|
e28da5fc4d | ||
|
|
46a1da7642 | ||
|
|
5e43f5c16e | ||
|
|
dc320bb8ae | ||
|
|
0ac0357a45 | ||
|
|
227b7ed138 | ||
|
|
14de604ecd | ||
|
|
d00da56cc0 | ||
|
|
3e67c5c708 | ||
|
|
a8f23e0877 | ||
|
|
3fe855703c | ||
|
|
312c4ce78a | ||
|
|
22de99e32c | ||
|
|
bdb341fd29 | ||
|
|
65538d2aa6 | ||
|
|
089b099f7f | ||
|
|
db9cc7909b | ||
|
|
26ab15b8b3 | ||
|
|
e4fe0859b3 | ||
|
|
cef63843bb | ||
|
|
a07466de94 | ||
|
|
aa91b12ca9 |
@@ -1,6 +1,8 @@
|
||||
name: Deploy VM and App
|
||||
|
||||
on:
|
||||
push:
|
||||
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
tfvars_file:
|
||||
@@ -13,45 +15,64 @@ on:
|
||||
- multi.tfvars.example
|
||||
|
||||
jobs:
|
||||
terraform:
|
||||
terraform-ansible-deploy:
|
||||
runs-on: ubuntu-latest
|
||||
defaults:
|
||||
run:
|
||||
working-directory: terraform
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: hashicorp/setup-terraform@v3
|
||||
- name: Check files & Select tfvars
|
||||
shell: bash
|
||||
run: |
|
||||
#rm ansible/inventory/inventory.yml
|
||||
mkdir -p ansible/inventory
|
||||
cd terraform
|
||||
#rm vm_data.yml
|
||||
cp "${{ inputs.tfvars_file || 'single.tfvars.example' }}" terraform.tfvars
|
||||
|
||||
- name: Select tfvars
|
||||
run: cp "${{ inputs.tfvars_file }}" terraform.tfvars
|
||||
- uses: hashicorp/setup-terraform@v4
|
||||
|
||||
- name: Check path
|
||||
run: pwd
|
||||
|
||||
- name: Terraform init
|
||||
run: terraform init
|
||||
working-directory: "terraform"
|
||||
|
||||
- name: Terraform apply
|
||||
run: terraform apply -auto-approve
|
||||
|
||||
- name: Write inventory
|
||||
run: |
|
||||
mkdir -p ../ansible/inventory
|
||||
terraform output -json vm_ipv4_addresses | jq -r '
|
||||
to_entries[] | "[app]\n\(.value) ansible_user=cloud"
|
||||
' > ../ansible/inventory/hosts.ini
|
||||
|
||||
- name: Write tags
|
||||
run: terraform output -json vm_tags > ../ansible/vm_tags.json
|
||||
|
||||
deploy:
|
||||
needs: terraform
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
working-directory: "terraform"
|
||||
|
||||
- name: Install Ansible
|
||||
shell: bash
|
||||
run: |
|
||||
python3 -m pip install --upgrade pip
|
||||
pip install ansible community.docker
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y ansible rsync
|
||||
|
||||
- name: Deploy app
|
||||
run: ansible-playbook -i ansible/inventory/hosts.ini ansible/playbooks/deploy.yml
|
||||
- name: Set up SSH
|
||||
run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/vlans_rsa
|
||||
chmod 600 ~/.ssh/vlans_rsa
|
||||
cat > ~/.ssh/config <<'EOF'
|
||||
Host *
|
||||
StrictHostKeyChecking no
|
||||
UserKnownHostsFile=/dev/null
|
||||
EOF
|
||||
|
||||
- name: Run playbook
|
||||
run: |
|
||||
ansible-playbook ansible/playbooks/docker_copy.yml -i ansible/inventory/inventory.yml -u cloud --private-key ~/.ssh/vlans_rsa
|
||||
|
||||
- name: Configure Git
|
||||
run: |
|
||||
git config user.name "git-bot"
|
||||
git config user.email "got-bot@text.com"
|
||||
|
||||
- name: Commit and push to Gitea
|
||||
run: |
|
||||
git remote set-url origin https://$GITEA_USERNAME:${{ secrets.GIT_BOT_TOKEN }}@tea.charcarservices.uk/CC/N8N.git
|
||||
git add terraform/vm_data.yml ansible/inventory/inventory.yml
|
||||
git diff --cached --quiet || git commit -m "chore: update terraform outputs"
|
||||
git push origin HEAD:main
|
||||
env:
|
||||
GITEA_USERNAME: git-bot # or your bot account
|
||||
|
||||
36
.gitea/workflows/destroy_module.yml
Normal file
36
.gitea/workflows/destroy_module.yml
Normal file
@@ -0,0 +1,36 @@
|
||||
name: Terraform Destroy
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
working_directory:
|
||||
description: "Working directory for the module"
|
||||
required: false
|
||||
default: "terraform"
|
||||
type: string
|
||||
|
||||
jobs:
|
||||
destroy:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
defaults:
|
||||
run:
|
||||
shell: bash
|
||||
working-directory: ${{ inputs.working_directory }}
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Terraform
|
||||
uses: hashicorp/setup-terraform@v4
|
||||
|
||||
- name: Terraform Init
|
||||
run: terraform init
|
||||
|
||||
- name: Terraform Destroy Plan
|
||||
run: terraform plan -destroy -out=tfplan
|
||||
continue-on-error: false
|
||||
|
||||
- name: Terraform Destroy
|
||||
run: terraform destroy -auto-approve
|
||||
@@ -142,4 +142,4 @@ This template works well for service VMs such as:
|
||||
- Jellyfin
|
||||
- Code Server
|
||||
- Monitoring or proxy VMs
|
||||
|
||||
|
||||
8
ansible/inventory/inventory.yml
Executable file
8
ansible/inventory/inventory.yml
Executable file
@@ -0,0 +1,8 @@
|
||||
"all":
|
||||
"children":
|
||||
"n8n":
|
||||
"hosts":
|
||||
"n8n-01":
|
||||
"ansible_host": "192.168.10.107"
|
||||
"vars":
|
||||
"ansible_user": "cloud"
|
||||
8
ansible/playbooks/docker_copy.yml
Normal file
8
ansible/playbooks/docker_copy.yml
Normal file
@@ -0,0 +1,8 @@
|
||||
- hosts: all
|
||||
become: true
|
||||
vars:
|
||||
folder_name: n8n
|
||||
|
||||
roles:
|
||||
- role: docker
|
||||
config_flavor: copy
|
||||
15
ansible/playbooks/roles/docker/defaults/main.yml
Normal file
15
ansible/playbooks/roles/docker/defaults/main.yml
Normal file
@@ -0,0 +1,15 @@
|
||||
docker_comparisons:
|
||||
env: strict
|
||||
labels: strict
|
||||
|
||||
docker_image_name_mismatch: recreate
|
||||
|
||||
docker_state: started
|
||||
|
||||
docker_restart_policy: unless-stopped
|
||||
|
||||
docker_pull: "missing"
|
||||
|
||||
gather_facts: true
|
||||
|
||||
config_flavor: none
|
||||
17
ansible/playbooks/roles/docker/tasks/docker_copy.yml
Normal file
17
ansible/playbooks/roles/docker/tasks/docker_copy.yml
Normal file
@@ -0,0 +1,17 @@
|
||||
---
|
||||
# Copy directory recursively to remote host
|
||||
|
||||
- name: Copy project directory to remote
|
||||
ansible.builtin.synchronize:
|
||||
src: ../compose/{{ folder_name }}
|
||||
dest: /home/cloud/
|
||||
mode: push
|
||||
|
||||
|
||||
- name: Start Compose stack
|
||||
community.docker.docker_compose_v2:
|
||||
project_src: /home/cloud/{{ folder_name }}
|
||||
build: always
|
||||
pull: always
|
||||
state: present
|
||||
|
||||
27
ansible/playbooks/roles/docker/tasks/docker_destroy.yml
Normal file
27
ansible/playbooks/roles/docker/tasks/docker_destroy.yml
Normal file
@@ -0,0 +1,27 @@
|
||||
- name: Get running containers
|
||||
docker_host_info:
|
||||
containers: yes
|
||||
register: docker_info
|
||||
|
||||
- name: Stop running containers
|
||||
docker_container:
|
||||
name: "{{ item }}"
|
||||
state: stopped
|
||||
loop: "{{ docker_info.containers | map(attribute='Id') | list }}"
|
||||
|
||||
- name: Remove Stoped docker containers
|
||||
shell: |
|
||||
docker rm $(docker ps -a -q);
|
||||
when: docker_info.containers != 0
|
||||
|
||||
- name: Get details of all images
|
||||
docker_host_info:
|
||||
images: yes
|
||||
verbose_output: yes
|
||||
register: image_info
|
||||
|
||||
- name: Remove all images
|
||||
docker_image:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
loop: "{{ image_info.images | map(attribute='Id') | list }}"
|
||||
23
ansible/playbooks/roles/docker/tasks/docker_install.yml
Normal file
23
ansible/playbooks/roles/docker/tasks/docker_install.yml
Normal file
@@ -0,0 +1,23 @@
|
||||
- name: Install gpg
|
||||
ansible.builtin.apt:
|
||||
name: gpg
|
||||
|
||||
- name: Add Docker repository key
|
||||
ansible.builtin.apt_key:
|
||||
url: https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg
|
||||
keyring: /etc/apt/trusted.gpg.d/docker.gpg
|
||||
|
||||
- name: Add Docker repository
|
||||
ansible.builtin.apt_repository:
|
||||
# Use HTTP to enable apt-cacher
|
||||
repo: deb http://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable
|
||||
filename: docker
|
||||
|
||||
- name: Install Docker
|
||||
ansible.builtin.apt:
|
||||
name: "{{ item }}"
|
||||
loop:
|
||||
- docker-ce
|
||||
- docker-ce-cli
|
||||
- containerd.io
|
||||
|
||||
17
ansible/playbooks/roles/docker/tasks/main.yml
Normal file
17
ansible/playbooks/roles/docker/tasks/main.yml
Normal file
@@ -0,0 +1,17 @@
|
||||
---
|
||||
- name: Docker Install
|
||||
include_tasks: docker_install.yml
|
||||
when: config_flavor == "install"
|
||||
|
||||
- name: Docker Stop & Destroy
|
||||
include_tasks: docker_destroy.yml
|
||||
when: config_flavor == "destroy"
|
||||
|
||||
|
||||
- name: Docker Transfer Compose to Remote Host
|
||||
include_tasks: docker_copy.yml
|
||||
when: config_flavor == "copy"
|
||||
|
||||
#####
|
||||
# You need to set up each docker playbook to a config flavor or look for an input module and assign config_flavor to it
|
||||
######
|
||||
22
ansible/playbooks/tags.yml
Normal file
22
ansible/playbooks/tags.yml
Normal file
@@ -0,0 +1,22 @@
|
||||
---
|
||||
- name: Update Proxmox VM tags
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
|
||||
vars_files:
|
||||
- ../terraform/vm_data.yml
|
||||
|
||||
tasks:
|
||||
- name: Update tags on each VM
|
||||
community.proxmox.proxmox_kvm:
|
||||
api_user: "{{ lookup('env', 'PROXMOX_USER') }}"
|
||||
api_token_id: "{{ lookup('env', 'PROXMOX_TOKEN_ID') }}"
|
||||
api_token_secret: "{{ lookup('env', 'PROXMOX_TOKEN_SECRET') }}"
|
||||
api_host: "{{ lookup('env', 'PROXMOX_HOST') }}"
|
||||
validate_certs: true
|
||||
node: "{{ item.value.node_name }}"
|
||||
name: "{{ item.value.vm_name }}"
|
||||
state: present
|
||||
update: true
|
||||
tags: "{{ item.value.tags }}"
|
||||
loop: "{{ vm_tag_data | dict2items }}"
|
||||
@@ -2,11 +2,25 @@ locals {
|
||||
instance_map = var.instance_mode == "single" ? {
|
||||
main = var.instance
|
||||
} : var.instances
|
||||
|
||||
vm_created = {
|
||||
for k, v in local.instance_map :
|
||||
k => {
|
||||
service_name = v.service_name
|
||||
vm_name = v.vm_name
|
||||
node_name = v.node_name
|
||||
ipv4_address = module.vm-n8n[k].vm_ipv4_address
|
||||
vm_tags = concat(
|
||||
try(v.vm_tags, []),
|
||||
["terraform", "docker", v.service_name, "ip-${replace(module.vm-n8n[k].vm_ipv4_address, ".", "-")}"]
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module "each.value.vm_name" {
|
||||
module "vm-n8n" {
|
||||
for_each = local.instance_map
|
||||
source = "./modules/proxmox_ubuntu_cloudinit_template"
|
||||
source = "./modules/proxmox_ubuntu_cloudinit_clone"
|
||||
|
||||
vm_name = each.value.vm_name
|
||||
node_name = each.value.node_name
|
||||
@@ -23,3 +37,15 @@ module "each.value.vm_name" {
|
||||
)
|
||||
vm_user_sshkey = var.vm_defaults.vm_user_sshkey
|
||||
}
|
||||
|
||||
module "inventory" {
|
||||
source = "./modules/proxmox_ansible_inventory"
|
||||
filename = "${abspath("${path.root}/..")}/ansible/inventory/inventory.yml"
|
||||
instances = local.vm_created
|
||||
}
|
||||
|
||||
module "vm_data" {
|
||||
source = "./modules/proxmox_vm_data"
|
||||
filename = "${abspath("${path.root}/..")}/terraform/vm_data.yml"
|
||||
instances = local.vm_created
|
||||
}
|
||||
|
||||
26
terraform/modules/proxmox_ansible_inventory/main.tf
Normal file
26
terraform/modules/proxmox_ansible_inventory/main.tf
Normal file
@@ -0,0 +1,26 @@
|
||||
locals {
|
||||
inventory = {
|
||||
all = {
|
||||
vars = {
|
||||
ansible_user = "cloud"
|
||||
}
|
||||
children = {
|
||||
for svc in distinct([for k, v in var.instances : v.service_name]) :
|
||||
svc => {
|
||||
hosts = {
|
||||
for k, v in var.instances :
|
||||
v.vm_name => {
|
||||
ansible_host = v.ipv4_address
|
||||
}
|
||||
if v.service_name == svc
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "local_file" "inventory" {
|
||||
filename = var.filename
|
||||
content = yamlencode(local.inventory)
|
||||
}
|
||||
7
terraform/modules/proxmox_ansible_inventory/outputs.tf
Normal file
7
terraform/modules/proxmox_ansible_inventory/outputs.tf
Normal file
@@ -0,0 +1,7 @@
|
||||
output "filename" {
|
||||
value = local_file.inventory.filename
|
||||
}
|
||||
|
||||
output "content" {
|
||||
value = local_file.inventory.content
|
||||
}
|
||||
13
terraform/modules/proxmox_ansible_inventory/variables.tf
Normal file
13
terraform/modules/proxmox_ansible_inventory/variables.tf
Normal file
@@ -0,0 +1,13 @@
|
||||
variable "filename" {
|
||||
description = "Path to write the inventory.yml file"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "instances" {
|
||||
description = "Normalized instance map keyed by instance key"
|
||||
type = map(object({
|
||||
service_name = string
|
||||
vm_name = string
|
||||
ipv4_address = string
|
||||
}))
|
||||
}
|
||||
123
terraform/modules/proxmox_ubuntu_cloudinit_clone/main.tf
Normal file
123
terraform/modules/proxmox_ubuntu_cloudinit_clone/main.tf
Normal file
@@ -0,0 +1,123 @@
|
||||
locals {
|
||||
selected_instance = one([
|
||||
for cfg in var.instance_configs :
|
||||
cfg if cfg.crispy_name == var.node_name
|
||||
])
|
||||
}
|
||||
|
||||
resource "proxmox_virtual_environment_file" "cloud_config" {
|
||||
content_type = "snippets"
|
||||
datastore_id = "local"
|
||||
node_name = "pop"
|
||||
|
||||
source_raw {
|
||||
file_name = "vm.cloud-config.yaml" # The name of the snippet file
|
||||
data = <<-EOF
|
||||
#cloud-config
|
||||
hostname: ${var.vm_name}
|
||||
|
||||
package_update: true
|
||||
package_upgrade: true
|
||||
|
||||
system_info:
|
||||
default_user:
|
||||
groups: [ docker ]
|
||||
|
||||
users:
|
||||
- default
|
||||
- name: cloud
|
||||
groups:
|
||||
- sudo
|
||||
- docker
|
||||
shell: /bin/bash
|
||||
ssh-authorized-keys:
|
||||
- "${var.vm_user_sshkey}" # Inject user's SSH key
|
||||
sudo: ALL=(ALL) NOPASSWD:ALL
|
||||
|
||||
packages:
|
||||
- qemu-guest-agent
|
||||
- apt-transport-https
|
||||
- ca-certificates
|
||||
- curl
|
||||
- gnupg
|
||||
- lsb-release
|
||||
- unattended-upgrades
|
||||
|
||||
runcmd:
|
||||
- systemctl enable qemu-guest-agent
|
||||
- mkdir -p /etc/apt/keyrings
|
||||
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
||||
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
|
||||
- apt-get update
|
||||
- apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
|
||||
- systemctl enable docker
|
||||
- systemctl start docker
|
||||
- reboot
|
||||
EOF
|
||||
}
|
||||
}
|
||||
|
||||
resource "proxmox_virtual_environment_vm" "ubuntu_22_minimal_clone" {
|
||||
name = var.vm_name # VM name
|
||||
node_name = var.node_name # Proxmox node to deploy the VM
|
||||
tags = var.vm_tags # Optional VM tags for categorization
|
||||
|
||||
agent {
|
||||
enabled = true # Enable the QEMU guest agent
|
||||
}
|
||||
|
||||
stop_on_destroy = true # Ensure VM is stopped gracefully when destroyed
|
||||
|
||||
|
||||
clone {
|
||||
vm_id = local.selected_instance.vmid # ID of the source template
|
||||
node_name = local.selected_instance.crispy_name # Node of the source template
|
||||
}
|
||||
|
||||
bios = var.vm_bios # BIOS type (e.g., seabios or ovmf)
|
||||
machine = var.vm_machine # Machine type (e.g., q35)
|
||||
|
||||
cpu {
|
||||
cores = var.vm_cpu # Number of CPU cores
|
||||
type = "host" # Use host CPU type for best compatibility/performance
|
||||
}
|
||||
|
||||
memory {
|
||||
dedicated = var.vm_ram # RAM in MB
|
||||
}
|
||||
|
||||
disk {
|
||||
datastore_id = var.node_datastore # Datastore to hold the disk
|
||||
interface = "scsi0" # Primary disk interface
|
||||
size = var.vm_size
|
||||
}
|
||||
|
||||
|
||||
initialization {
|
||||
user_data_file_id = proxmox_virtual_environment_file.cloud_config.id # Link the cloud-init file
|
||||
datastore_id = var.node_datastore
|
||||
interface = "scsi1" # Separate interface for cloud-init
|
||||
ip_config {
|
||||
ipv4 {
|
||||
address = "dhcp" # Get IP via DHCP
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
network_device {
|
||||
bridge = var.bridge # Use the default bridge
|
||||
}
|
||||
|
||||
lifecycle {
|
||||
ignore_changes = [ # Ignore initialization section after first depoloyment for idempotency
|
||||
initialization
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
output "vm_ipv4_address" {
|
||||
value = proxmox_virtual_environment_vm.ubuntu_22_minimal_clone.ipv4_addresses[1][0]
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
proxmox = {
|
||||
source = "bpg/proxmox"
|
||||
version = "0.106.0"
|
||||
#url = https://registry.terraform.io/providers/bpg/proxmox/latest/docs/guides/clone-vm
|
||||
}
|
||||
aws = {
|
||||
source = "hashicorp/aws"
|
||||
version = "6.38.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
140
terraform/modules/proxmox_ubuntu_cloudinit_clone/variables.tf
Normal file
140
terraform/modules/proxmox_ubuntu_cloudinit_clone/variables.tf
Normal file
@@ -0,0 +1,140 @@
|
||||
variable "pm_api_url" { default = "https://192.168.10.201:8006/api2/json" }
|
||||
variable "pm_api_token" { default = "terraform@pve!provider=760580c4-5c1f-462b-986a-dd244c6c95f2" }
|
||||
|
||||
variable "storage" { default = "hlst" }
|
||||
variable "bridge" { default = "vmbr0" }
|
||||
|
||||
variable "os_type" {
|
||||
default = "alpine"
|
||||
}
|
||||
|
||||
variable "instance_configs" {
|
||||
type = list(object({
|
||||
crispy_name = string
|
||||
vmid = string
|
||||
}))
|
||||
default = [
|
||||
{ crispy_name = "snap", vmid = "9002" },
|
||||
{ crispy_name = "crackle", vmid = "9000" },
|
||||
{ crispy_name = "pop", vmid = "900" }
|
||||
]
|
||||
}
|
||||
|
||||
variable "clone_count" {
|
||||
type = number
|
||||
default = 1
|
||||
}
|
||||
|
||||
variable "vm_count" { default = 1 }
|
||||
variable "name_prefix" { default = "dev" }
|
||||
variable "vm_ram" { default = 2048 }
|
||||
variable "vm_cpu" { default = 1 }
|
||||
variable "vm_size" { default = 10 }
|
||||
variable "vm_bios" {
|
||||
description = "Type of BIOS used for the VM"
|
||||
type = string
|
||||
default = "ovmf"
|
||||
}
|
||||
|
||||
variable "vm_machine" {
|
||||
description = "Type of machine used for the VM"
|
||||
type = string
|
||||
default = "q35"
|
||||
}
|
||||
|
||||
variable "vm_tags" {
|
||||
description = "Tags for the VM"
|
||||
type = list(any)
|
||||
default = ["test", "terraform"]
|
||||
}
|
||||
|
||||
variable "ipconfig0" { default = "ip=dhcp" }
|
||||
|
||||
variable "access_key" {
|
||||
type = string
|
||||
default = "GK242d456c0692a9d4cc102206"
|
||||
}
|
||||
|
||||
variable "secret_key" {
|
||||
type = string
|
||||
default = "1d7e22b7a8892cb11b569017659aa511b37b53287c4d1699c310d9f8ac76df09"
|
||||
}
|
||||
|
||||
variable "region" {
|
||||
type = string
|
||||
default = "garage"
|
||||
}
|
||||
|
||||
variable "endpoints_s3" {
|
||||
type = string
|
||||
description = "S3 endpoint"
|
||||
default = "http://192.168.10.109:3909"
|
||||
}
|
||||
|
||||
variable "skip_credentials_validation" {
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "skip_requesting_account_id" {
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "skip_metadata_api_check" {
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "skip_region_validation" {
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "use_path_style" {
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "use_lockfile" {
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "vm_name" {
|
||||
description = "Hostname of the VM"
|
||||
type = string
|
||||
default = "Lab"
|
||||
}
|
||||
|
||||
variable "vm_user_sshkey" {
|
||||
description = "Admin user SSH key of the VM"
|
||||
type = string
|
||||
default = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRlWaLBt/qmWY01Cd6jN/YxLnlT+6lg+evEdN/dIajirdTj1rCbAdlG3WYvo+4BpN17HK3/eGQpGUMbgI/8MVd8YPODcD34gaNX0w2v66BwHx+S6BZUpz5T2IoQT0JtSv/TtFICoff5gXdNRpfd4eWsmTioEqLA6oToJLE4dn3jvAzFi9y7fyLqvuoQMmPidYYJjGT30eiULtXNspoEP+GmuWmVEu+znzMWaKDWKdOsii4Cv1aWCRKSDDRzDBrZI2mP+Vm4HDQBdgDYRw4ehumMDtfaSjyJCnrk691bIM+wxzICuIEecg5kq5HcUPvo2mFyWPAEXb5xlXnuopYEBd7 Generated By NeoServer"
|
||||
}
|
||||
|
||||
|
||||
variable "node_name" {
|
||||
description = "Proxmox host for the VM"
|
||||
type = string
|
||||
default = "pop"
|
||||
}
|
||||
|
||||
variable "node_datastore" {
|
||||
description = "Datastore used for VM storage"
|
||||
type = string
|
||||
default = "hlst"
|
||||
}
|
||||
|
||||
variable "vm_template" {
|
||||
description = "Template of the VM"
|
||||
type = string
|
||||
default = "ubuntu-cloud"
|
||||
}
|
||||
|
||||
|
||||
variable "vm_user" {
|
||||
description = "Admin user of the VM"
|
||||
type = string
|
||||
default = "cloud"
|
||||
}
|
||||
17
terraform/modules/proxmox_vm_data/main.tf
Normal file
17
terraform/modules/proxmox_vm_data/main.tf
Normal file
@@ -0,0 +1,17 @@
|
||||
locals {
|
||||
vm_data = {
|
||||
vm_tag_data = {
|
||||
for k, v in var.instances :
|
||||
k => {
|
||||
node_name = v.node_name
|
||||
vm_name = v.vm_name
|
||||
tags = v.vm_tags
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "local_file" "vm_data" {
|
||||
filename = var.filename
|
||||
content = yamlencode(local.vm_data)
|
||||
}
|
||||
7
terraform/modules/proxmox_vm_data/outputs.tf
Normal file
7
terraform/modules/proxmox_vm_data/outputs.tf
Normal file
@@ -0,0 +1,7 @@
|
||||
output "filename" {
|
||||
value = local_file.vm_data.filename
|
||||
}
|
||||
|
||||
output "content" {
|
||||
value = local_file.vm_data.content
|
||||
}
|
||||
15
terraform/modules/proxmox_vm_data/variables.tf
Normal file
15
terraform/modules/proxmox_vm_data/variables.tf
Normal file
@@ -0,0 +1,15 @@
|
||||
variable "filename" {
|
||||
description = "Path to write the vm_data.yml file"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "instances" {
|
||||
description = "Normalized instance map keyed by instance key"
|
||||
type = map(object({
|
||||
service_name = string
|
||||
vm_name = string
|
||||
node_name = string
|
||||
ipv4_address = string
|
||||
vm_tags = list(string)
|
||||
}))
|
||||
}
|
||||
@@ -1,20 +1,41 @@
|
||||
output "vm_ipv4_addresses" {
|
||||
description = "Map of instance key to VM IPv4 address"
|
||||
value = {
|
||||
for k, m in module.vm : k => m.vm_ipv4_address
|
||||
}
|
||||
}
|
||||
|
||||
output "vm_tags" {
|
||||
value = {
|
||||
for k, m in module.vm : k => concat(
|
||||
try(local.instance_map[k].vm_tags, []),
|
||||
["terraform", "docker", local.instance_map[k].service_name, "ip-${replace(m.vm_ipv4_address, ".", "-")}"]
|
||||
)
|
||||
for k, m in module.vm-n8n : k => m.vm_ipv4_address
|
||||
}
|
||||
}
|
||||
|
||||
output "service_names" {
|
||||
description = "Map of instance key to service name"
|
||||
value = {
|
||||
for k, cfg in local.instance_map : k => cfg.service_name
|
||||
for k, v in local.instance_map : k => v.service_name
|
||||
}
|
||||
}
|
||||
|
||||
output "vm_names" {
|
||||
description = "Map of instance key to VM name"
|
||||
value = {
|
||||
for k, v in local.instance_map : k => v.vm_name
|
||||
}
|
||||
}
|
||||
|
||||
output "vm_tag_data" {
|
||||
description = "Map of instance key to node, VM name, and final tags"
|
||||
value = {
|
||||
for k, v in local.vm_created : k => {
|
||||
node_name = v.node_name
|
||||
vm_name = v.vm_name
|
||||
tags = v.vm_tags
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
output "inventory_file" {
|
||||
description = "Path to the generated Ansible inventory"
|
||||
value = module.inventory.filename
|
||||
}
|
||||
|
||||
output "vm_data_file" {
|
||||
description = "Path to the generated VM data file"
|
||||
value = module.vm_data.filename
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
instance_mode = "single"
|
||||
|
||||
instance = {
|
||||
service_name = "n8n"
|
||||
vm_name = "n8n-01"
|
||||
node_name = "pop"
|
||||
app_port = 5678
|
||||
app_image = "docker.n8n.io/n8nio/n8n"
|
||||
vm_tags = ["agentic"]
|
||||
}
|
||||
|
||||
@@ -1,40 +1,44 @@
|
||||
variable "instance_mode" {
|
||||
type = string
|
||||
default = "single"
|
||||
type = string
|
||||
description = "single or multiple"
|
||||
|
||||
validation {
|
||||
condition = contains(["single", "multiple"], var.instance_mode)
|
||||
error_message = "instance_mode must be either single or multiple."
|
||||
}
|
||||
}
|
||||
|
||||
variable "instance" {
|
||||
description = "Single instance definition"
|
||||
type = object({
|
||||
service_name = string
|
||||
vm_name = string
|
||||
node_name = string
|
||||
vm_cpu = optional(number)
|
||||
vm_ram = optional(number)
|
||||
vm_size = optional(string)
|
||||
app_port = number
|
||||
app_image = string
|
||||
vm_tags = optional(list(string))
|
||||
service_name = string
|
||||
vm_name = string
|
||||
node_name = string
|
||||
vm_cpu = optional(number)
|
||||
vm_ram = optional(number)
|
||||
vm_size = optional(string)
|
||||
vm_tags = optional(list(string))
|
||||
})
|
||||
default = null
|
||||
default = null
|
||||
nullable = true
|
||||
description = "Used only when instance_mode = single."
|
||||
}
|
||||
|
||||
variable "instances" {
|
||||
description = "Multiple instance definitions"
|
||||
type = map(object({
|
||||
service_name = string
|
||||
vm_name = string
|
||||
node_name = string
|
||||
vm_cpu = optional(number)
|
||||
vm_ram = optional(number)
|
||||
vm_size = optional(string)
|
||||
app_port = number
|
||||
app_image = string
|
||||
vm_tags = optional(list(string))
|
||||
service_name = string
|
||||
vm_name = string
|
||||
node_name = string
|
||||
vm_cpu = optional(number)
|
||||
vm_ram = optional(number)
|
||||
vm_size = optional(string)
|
||||
vm_tags = optional(list(string))
|
||||
}))
|
||||
default = {}
|
||||
default = {}
|
||||
nullable = false
|
||||
description = "Used only when instance_mode = multiple."
|
||||
}
|
||||
|
||||
|
||||
variable "vm_defaults" {
|
||||
type = object({
|
||||
node_datastore = string
|
||||
@@ -52,8 +56,8 @@ variable "vm_defaults" {
|
||||
vm_machine = "q35"
|
||||
vm_user_sshkey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRlWaLBt/qmWY01Cd6jN/YxLnlT+6lg+evEdN/dIajirdTj1rCbAdlG3WYvo+4BpN17HK3/eGQpGUMbgI/8MVd8YPODcD34gaNX0w2v66BwHx+S6BZUpz5T2IoQT0JtSv/TtFICoff5gXdNRpfd4eWsmTioEqLA6oToJLE4dn3jvAzFi9y7fyLqvuoQMmPidYYJjGT30eiULtXNspoEP+GmuWmVEu+znzMWaKDWKdOsii4Cv1aWCRKSDDRzDBrZI2mP+Vm4HDQBdgDYRw4ehumMDtfaSjyJCnrk691bIM+wxzICuIEecg5kq5HcUPvo2mFyWPAEXb5xlXnuopYEBd7 Generated By NeoServer"
|
||||
bridge = "vmbr0"
|
||||
vm_cpu = 1
|
||||
vm_ram = 2048
|
||||
vm_cpu = 2
|
||||
vm_ram = 4096
|
||||
vm_size = 20
|
||||
}
|
||||
}
|
||||
|
||||
10
terraform/vm_data.yml
Executable file
10
terraform/vm_data.yml
Executable file
@@ -0,0 +1,10 @@
|
||||
"vm_tag_data":
|
||||
"main":
|
||||
"node_name": "pop"
|
||||
"tags":
|
||||
- "agentic"
|
||||
- "terraform"
|
||||
- "docker"
|
||||
- "n8n"
|
||||
- "ip-192-168-10-107"
|
||||
"vm_name": "n8n-01"
|
||||
Reference in New Issue
Block a user