Automate Jenkins and Grafana Deployment with Ansible and Docker: A Step-by-Step Guide
Building a robust CI/CD pipeline and monitoring system is like constructing a well-oiled machine—it needs precision, efficiency, and reliability. This guide shows you how to use Ansible to automate the deployment of Jenkins and Grafana in Docker containers, complete with secure server configurations. Perfect for DevOps engineers and IT admins, this setup streamlines your workflow and boosts system visibility. Let’s dive into creating a secure, scalable environment for continuous integration and monitoring.
Why Use Ansible, Docker, Jenkins, and Grafana Together?
Jenkins powers your CI/CD pipelines, automating builds and deployments, while Grafana visualizes system metrics for real-time insights. Running them in Docker ensures lightweight, portable environments, and Ansible automates the entire setup, saving you from manual configuration headaches. Think of it as hiring a team of expert contractors who set up your infrastructure perfectly every time. This approach is ideal for teams looking to scale efficiently while maintaining security.
Prerequisites for a Seamless Setup
Before starting, ensure you have:
- Ansible 2.9+ installed on your control node.
- SSH access to your target servers (running Ubuntu, RHEL, or similar).
- Internet access on target servers for package downloads.
- A vault password file for secure playbook execution (optional but recommended).
Pro Tip: Test SSH connectivity to your servers before running the playbook to avoid delays.
Step-by-Step Guide to Deploy Jenkins and Grafana
Follow these steps to automate your server setup and deploy Jenkins and Grafana in Docker containers.
1. Clone the Ansible Repository
Start by grabbing the Ansible playbook from GitHub:
git clone https://github.com/Shipssv83/Ansible-Jenkins-Grafana-Docker-Install.git
cd Ansible-Jenkins-Grafana-Docker-Install
This repository contains all the playbooks and roles needed for a complete setup.
2. Install Required Ansible Galaxy Roles
Install the necessary roles to ensure your playbooks run smoothly:
ansible-galaxy install -r roles/requirements.yml
To update existing roles, use:
ansible-galaxy install -g -f -r roles/requirements.yml
These roles handle tasks like server configuration, Docker installation, and firewall setup.
3. Configure the Inventory File
Create an hosts file to specify your target servers:
vim hosts
Add your server details, for example:
[servers]
192.168.1.100 ansible_user=dev
Replace 192.168.1.100 with your server’s IP and dev with your SSH user.
4. Customize Playbook Variables
Edit variables to tailor the setup to your needs. Key global variables include:
- hosts: Target server group (e.g.,
servers). - timezone: Set to your region (e.g.,
Europe/Warsaw). - env: Environment name (e.g.,
prod).
For Chrony NTP configuration, adjust:
chrony_service_name: "chronyd"
chrony_ntp_servers: ["pool.ntp.org"]
chrony_config_file: "/etc/chrony.conf"
chrony_makestep_threshold: 5
chrony_makestep_limit: 3
These ensure accurate time synchronization, critical for logging and CI/CD processes.
5. Deploy Grafana in Docker
Run the playbook to set up Grafana:
ansible-playbook -i hosts --user dev --extra-vars "hosts=servers" --vault-password-file=password_file playbooks/grafana-docker-install.yml
What’s happening? This playbook:
- Installs Docker and Docker Compose.
- Deploys Grafana in a Docker container.
- Configures UFW firewall rules for secure access.
6. Deploy Jenkins in Docker
Run the playbook to set up Jenkins:
ansible-playbook -i hosts --user dev --extra-vars "hosts=servers" --vault-password-file=password_file playbooks/jenkins-docker-install.yml
What’s included? This playbook:
- Installs Docker and Docker Compose (if not already present).
- Deploys Jenkins in a Docker container.
- Opens necessary ports via UFW for secure access.
7. Secure Your Server
The playbooks also configure:
- SSH Daemon: Hardened settings for secure remote access.
- Chrony NTP: Synchronizes server time for accurate logs.
- UFW Firewall: Restricts access to only necessary ports.
This is like locking your doors and setting up a security system for your digital infrastructure.
Accessing Jenkins and Grafana
Once deployed:
- Jenkins: Access at
http://<server-ip>:8080and complete the initial setup wizard. - Grafana: Visit
http://<server-ip>:3000, log in (default: admin/admin), and configure data sources.
Quick Tip: Save your Jenkins unlock code (displayed in logs) and change Grafana’s default password immediately for security.
Benefits of This Approach
This setup delivers:
- Automation: Ansible eliminates manual configuration errors.
- Portability: Docker ensures consistent environments across servers.
- Security: UFW and SSH hardening protect your infrastructure.
- Scalability: Easily replicate the setup for additional servers.
It’s like building a modular factory—efficient, secure, and ready to scale.
Troubleshooting Tips
If issues arise:
- Check Playbook Logs: Review Ansible output for errors.
- Verify Docker: Use
docker psto confirm containers are running. - Test Firewall Rules: Ensure UFW allows ports 8080 (Jenkins) and 3000 (Grafana).
- Inspect SSH: Confirm your SSH user has the correct permissions.
Conclusion: Streamline Your DevOps Workflow
With Jenkins, Grafana, Docker, and Ansible, you’ve built a powerful, automated system for CI/CD and monitoring. Your pipelines are ready to roll, and your metrics are just a dashboard away. Want to optimize your DevOps setup further? Connect with our team at 1it.pro or share your success on 1it.pro to inspire the community!
🚀 Explore more guides on our blog 👉 blog.1it.pro
📧 Contact us: admin@1it.pro for expert IT guidance.
🌐 Explore more: Visit 1it.pro for top-tier IT solutions.