This guide provides a complete Ansible playbook to install Alerta using Docker with PostgreSQL for alert management, correlation, and notification routing.
Current Alerta version: 8.7.0 (Docker image)
Create a file named alerta.yml:
---
- name: Install and Configure Alerta
hosts: alerta
become: true
vars:
alerta_version: "8.7.0"
alerta_port: 8080
alerta_secret_key: "CHANGE-ME-TO-A-RANDOM-SECRET-KEY"
alerta_admin_users:
- name: admin
email: admin@example.com
password: "AdminP@ss123"
postgres_version: "15"
postgres_password: "postgres_secure_password"
alerta_data_dir: "/opt/alerta/data"
alerta_config_dir: "/opt/alerta/config"
tasks:
- name: Create Alerta directories
file:
path: "{{ item }}"
state: directory
owner: root
group: root
mode: '0755'
loop:
- "{{ alerta_data_dir }}"
- "{{ alerta_config_dir }}"
- name: Create Docker Compose file
copy:
dest: "{{ alerta_data_dir }}/docker-compose.yml"
owner: root
group: root
mode: '0644'
content: |
version: '3.8'
services:
alerta:
image: alerta/alerta:{{ alerta_version }}
container_name: alerta
restart: unless-stopped
ports:
- "{{ alerta_port }}:8080"
depends_on:
- postgres
environment:
- DATABASE_URL=postgresql://alerta:{{ postgres_password }}@postgres:5432/alerta
- SECRET_KEY={{ alerta_secret_key }}
- ADMIN_USERS={{ alerta_admin_users | map(attribute='email') | join(',') }}
- ADMIN_KEY={{ alerta_secret_key }}
- CORS_ORIGINS=http://localhost:{{ alerta_port }},http://alerta-web:3000
- PLUGINS=reject,blackout,normalise,enhance
networks:
- alerta_net
postgres:
image: postgres:{{ postgres_version }}
container_name: alerta-postgres
restart: unless-stopped
environment:
- POSTGRES_DB=alerta
- POSTGRES_USER=alerta
- POSTGRES_PASSWORD={{ postgres_password }}
volumes:
- {{ alerta_data_dir }}/postgres:/var/lib/postgresql/data
networks:
- alerta_net
networks:
alerta_net:
driver: bridge
- name: Start Alerta containers
community.docker.docker_compose:
project_src: "{{ alerta_data_dir }}"
state: present
build: false
pull: true
- name: Wait for Alerta to start
wait_for:
port: "{{ alerta_port }}"
delay: 10
timeout: 120
- name: Configure firewall (UFW)
ufw:
rule: allow
port: "{{ alerta_port }}"
proto: tcp
comment: "Alerta web interface"
when: ansible_os_family == "Debian"
failed_when: false
- name: Configure firewall (firewalld)
firewalld:
port: "{{ alerta_port }}/tcp"
permanent: true
immediate: true
state: enabled
when: ansible_os_family == "RedHat"
failed_when: false
- name: Verify Alerta is running
uri:
url: "http://localhost:{{ alerta_port }}/api/management/healthcheck"
method: GET
status_code: 200
register: health_check
retries: 5
delay: 5
until: health_check.status == 200
- name: Display Alerta status
debug:
msg: |
Alerta {{ alerta_version }} installed successfully!
Web Interface: http://{{ ansible_default_ipv4.address | default(ansible_host) }}:{{ alerta_port }}
API Key: {{ alerta_secret_key }}
Admin Email: {{ alerta_admin_users[0].email }}
IMPORTANT: Change default credentials after first login!
Data directory: {{ alerta_data_dir }}
---
alerta:
hosts:
alerta-server:
ansible_host: 192.168.1.116
ansible_user: ansible
ansible_become: true
# Test connectivity
ansible all -i inventory.yml -m ping
# Run the Alerta playbook
ansible-playbook -i inventory.yml alerta.yml
# Run with custom secret key
ansible-playbook -i inventory.yml alerta.yml \
-e "alerta_secret_key=$(openssl rand -hex 32)"
# Check container status
ssh alerta-server "docker ps | grep alerta"
# View container logs
ssh alerta-server "docker logs alerta"
# Test API health
curl http://alerta-server:8080/api/management/healthcheck
# Test API info
curl http://alerta-server:8080/api/management/config
# Access web UI
# http://alerta-server:8080
- name: Configure Alerta routing and plugins
hosts: alerta
become: true
vars:
alerta_config_dir: "/opt/alerta/config"
tasks:
- name: Create Alerta configuration
copy:
dest: "{{ alerta_config_dir }}/alertad.conf"
owner: root
group: root
mode: '0644'
content: |
# Alerta Configuration
# Database
DATABASE_URL = 'postgresql://alerta:postgres_secure_password@postgres:5432/alerta'
# Security
SECRET_KEY = 'CHANGE-ME-TO-A-RANDOM-SECRET-KEY'
CORS_ORIGINS = ['http://localhost:8080']
# Plugins
PLUGINS = ['reject', 'blackout', 'normalise', 'enhance', 'heartbeat', 'opsgenie']
# Alert settings
DEFAULT_SEVERITY = 'indeterminate'
DEFAULT_TIMEOUT = 86400
# Email notifications
SMTP_HOST = 'smtp.example.com'
SMTP_PORT = 587
SMTP_USERNAME = 'alerta@example.com'
SMTP_PASSWORD = 'smtp_password'
MAIL_FROM = 'alerta@example.com'
EMAIL_VERIFICATION = False
# Webhook integrations
WEBHOOK_URLS = [
'https://hooks.slack.com/services/XXX/YYY/ZZZ'
]
- name: Restart Alerta
community.docker.docker_compose:
project_src: "{{ alerta_data_dir }}"
state: present
restarted: true
- name: Configure Alerta Slack integration
hosts: alerta
become: true
vars:
alerta_config_dir: "/opt/alerta/config"
slack_webhook_url: "https://hooks.slack.com/services/XXX/YYY/ZZZ"
slack_channel: "#alerts"
tasks:
- name: Update Alerta config for Slack
lineinfile:
path: "{{ alerta_config_dir }}/alertad.conf"
regexp: "^WEBHOOK_URLS"
line: "WEBHOOK_URLS = ['{{ slack_webhook_url }}']"
- name: Add Slack plugin
lineinfile:
path: "{{ alerta_config_dir }}/alertad.conf"
regexp: "^PLUGINS"
line: "PLUGINS = ['reject', 'blackout', 'normalise', 'enhance', 'heartbeat', 'slack']"
- name: Add Slack configuration
blockinfile:
path: "{{ alerta_config_dir }}/alertad.conf"
marker: "# {mark} SLACK CONFIG"
block: |
# Slack Integration
SLACK_WEBHOOK_URL = '{{ slack_webhook_url }}'
SLACK_CHANNEL = '{{ slack_channel }}'
SLACK_ICON_EMOJI = ':rotating_light:'
SLACK_ATTACHMENTS = True
- name: Restart Alerta
community.docker.docker_compose:
project_src: "{{ alerta_data_dir }}"
state: present
restarted: true
- name: Send test alert to Alerta
hosts: alerta
become: false
vars:
alerta_port: 8080
alerta_api_key: "CHANGE-ME-TO-A-RANDOM-SECRET-KEY"
tasks:
- name: Send test alert
uri:
url: "http://localhost:{{ alerta_port }}/api/alert"
method: POST
headers:
Content-Type: application/json
Authorization: "Key {{ alerta_api_key }}"
body_format: json
body:
resource: "Test Server"
event: "TestEvent"
environment: "Production"
severity: "warning"
service:
- "test-service"
group: "Network"
value: "Test alert value"
text: "This is a test alert from Ansible"
tags:
- "test"
- "ansible"
attributes:
test: "true"
register: alert_result
- name: Display alert result
debug:
var: alert_result.json
- name: Backup Alerta data
hosts: alerta
become: true
vars:
alerta_data_dir: "/opt/alerta/data"
backup_dir: "/backup/alerta"
tasks:
- name: Create backup directory
file:
path: "{{ backup_dir }}"
state: directory
mode: '0755'
- name: Backup PostgreSQL database
community.docker.docker_container_exec:
container: alerta-postgres
command: pg_dump -U alerta alerta
register: pg_dump
changed_when: false
- name: Save database dump
copy:
content: "{{ pg_dump.stdout }}"
dest: "{{ backup_dir }}/alerta-db-{{ ansible_date_time.date }}.sql"
- name: Backup configuration
archive:
path: "{{ alerta_config_dir }}"
dest: "{{ backup_dir }}/config-backup-{{ ansible_date_time.date }}.tar.gz"
- name: Clean up old backups (keep 7 days)
find:
paths: "{{ backup_dir }}"
patterns: "*.sql,*.tar.gz"
age: 7d
register: old_backups
- name: Remove old backups
file:
path: "{{ item.path }}"
state: absent
loop: "{{ old_backups.files }}"
# Check container logs
docker logs alerta
# Check PostgreSQL logs
docker logs alerta-postgres
# Check container status
docker ps -a | grep alerta
# Test database connection
docker exec alerta-postgres psql -U alerta -d alerta -c "\dt"
# Check DATABASE_URL
docker exec alerta env | grep DATABASE
# Test API directly
curl -X GET http://localhost:8080/api/management/healthcheck
# Check API logs
docker logs alerta 2>&1 | grep -i error
# Verify API key
curl -H "Authorization: Key YOUR_API_KEY" http://localhost:8080/api/userinfo
# Check alert endpoint
curl -X POST http://localhost:8080/api/alert \
-H "Content-Type: application/json" \
-H "Authorization: Key YOUR_API_KEY" \
-d '{"resource":"test","event":"test","environment":"test"}'
# Check plugins
docker exec alerta alertad plugins
openssl rand -hex 32Beyond this playbook, we offer:
Contact our automation team: office@linux-server-admin.com