DEV Community

Cover image for How to Set Up a Jenkins Host on a Remote Server for Ansible Job Execution
Muhammad Sarim
Muhammad Sarim

Posted on

How to Set Up a Jenkins Host on a Remote Server for Ansible Job Execution

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 configure Jenkins to execute Ansible jobs on a remote server.

Why is This Setup Necessary?

Without configuring Jenkins to communicate with the remote server:

  • Jenkins cannot SSH into the remote host.
  • Ansible playbooks and YAML files will fail to execute.
  • Automated deployments and infrastructure provisioning will not work.
  • CI/CD pipelines relying on Ansible automation will break.

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
Enter fullscreen mode Exit fullscreen mode

Start and enable Jenkins:

sudo systemctl start jenkins
sudo systemctl enable jenkins
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Give Jenkins sudo privileges:

echo 'jenkins ALL=(ALL) NOPASSWD: ALL' | sudo tee /etc/sudoers.d/jenkins
Enter fullscreen mode Exit fullscreen mode

Step 3: Enable SSH Access for Jenkins
On the remote server, switch to the Jenkins user:

sudo su - jenkins
Enter fullscreen mode Exit fullscreen mode

Generate an SSH key:

ssh-keygen -t rsa -b 4096
Enter fullscreen mode Exit fullscreen mode

Copy the public key to the remote server where Ansible will execute:

ssh-copy-id jenkins@<REMOTE_SERVER_IP>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Verify the installation:

ansible --version
Enter fullscreen mode Exit fullscreen mode

Step 5: Configure Jenkins to Run Ansible Jobs

  1. Install the Ansible Plugin in Jenkins:
Go to Jenkins Dashboard ā†’ Manage Jenkins ā†’ Manage Plugins.

Search for "Ansible Plugin" and install it.

Restart Jenkin
Enter fullscreen mode Exit fullscreen mode
  1. 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).
Enter fullscreen mode Exit fullscreen mode
  1. 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.
Enter fullscreen mode Exit fullscreen mode

Final Verification

Once everything is set up:

  • Run a test job in Jenkins.
  • Check if the Ansible playbook executes successfully.
  • If there are permission errors, ensure that the Jenkins user has the correct SSH access and sudo privileges. 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! šŸ‘‡

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)