This guide provides a full Ansible playbook to deploy n8n with Docker Compose on Debian 10+, Ubuntu LTS, and RHEL 9+ compatible hosts.
- name: Deploy n8n
hosts: n8n
become: true
vars:
app_root: /opt/n8n
n8n_host: n8n.example.com
n8n_protocol: https
n8n_port: 5678
n8n_encryption_key: "{{ vault_n8n_encryption_key }}"
timezone: UTC
tasks:
- name: Install Docker on Debian/Ubuntu
apt:
name:
- docker.io
- docker-compose-plugin
state: present
update_cache: true
when: ansible_os_family == "Debian"
- name: Install Docker on RHEL family
dnf:
name:
- docker
- docker-compose-plugin
state: present
when: ansible_os_family == "RedHat"
- name: Enable and start Docker
service:
name: docker
state: started
enabled: true
- name: Create application directory
file:
path: "{{ app_root }}"
state: directory
mode: "0755"
- name: Create data directory
file:
path: "{{ app_root }}/data"
state: directory
mode: "0755"
- name: Write Docker Compose file
copy:
dest: "{{ app_root }}/docker-compose.yml"
mode: "0644"
content: |
services:
n8n:
image: docker.n8n.io/n8nio/n8n:latest
restart: unless-stopped
ports:
- "{{ n8n_port }}:5678"
environment:
- N8N_HOST={{ n8n_host }}
- N8N_PROTOCOL={{ n8n_protocol }}
- N8N_PORT={{ n8n_port }}
- N8N_ENCRYPTION_KEY={{ n8n_encryption_key }}
- TZ={{ timezone }}
- GENERIC_TIMEZONE={{ timezone }}
volumes:
- n8n_data:/home/node/.n8n
volumes:
n8n_data:
- name: Start n8n stack
command: docker compose up -d
args:
chdir: "{{ app_root }}"
vault_n8n_encryption_key in Ansible Vault for security.n8n_data volume preserves workflows, credentials, and encryption keys.docker.n8n.io/n8nio/n8n:latest from the official n8n registry.http://YOUR-SERVER:5678Beyond this playbook, we offer:
Contact our automation team: office@linux-server-admin.com