From 089b099f7f13c13f97dd53bbecd1c717bfb1ea01 Mon Sep 17 00:00:00 2001 From: CC Date: Thu, 21 May 2026 13:12:12 +0100 Subject: [PATCH] n8n ansible base --- .gitea/workflows/deploy.yml | 26 ++++++++++++++----- ansible/playbooks/tags.yml | 22 ++++++++++++++++ terraform/main.tf | 26 +++++++++++++++++++ .../modules/proxmox_ansible_inventory/main.tf | 26 +++++++++++++++++++ .../proxmox_ansible_inventory/outputs.tf | 7 +++++ .../proxmox_ansible_inventory/variables.tf | 13 ++++++++++ terraform/modules/proxmox_vm_data/main.tf | 17 ++++++++++++ terraform/modules/proxmox_vm_data/outputs.tf | 7 +++++ .../modules/proxmox_vm_data/variables.tf | 15 +++++++++++ terraform/terraform.tfvars | 11 -------- 10 files changed, 152 insertions(+), 18 deletions(-) create mode 100644 ansible/playbooks/tags.yml create mode 100644 terraform/modules/proxmox_ansible_inventory/main.tf create mode 100644 terraform/modules/proxmox_ansible_inventory/outputs.tf create mode 100644 terraform/modules/proxmox_ansible_inventory/variables.tf create mode 100644 terraform/modules/proxmox_vm_data/main.tf create mode 100644 terraform/modules/proxmox_vm_data/outputs.tf create mode 100644 terraform/modules/proxmox_vm_data/variables.tf delete mode 100644 terraform/terraform.tfvars diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 74c32db..e2dcb63 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -13,7 +13,7 @@ on: - multi.tfvars.example jobs: - terraform: + terraform-deploy: runs-on: ubuntu-latest defaults: run: @@ -21,7 +21,7 @@ jobs: steps: - uses: actions/checkout@v4 - - uses: hashicorp/setup-terraform@v3 + - uses: hashicorp/setup-terraform@v4 - name: Select tfvars run: cp "${{ inputs.tfvars_file }}" terraform.tfvars @@ -42,16 +42,28 @@ jobs: - name: Write tags run: terraform output -json vm_tags > ../ansible/vm_tags.json - deploy: + ansible-configure: needs: terraform runs-on: ubuntu-latest + defaults: + run: + working-directory: ansible steps: - uses: actions/checkout@v4 - 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 - - 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/id_rsa + chmod 600 ~/.ssh/id_rsa + echo -e "Host *\n\tStrictHostKeyChecking no\n\tUserKnownHostsFile=/dev/null\n" > ~/.ssh/config + + - name: Run playbook + run: | + ansible-playbook playbooks/docker_copy.yml -i inventory/inventory.yml -u cloud --private-key ~/.ssh/id_rsa diff --git a/ansible/playbooks/tags.yml b/ansible/playbooks/tags.yml new file mode 100644 index 0000000..9d3f8e7 --- /dev/null +++ b/ansible/playbooks/tags.yml @@ -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 }}" diff --git a/terraform/main.tf b/terraform/main.tf index ba5123f..2c3a6b2 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -2,6 +2,20 @@ 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[k].vm_ipv4_address + vm_tags = concat( + try(v.vm_tags, []), + ["terraform", "docker", v.service_name, "ip-${replace(module.vm[k].vm_ipv4_address, ".", "-")}"] + ) + } + } } module "vm-n8n" { @@ -23,3 +37,15 @@ module "vm-n8n" { ) vm_user_sshkey = var.vm_defaults.vm_user_sshkey } + +module "inventory" { + source = "./modules/proxmox_ansible_inventory" + filename = "${path.module}/ansible/inventory/inventory.yml" + instances = local.vm_created +} + +module "vm_data" { + source = "./modules/proxmox_vm_data" + filename = "${path.module}/terraform/vm_data.yml" + instances = local.vm_created +} diff --git a/terraform/modules/proxmox_ansible_inventory/main.tf b/terraform/modules/proxmox_ansible_inventory/main.tf new file mode 100644 index 0000000..6c262d8 --- /dev/null +++ b/terraform/modules/proxmox_ansible_inventory/main.tf @@ -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) +} diff --git a/terraform/modules/proxmox_ansible_inventory/outputs.tf b/terraform/modules/proxmox_ansible_inventory/outputs.tf new file mode 100644 index 0000000..0ab7d6c --- /dev/null +++ b/terraform/modules/proxmox_ansible_inventory/outputs.tf @@ -0,0 +1,7 @@ +output "filename" { + value = local_file.inventory.filename +} + +output "content" { + value = local_file.inventory.content +} diff --git a/terraform/modules/proxmox_ansible_inventory/variables.tf b/terraform/modules/proxmox_ansible_inventory/variables.tf new file mode 100644 index 0000000..ea3837f --- /dev/null +++ b/terraform/modules/proxmox_ansible_inventory/variables.tf @@ -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 + })) +} diff --git a/terraform/modules/proxmox_vm_data/main.tf b/terraform/modules/proxmox_vm_data/main.tf new file mode 100644 index 0000000..b1a0368 --- /dev/null +++ b/terraform/modules/proxmox_vm_data/main.tf @@ -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) +} diff --git a/terraform/modules/proxmox_vm_data/outputs.tf b/terraform/modules/proxmox_vm_data/outputs.tf new file mode 100644 index 0000000..c6c849f --- /dev/null +++ b/terraform/modules/proxmox_vm_data/outputs.tf @@ -0,0 +1,7 @@ +output "filename" { + value = local_file.vm_data.filename +} + +output "content" { + value = local_file.vm_data.content +} diff --git a/terraform/modules/proxmox_vm_data/variables.tf b/terraform/modules/proxmox_vm_data/variables.tf new file mode 100644 index 0000000..a523f07 --- /dev/null +++ b/terraform/modules/proxmox_vm_data/variables.tf @@ -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) + })) +} diff --git a/terraform/terraform.tfvars b/terraform/terraform.tfvars deleted file mode 100644 index 7705723..0000000 --- a/terraform/terraform.tfvars +++ /dev/null @@ -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"] -} -