Deploying to EC2 Servers Using Jenkins and Ansible Locally
In this blog, we'll walk through deploying code to EC2 servers using Jenkins and Ansible running locally. This is a practical approach that keeps your

software dev who actually enjoys debugging (weird, i know) i spend most of my time fixing broken code , Building new things and honestly? i love it. there's something about hunting down bugs that just hits different. when i'm not staring at terminal logs, i'm either at the gym or checking out new places. currently exploring different areas in tech and building stuff in public because why not share the messy journey. india ๐ฎ๐ณ
Prerequisites
Before we dive in, make sure you have:
Docker installed on your local machine
An AWS account with at least one EC2 instance running (Ubuntu/Debian-based recommended)
Your EC2
.pemkey file downloaded and stored safelyBasic understanding of SSH and command-line operations
Important: Go ahead and launch your EC2 instance now if you haven't already. You'll need it ready before we get to the deployment stage. Make sure to note down the public IP address and keep your .pem file handy.
Setting Up Jenkins with Docker
Instead of installing Jenkins directly and dealing with Java dependencies, we'll use Docker. This keeps everything isolated and makes cleanup easier.
Run this command to start Jenkins:
bash
docker run -d --name jenkins -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts
This command does a few things:
-druns the container in detached mode (in the background)--name jenkinsgives our container a friendly name-p 8080:8080exposes Jenkins web interface on port 8080-p 50000:50000exposes the port Jenkins uses for build agentsjenkins/jenkins:ltsuses the official long-term support Jenkins image
You can attach a volume if you want your Jenkins data to persist, but for this tutorial, we'll keep it simple.
Initial Jenkins Setup
Open your browser and go to http://localhost:8080. Jenkins will ask for an initial admin password. To get it, run:
bash
docker exec jenkins cat /var/lib/jenkins/secrets/initialPassword
Copy that password, paste it into Jenkins, and follow the setup wizard. Choose "Install suggested plugins" when prompted โ this gives you a solid foundation to work with.
Create your first admin user when asked, and you're good to go.
Installing Ansible Inside the Jenkins Container
Here's where it gets interesting. We're going to install Ansible inside the same container as Jenkins. This way, Jenkins can directly use Ansible without any network complications.
First, get into your Jenkins container as root
docker exec -it --user root jenkins /bin/bash
Now you're inside the container. Run these commands to install Python and Ansible
apt update
apt install -y python3 python3-pip python3-venv ansible
you will get some output like this.

Ansible needs Python to run its playbooks, so we're installing both together. This might take a minute or two.
Once it's done, verify everything installed correctly
ansible --version
python3 --version
You should see version numbers for both. Type exit to leave the container shell.
Configuring Ansible in Jenkins
Back in the Jenkins web interface, we need to install the Ansible plugin and configure it.
Go to Dashboard โ Manage Jenkins โ Plugins
Click on Available plugins
Search for "Ansible" and install the Ansible plugin
Restart Jenkins when prompted (or just check "Restart Jenkins when installation is complete")
After Jenkins restarts, configure the Ansible installation:
Go to Dashboard โ Manage Jenkins โ Tools
Scroll down to the Ansible section
Click Add Ansible
Give it a name like "Ansible" (you'll reference this name later)
For the path, enter
/usr/bin(this is where Ansible was installed inside the container)Save your changes

Adding Your EC2 SSH Credentials
Jenkins needs your .pem key to SSH into your EC2 instance. Let's add it securely.
Go to Dashboard โ Manage Jenkins โ Credentials
Click on (global) domain
Click Add Credentials
Choose SSH Username with private key as the kind
Set ID to something memorable like
private-keySet Username to
ubuntu(orec2-userif you're using Amazon Linux)Under Private Key, select Enter directly
Click Add and paste your entire
.pemfile contentClick Create

Creating Your Deployment Repository
I've created a demo repository you can use as a reference:
https://github.com/ashok-2003/devops-0-100/tree/master/jenkin-ansible-demo
Your repository should have at minimum:
dev.ini- Your Ansible inventory file (lists your EC2 servers)apache.yml- Your Ansible playbook (defines what to deploy)Your application files
The dev.ini file should look something like this
[webservers]
your-ec2-public-ip ansible_user=ubuntu ansible_ssh_private_key_file=/path/to/key
Replace your-ec2-public-ip with your actual EC2 instance's public IP address.
Creating the Jenkins Pipeline
Now for the fun part โ creating a pipeline that ties everything together.
From the Jenkins dashboard, click New Item
Enter a name like "Deploy to EC2"
Choose Pipeline and click OK
Scroll down to the Pipeline section
Paste this script
pipeline {
agent any
stages {
stage('SCM Checkout') {
steps {
git url: 'https://github.com/ashok-2003/devops-0-100.git', branch: 'master'
}
}
stage('Run Ansible Playbook') {
steps {
ansiblePlaybook(
credentialsId: 'private-key',
disableHostKeyChecking: true,
installation: 'Ansible',
inventory: 'jenkin-ansible-demo/dev.ini',
playbook: 'jenkin-ansible-demo/apache.yml'
)
}
}
}
}
This pipeline does two things:
SCM Checkout: Grabs your code from GitHub
Run Ansible Playbook: Executes your Ansible playbook against your EC2 instance
Make sure to update the Git URL to point to your own repository if you're not using the demo one.
- Click Save and you will see Some output like this.

Running Your First Deployment
Click Build Now from your pipeline's page. Jenkins will start executing your pipeline. You can click on the build number (like #1) and then Console Output to watch it happen in real-time.
If everything is configured correctly, you'll see:
Jenkins pulling your code from Git
Ansible connecting to your EC2 instance
The playbook executing each task
A success message at the end
Verifying Your Deployment
Open your browser and navigate to your EC2 instance's public IP address. If you deployed a web application (like the Apache server in the demo), you should see it running.
For the demo repository, you'll see a simple webpage confirming that everything deployed successfully.

Troubleshooting Common Issues
"Host key verification failed": The disableHostKeyChecking: true setting should handle this, but if you still see it, make sure your credentials are correctly configured.
"Permission denied (publickey)": Double-check that you pasted your entire .pem file content into Jenkins credentials, including the -----BEGIN RSA PRIVATE KEY----- and -----END RSA PRIVATE KEY----- lines.
Ansible command not found: Make sure you installed Ansible in the Jenkins container and configured the Ansible installation path correctly in Jenkins tools.
Can't connect to EC2: Verify your EC2 security group allows inbound SSH (port 22) from your IP address.


