<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Muhammad Sarim</title>
    <description>The latest articles on DEV Community by Muhammad Sarim (@titans_sarim_42380409554b).</description>
    <link>https://dev.to/titans_sarim_42380409554b</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2979596%2F290b0a60-96d7-4ba6-833a-faaec5af0cd6.jpg</url>
      <title>DEV Community: Muhammad Sarim</title>
      <link>https://dev.to/titans_sarim_42380409554b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/titans_sarim_42380409554b"/>
    <language>en</language>
    <item>
      <title>How to Set Up a Jenkins Host on a Remote Server for Ansible Job Execution</title>
      <dc:creator>Muhammad Sarim</dc:creator>
      <pubDate>Wed, 26 Mar 2025 22:49:04 +0000</pubDate>
      <link>https://dev.to/titans_sarim_42380409554b/how-to-set-up-a-jenkins-host-on-a-remote-server-for-ansible-job-execution-3dl0</link>
      <guid>https://dev.to/titans_sarim_42380409554b/how-to-set-up-a-jenkins-host-on-a-remote-server-for-ansible-job-execution-3dl0</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why is This Setup Necessary?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Without configuring Jenkins to communicate with the remote server:&lt;/p&gt;

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

&lt;p&gt;By setting up a Jenkins host on a remote server, we ensure seamless automation, allowing Jenkins to trigger Ansible jobs and execute tasks efficiently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step-by-Step Guide to Configuring Jenkins for Ansible Execution on a Remote Server&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Step 1: Install Jenkins on the Remote Server&lt;/strong&gt;&lt;br&gt;
If Jenkins is not already installed, you can set it up using the following steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update &amp;amp;&amp;amp; 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 &amp;gt; /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 &amp;gt; /dev/null

sudo apt update

sudo apt install jenkins -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start and enable Jenkins:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl start jenkins
sudo systemctl enable jenkins
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Create a Jenkins User on the Remote Server&lt;/strong&gt;&lt;br&gt;
To allow Jenkins to execute Ansible jobs on a remote server, create a dedicated Jenkins user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo useradd -m -s /bin/bash jenkins
sudo passwd jenkins
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Give Jenkins sudo privileges:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo 'jenkins ALL=(ALL) NOPASSWD: ALL' | sudo tee /etc/sudoers.d/jenkins
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Enable SSH Access for Jenkins&lt;/strong&gt;&lt;br&gt;
On the remote server, switch to the Jenkins user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su - jenkins
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generate an SSH key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-keygen -t rsa -b 4096
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the public key to the remote server where Ansible will execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-copy-id jenkins@&amp;lt;REMOTE_SERVER_IP&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Install Ansible on the Remote Server&lt;/strong&gt;&lt;br&gt;
If Ansible is not already installed on the remote server, install it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update &amp;amp;&amp;amp; sudo apt install ansible -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify the installation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ansible --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5: Configure Jenkins to Run Ansible Jobs&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install the Ansible Plugin in Jenkins:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Go to Jenkins Dashboard → Manage Jenkins → Manage Plugins.

Search for "Ansible Plugin" and install it.

Restart Jenkin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Configure Jenkins to Use Ansible:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Go to Manage Jenkins → Global Tool Configuration.

Under Ansible installations, provide the Ansible installation path (e.g., /usr/bin/ansible).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a Jenkins Job for Running Ansible Playbooks:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Final Verification&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once everything is set up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run a test job in Jenkins.&lt;/li&gt;
&lt;li&gt;Check if the Ansible playbook executes successfully.&lt;/li&gt;
&lt;li&gt;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. 🚀&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Have you set up Jenkins and Ansible before? Share your experience and challenges in the comments below! 👇&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>microservices</category>
      <category>kubernetes</category>
      <category>jenkins</category>
    </item>
  </channel>
</rss>
