Install and Use the Ansible Jenkins Plugin

This tutorial explores how we can integrate Ansible in our Jenkins pipelines using the Ansible plugin. This allows us to run the Ansible tasks as a build step in our Jenkins pipeline.

This is an incredible feature which allows the CI/CD tools such as Jenkins to integrate with Ansible which gives you more control and automation of the tasks that you are executing.


To follow along with this tutorial, you need to ensure that you have the following requirements:

  1. A Jenkins controller or Agent with installed Ansible CLI.
  2. The latest Python interpreter which is installed on the Jenkins controller/Agent.
  3. Ansible inventory which is configured with your target machines.

You may also need sufficient permissions to execute the commands on the Agent/Controller or the Ansible nodes.

Sample Project Configuration

Ansible Inventory Layout

Let us break down the project setup. For this tutorial, we have an Ubuntu server on which install the Apache web server using Ansible.

The following shows the layout of our Ansible host inventory file:


The previous inventory contains a webserver category with only one host that points to the IP address of the Ubuntu server.

Ansible Playbook

Next is the Ansible playbook that we wish to execute on the server. Finally, as mentioned, we need to install the Apache Webserver on the Ubuntu machine.

The corresponding playbook is as follows:

- hosts: all
  become: true
  - name: Update package manager cache
      update_cache: true
  - name: Install Apache web server
      name: apache2
      state: present
  - name: Start Apache web server
      name: apache2
      state: started

The provided playbook starts by updating the package manager cache on the target host. Since we only have one host on the Ansible inventory file, we can set the hosts to all as shown in the previous playbook.

Next, we install the Apache Web Server using the Ansible apt module. Finally, we start the webserver using the service module.

Jenkinsfile Script

The last file that we need is the Jenkinsfile which defines the stages and the steps of the Jenkins pipeline.

The following shows the target Jenkinsfile:

pipeline {
  agent { label 'debian' }
  stages {
    stage('Run Ansible Playbook') {
      steps {
        sh 'ansible-playbook -I /etc/ansible/hosts -u ubuntu ~/playbooks/install-apache.yml'

In this case, the Jenkinsfile uses the Agent with the “debian” label . Then, the Jenkinsfile invokes the ansible-playbook command to run the install-apache.yml playbook on the target host.

Building a Jenkins Pipeline

Once the target files and tools are ready, we can set up a sample Jenkins pipeline.

Login into your Jenkins controller and create a new Jenkins pipeline.

In the pipeline section, select the “Pipeline script” and paste the Jenkinsfile that we created earlier.

Click “Save” and choose “Build Now”.

Once we click build, Jenkins invokes the commands and runs the Ansible tasks which are defined in the playbook.


You discovered how to use Jenkins to run the Ansible playbooks on the target host using the Jenkins Ansible plugin.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list