Date:

Setting Up a Jenkins Host for Ansible Job Execution on a Remote Server

Integrating Jenkins with Ansible: A Step-by-Step Guide

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, use 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

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 > /dev/null

Step 3: Enable SSH Access for Jenkins

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, 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.

Latest stories

Read More

LEAVE A REPLY

Please enter your comment!
Please enter your name here