12 changed files with 186 additions and 21 deletions
+1
View File
@@ -6,6 +6,7 @@
# Secrets # Secrets
.env .env
.envrc
# Ansible # Ansible
ansible/inventory.ini ansible/inventory.ini
+76
View File
@@ -0,0 +1,76 @@
# dokploy
Terraform + Ansible setup for a self-hosted [Dokploy](https://dokploy.com/) instance.
## Setup
1. Create a Hetzner Cloud API key
2. Create a Cloudflare DNS key with `Zone.DNS` edit permission
### Local setup
Secrets live in `terraform/.env` and are injected as `TF_VAR_*` environment
variables into the local shell session:
```env
TF_VAR_hcloud_token=...
TF_VAR_cloudflare_api_token=...
TF_VAR_domain_name=...
```
#### direnv
Setup direnv on the development machine and add `terraform/.envrc`:
```env
dotenv_if_exists .env
```
Then load the variables into the shell:
```sh
cd terraform
direnv allow
```
Run Terraform / Tofu manually:
```sh
tofu init
tofu plan
tofu apply
```
#### Without direnv
Load the file manually, scoped to a single command:
```sh
cd terraform
set -a; . ./.env; set +a
tofu plan
```
Or use a wrapper that keeps the variables contained to the `tofu` subprocess:
```sh
#!/bin/sh
set -a
. ./.env
set +a
exec tofu "$@"
```
## CI/CD
CI reads the same variables from the git forge's secret store — no `.env` file is needed.
Add the necessary variables
GitHub Actions example:
```yaml
env:
TF_VAR_hcloud_token: ${{ secrets.TF_VAR_HCLOUD_TOKEN }}
TF_VAR_cloudflare_api_token: ${{ secrets.TF_VAR_CLOUDFLARE_API_TOKEN }}
TF_VAR_domain_name: ${{ secrets.TF_VAR_DOMAIN_NAME }}
```
+1 -1
View File
@@ -1,5 +1,5 @@
--- ---
ansible_user: "ansible" ansible_user: "root"
sysadmin_user: "alessandrovitali" sysadmin_user: "alessandrovitali"
sysadmin_public_ssh_key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAm/J+9YG+odym9In9C4iLcrfXlrlPK2TygtI7lBNNpl" sysadmin_public_ssh_key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAm/J+9YG+odym9In9C4iLcrfXlrlPK2TygtI7lBNNpl"
-2
View File
@@ -1,2 +0,0 @@
[dokploy]
dokploy ansible_host=${public_ip} ansible_user=debian
+1
View File
@@ -6,3 +6,4 @@
- role: studio.ansible.base - role: studio.ansible.base
- role: studio.ansible.users - role: studio.ansible.users
- role: studio.ansible.security - role: studio.ansible.security
- role: dokploy
+14
View File
@@ -0,0 +1,14 @@
---
# Official Dokploy installer
dokploy_installer_url: "https://dokploy.com/install.sh"
dokploy_installer_path: "/usr/local/bin/dokploy-install.sh"
# Pin to a specific release tag for reproducible installs
dokploy_version: "latest"
# Packages the installer expects on a minimal Debian image
# dokploy_prerequisites:
# - ca-certificates
# - curl
# - openssl
# - iproute2
+33
View File
@@ -0,0 +1,33 @@
---
# - name: Install Dokploy prerequisites
# ansible.builtin.apt:
# name: "{{ dokploy_prerequisites }}"
# state: present
# update_cache: true
- name: Download Dokploy installer
ansible.builtin.get_url:
url: "{{ dokploy_installer_url }}"
dest: "{{ dokploy_installer_path }}"
mode: "0755"
# The upstream installer is not idempotent & exits non-zero if the swarm is already initialized. Guard on the presence of the dokploy swarm service
- name: Check if Dokploy is already installed
ansible.builtin.command: docker service inspect dokploy
register: dokploy_installed
changed_when: false
failed_when: false
- name: Install Dokploy
ansible.builtin.command: "{{ dokploy_installer_path }}"
environment:
DOKPLOY_VERSION: "{{ dokploy_version }}"
changed_when: true
when: dokploy_installed.rc != 0
# - name: Update Dokploy (opt-in)
# ansible.builtin.command: "{{ dokploy_installer_path }} update"
# environment:
# DOKPLOY_VERSION: "{{ dokploy_version }}"
# changed_when: true
# when: dokploy_installed.rc == 0
+45 -7
View File
@@ -1,22 +1,51 @@
# SSH key shared with the VPS # Setup the SSH key in the Hetzner Cloud Console
resource "hcloud_ssh_key" "ansible" { resource "hcloud_ssh_key" "ansible" {
name = var.project_name name = var.project_name
public_key = var.ssh_public_key public_key = var.ssh_public_key
} }
# Provision a small Hetzner Cloud VPS # Provision the Hetzner Cloud VPS
resource "hcloud_server" "dokploy" { resource "hcloud_server" "dokploy" {
name = var.project_name name = var.project_name
image = var.hcloud_image image = var.hcloud_image
server_type = var.hcloud_server_type server_type = var.hcloud_server_type
location = var.hcloud_location location = var.hcloud_location
ssh_keys = [hcloud_ssh_key.ansible.id] user_data = templatefile("${path.module}/templates/user_data.tpl", {
ssh_public_key = var.ssh_public_key
})
firewall_ids = [hcloud_firewall.host.id]
public_net { public_net {
ipv4_enabled = true ipv4_enabled = true
} }
} }
# Configure Hetzner Cloud Firewall
resource "hcloud_firewall" "host" {
name = var.project_name
rule {
direction = "in"
protocol = "tcp"
port = "80"
source_ips = ["0.0.0.0/0", "::/0"]
}
rule {
direction = "in"
protocol = "tcp"
port = "443"
source_ips = ["0.0.0.0/0", "::/0"]
}
rule {
direction = "in"
protocol = "tcp"
port = "22"
source_ips = ["0.0.0.0/0", "::/0"]
}
}
# Resolve the Cloudflare zone from its apex domain name # Resolve the Cloudflare zone from its apex domain name
data "cloudflare_zone" "zone" { data "cloudflare_zone" "zone" {
filter = { filter = {
@@ -28,10 +57,19 @@ locals {
public_ip = hcloud_server.dokploy.ipv4_address public_ip = hcloud_server.dokploy.ipv4_address
} }
# Automatically publish the VPS IP as a Cloudflare A record # Publish the VPS IP as a Cloudflare A record
resource "cloudflare_dns_record" "a" { resource "cloudflare_dns_record" "domain" {
zone_id = data.cloudflare_zone.zone.id zone_id = data.cloudflare_zone.zone.id
name = var.record_name name = "@"
type = "A"
content = local.public_ip
proxied = var.cloudflare_proxied
ttl = var.cloudflare_proxied ? 1 : 3600
}
resource "cloudflare_dns_record" "subdomain" {
zone_id = data.cloudflare_zone.zone.id
name = "*"
type = "A" type = "A"
content = local.public_ip content = local.public_ip
proxied = var.cloudflare_proxied proxied = var.cloudflare_proxied
@@ -40,7 +78,7 @@ resource "cloudflare_dns_record" "a" {
# Write IP to Ansible inventory # Write IP to Ansible inventory
resource "local_file" "ansible_inventory" { resource "local_file" "ansible_inventory" {
content = templatefile("${path.module}/../ansible/inventory.tpl", { content = templatefile("${path.module}/templates/ansible_inventory.tpl", {
public_ip = local.public_ip public_ip = local.public_ip
}) })
filename = "${path.module}/../ansible/inventory.ini" filename = "${path.module}/../ansible/inventory.ini"
@@ -0,0 +1,2 @@
[dokploy]
dokploy ansible_host=${public_ip}
+9
View File
@@ -0,0 +1,9 @@
# cloud-config
users:
- name: ansible
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
ssh_authorized_keys:
- ${ssh_public_key}
ssh_pwauth: false
disable_root: true
-1
View File
@@ -4,7 +4,6 @@ hcloud_server_type = "cx23"
hcloud_image = "debian-13" hcloud_image = "debian-13"
hcloud_location = "nbg1" hcloud_location = "nbg1"
record_name = "dokploy"
cloudflare_proxied = true cloudflare_proxied = true
# Note: Use file() to easily read your local SSH public key # Note: Use file() to easily read your local SSH public key
-6
View File
@@ -45,12 +45,6 @@ variable "domain_name" {
description = "Cloudflare zone (apex domain) in which to create the A record" description = "Cloudflare zone (apex domain) in which to create the A record"
} }
variable "record_name" {
type = string
description = "DNS record name relative to the zone; use \"@\" for the apex"
default = "dokploy"
}
variable "cloudflare_proxied" { variable "cloudflare_proxied" {
type = bool type = bool
description = "Whether Cloudflare should proxy (orange-cloud) the A record" description = "Whether Cloudflare should proxy (orange-cloud) the A record"