From 6069ab5ffaf49a45756c4c22189c30676901a535 Mon Sep 17 00:00:00 2001 From: TapTap Date: Fri, 13 Nov 2026 10:59:09 +0100 Subject: [PATCH] add ailab config --- .gitignore | 1 + ansible.cfg | 9 +++ ansible/playbook.yaml | 16 +++++ cloud-inits/qemu.yaml.tftpl | 17 +++++ main.tf | 130 ++++++++++++++++++++++++++++++++++++ provider.tf | 19 ++++++ variables.tf | 28 ++++++++ 7 files changed, 220 insertions(+) create mode 100644 ansible.cfg create mode 100644 ansible/playbook.yaml create mode 100644 cloud-inits/qemu.yaml.tftpl create mode 100644 main.tf create mode 100644 provider.tf create mode 100644 variables.tf diff --git a/.gitignore b/.gitignore index 6f5eaa0..80fcf6c 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ override.tf.json # Ignore transient lock info files created by terraform apply .terraform.tfstate.lock.info +.terraform.lock.hcl # Ignore CLI configuration files .terraformrc diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..dad3201 --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,9 @@ +[defaults] +inventory = inventory.ini +host_key_checking = False +interpreter_python = auto_silent +stdout_callback = default +result_format = yaml + +[ssh_connection] +pipelining = True diff --git a/ansible/playbook.yaml b/ansible/playbook.yaml new file mode 100644 index 0000000..bfc15ff --- /dev/null +++ b/ansible/playbook.yaml @@ -0,0 +1,16 @@ +- name: Ailab Sandbox + hosts: all + gather_facts: false + pre_tasks: + - name: Wait for VM to finish booting and accept SSH + ansible.builtin.wait_for_connection: + timeout: 180 + tasks: + - name: Install nix + ansible.builtin.shell: curl --proto '=https' --tlsv1.2 -L https://nixos.org/nix/install | sh -s -- --daemon + args: + creates: /nix/var/nix/profiles/default/bin/nix + - name: Install opencode + ansible.builtin.shell: curl -fsSL https://opencode.ai/v2/install | bash + args: + creates: /home/taptap/.opencode/bin/opencode diff --git a/cloud-inits/qemu.yaml.tftpl b/cloud-inits/qemu.yaml.tftpl new file mode 100644 index 0000000..1865f0a --- /dev/null +++ b/cloud-inits/qemu.yaml.tftpl @@ -0,0 +1,17 @@ +#cloud-config +ssh_pwauth: true +users: + - name: ${username} + groups: [sudo] + shell: /bin/bash + sudo: ALL=(ALL) NOPASSWD:ALL + lock_passwd: false + plain_text_passwd: ${jsonencode(password)} +package_update: true +packages: + - qemu-guest-agent +runcmd: + - systemctl enable --now qemu-guest-agent + - sudo add-apt-repository -y ppa:jdxcode/mise + - sudo apt update + - sudo apt install -y mise diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..8764bac --- /dev/null +++ b/main.tf @@ -0,0 +1,130 @@ +data "proxmox_virtual_environment_nodes" "nodes" {} + +# data "proxmox_datastores" "datastores" { +# node_name = data.proxmox_virtual_environment_nodes.nodes.names[0] +# } +# +# output "datastores" { +# value = data.proxmox_datastores.datastores +# } + +resource "proxmox_download_file" "ubuntu_cloud" { + content_type = "import" + datastore_id = var.datastore_id + node_name = var.node + url = "https://cloud-images.ubuntu.com/releases/resolute/release/ubuntu-26.04-server-cloudimg-amd64.img" + file_name = "ubuntu-26-04-server-cloudimg.qcow2" +} + +resource "proxmox_virtual_environment_file" "qemu-startup" { + content_type = "snippets" + datastore_id = var.datastore_id + node_name = var.node + source_raw { + data = templatefile("${path.module}/cloud-inits/qemu.yaml.tftpl", { password = var.vm_password + username = var.vm_username }) + file_name = "qemu-startup-config.yaml" + } +} + +resource "proxmox_virtual_environment_vm" "ailab" { + name = "ailab" + description = "sandboxed playground for ai agents" + tags = ["ai", "ubuntu"] + node_name = var.node + keyboard_layout = "de" + + bios = "ovmf" + machine = "q35" + scsi_hardware = "virtio-scsi-single" + + operating_system { + type = "l26" + } + + efi_disk { + datastore_id = var.datastore_id + type = "4m" + pre_enrolled_keys = false + } + + disk { + datastore_id = var.datastore_id + import_from = proxmox_download_file.ubuntu_cloud.id + interface = "scsi0" + size = "64" + discard = "on" + iothread = true + ssd = true + } + + initialization { + datastore_id = var.datastore_id + user_data_file_id = proxmox_virtual_environment_file.qemu-startup.id + + ip_config { + ipv4 { + address = "dhcp" + } + } + } + + agent { + enabled = true + } + + serial_device {} + + cpu { + cores = 2 + type = "host" + } + + memory { + dedicated = 8192 + floating = 8192 + } + + network_device { + bridge = "vmbr2" + firewall = true + } +} + +output "test" { + value = flatten([for ipv4 in proxmox_virtual_environment_vm.ailab.ipv4_addresses : ipv4 if !contains(ipv4, "127.0.0.1")])[0] +} + +data "ansible_inventory" "ailab" { + group { + name = "sanboxes" + host { + name = flatten([for ipv4 in proxmox_virtual_environment_vm.ailab.ipv4_addresses : ipv4 if !contains(ipv4, "127.0.0.1")])[0] + ansible_user = var.vm_username + ansible_password = var.vm_password + } + } +} + +action "ansible_playbook_run" "update_ailab" { + config { + playbooks = ["${path.module}/ansible/playbook.yaml"] + inventories = [data.ansible_inventory.ailab.json] + } +} + +resource "terraform_data" "playbook_trigger" { + input = { + vm_id = proxmox_virtual_environment_vm.ailab.id + playbook_hash = filemd5("${path.module}/ansible/playbook.yaml") + } + + lifecycle { + action_trigger { + events = [after_create, after_update] + actions = [action.ansible_playbook_run.update_ailab] + } + } + + depends_on = [proxmox_virtual_environment_vm.ailab] +} diff --git a/provider.tf b/provider.tf new file mode 100644 index 0000000..fa79ac0 --- /dev/null +++ b/provider.tf @@ -0,0 +1,19 @@ +terraform { + required_providers { + proxmox = { + source = "bpg/proxmox" + version = "~> 0.122.0" + } + ansible = { + version = "~> 1.4.0" + source = "ansible/ansible" + } + } +} + +provider "proxmox" { + endpoint = var.proxmox_url + username = var.proxmox_username + password = var.proxmox_password + insecure = true +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..8e6b5eb --- /dev/null +++ b/variables.tf @@ -0,0 +1,28 @@ +variable "proxmox_url" { + type = string +} + +variable "proxmox_username" { + type = string +} + +variable "proxmox_password" { + type = string + sensitive = true +} +variable "vm_username" { + type = string +} +variable "vm_password" { + type = string + sensitive = true +} + +variable "node" { + type = string +} + +variable "datastore_id" { + type = string +} +