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

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 configuring 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        

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

  1. Install the Ansible Plugin in Jenkins:

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:

  • 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! ??



要查看或添加评论,请登录

社区洞察

其他会员也浏览了