Project goal
Automate a monitoring server:
- Install Docker + Docker Compose
- Deploy Prometheus, Grafana, and Loki
- Open the UIs from your browser
Control Node → Ansible → Monitoring EC2 → Compose stack
├── Prometheus :9090
├── Grafana :3000
└── Loki :3100
Security group
| Port | Service |
|---|---|
| 22 | SSH |
| 3000 | Grafana |
| 9090 | Prometheus |
| 3100 | Loki (optional external) |
Folder structure
ansible-project/
├── playbooks/monitoring.yml
└── files/
├── prometheus.yml
└── docker-compose.yml
Prometheus config
# files/prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- localhost:9090Compose file
# files/docker-compose.yml
version: "3.8"
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
restart: unless-stopped
ports:
- "9090:9090"
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
- prometheus_data:/prometheus
networks:
- monitoring
grafana:
image: grafana/grafana:latest
container_name: grafana
restart: unless-stopped
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=admin123
volumes:
- grafana_data:/var/lib/grafana
depends_on:
- prometheus
- loki
networks:
- monitoring
loki:
image: grafana/loki:latest
container_name: loki
restart: unless-stopped
ports:
- "3100:3100"
command: -config.file=/etc/loki/local-config.yaml
networks:
- monitoring
volumes:
prometheus_data:
grafana_data:
networks:
monitoring:Playbook
---
- name: Monitoring stack deployment
hosts: all
become: true
tasks:
- name: Install Docker
apt:
name: docker.io
state: present
update_cache: true
- name: Start Docker
service:
name: docker
state: started
enabled: true
- name: Add ubuntu to docker group
user:
name: ubuntu
groups: docker
append: true
- name: Reset SSH connection
meta: reset_connection
- name: Install Docker Compose v1 binary
get_url:
url: "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-Linux-x86_64"
dest: /usr/local/bin/docker-compose
mode: "0755"
- name: Create monitoring directories
file:
path: /opt/monitoring/prometheus
state: directory
recurse: true
- name: Copy Prometheus config
copy:
src: files/prometheus.yml
dest: /opt/monitoring/prometheus/prometheus.yml
- name: Copy Docker Compose file
copy:
src: files/docker-compose.yml
dest: /opt/monitoring/docker-compose.yml
- name: Start monitoring stack
shell: docker-compose up -d
args:
chdir: /opt/monitoring
- name: Verify containers
shell: docker ps
register: docker_output
changed_when: false
- name: Show containers
debug:
var: docker_output.stdout_linesansible-playbook playbooks/monitoring.ymlAccess & Grafana setup
| URL | Default |
|---|---|
http://<ip>:9090 | Prometheus |
http://<ip>:3000 | Grafana (admin / admin123) |
In Grafana: add Prometheus datasource → URL http://prometheus:9090 (Docker network) or http://localhost:9090 if probing from the host.
Change the Grafana admin password immediately. Prefer Ansible Vault for credentials in real environments.