How to Set Up a Jenkins Host on a Remote Server for Ansible Job Execution
Muhammad Sarim
Team Lead & Senior Full Stack Developer | Expert in Node.js, NestJS, Go, Django | Frontend | React, Next.js | Redis, Kafka | Cloud Architect: Jenkins, Azure, AWS, GCP
Integrating Jenkins with Ansible is essential for automating infrastructure management and deployment in modern DevOps workflows. However, if Jenkins is not properly set up to connect with the remote server, it will be unable to run Ansible playbooks and YAML files. This article outlines the necessity of this integration and provides a step-by-step guide to successfully configuring Jenkins to execute Ansible jobs on a remote server.
Why is This Setup Necessary?
Without configuring Jenkins to communicate with the remote server:
By setting up a Jenkins host on a remote server, we ensure seamless automation, allowing Jenkins to trigger Ansible jobs and execute tasks efficiently.
Step-by-Step Guide to Configuring Jenkins for Ansible Execution on a Remote Server
Step 1: Install Jenkins on the Remote Server
If Jenkins is not already installed, you can set it up using the following steps:
sudo apt update && sudo apt install openjdk-11-jdk -y
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt update
sudo apt install jenkins -y
Start and enable Jenkins:
sudo systemctl start jenkins
sudo systemctl enable jenkins
Step 2: Create a Jenkins User on the Remote Server
To allow Jenkins to execute Ansible jobs on a remote server, create a dedicated Jenkins user:
sudo useradd -m -s /bin/bash jenkins
sudo passwd jenkins
Give Jenkins sudo privileges:
echo 'jenkins ALL=(ALL) NOPASSWD: ALL' | sudo tee /etc/sudoers.d/jenkins
Step 3: Enable SSH Access for Jenkins
On the remote server, switch to the Jenkins user:
sudo su - jenkins
Generate an SSH key:
领英推荐
ssh-keygen -t rsa -b 4096
Copy the public key to the remote server where Ansible will execute:
ssh-copy-id jenkins@<REMOTE_SERVER_IP>
Step 4: Install Ansible on the Remote Server
If Ansible is not already installed on the remote server, install it:
sudo apt update && sudo apt install ansible -y
Verify the installation:
ansible --version
Step 5: Configure Jenkins to Run Ansible Jobs
Go to Jenkins Dashboard → Manage Jenkins → Manage Plugins.
Search for "Ansible Plugin" and install it.
Restart Jenkins.
2. Configure Jenkins to Use Ansible:
Go to Manage Jenkins → Global Tool Configuration.
Under Ansible installations, provide the Ansible installation path (e.g., /usr/bin/ansible).
3. Create a Jenkins Job for Running Ansible Playbooks:
Create a New Item in Jenkins → Choose Freestyle Project.
In the Build Environment, select Use SSH Agent and add the private key of the Jenkins user.
In the Build Step, choose "Execute shell" and provide the Ansible command:
ansible-playbook -i /etc/ansible/hosts playbook.yml
Save and Build the Job.
Final Verification
Once everything is set up:
By following these steps, Jenkins will be able to securely connect to a remote server and execute Ansible playbooks, ensuring smooth automation in your DevOps pipeline. ??
Have you set up Jenkins and Ansible before? Share your experience and challenges in the comments below! ??