How to Run Ansible Playbook From Jenkins (3 Easy Methods)

How to Run Ansible Playbook from Jenkins - DevOpsBuzz Guide

Ansible is open-source software that automates software provisioning, configuration management, and application deployment.

Ansible connects via SSH, remote PowerShell, or via other remote API and it is now one of the basic requirements to run the Ansible job through CI tools such as Jenkins to automate the server provisioning and configuration.

This helps developers and other users to directly run specific jobs without the deep knowledge of Ansible.

In this detailed guide, we will show you how to run Ansible Playbook from Jenkins using three different methods.

So, if you want to learn how to integrate Ansible with Jenkins, this is the complete step-by-step process (with screenshots).

Run Ansible Playbook from Jenkins: Your Options

You can run the Ansible playbook through Jenkins in multiple ways:

  • Method #1. Using default method which needs Ansible to be configured on Jenkins Master: best and the recommended method to run Ansible job.
  • Method #2. Executing Ansible playbook as Shell command or Windows batch command: can be used for testing purposes to run Ansible Playbook on remote host.
  • Method #3. Running Job on a separate Node/Agent where you can install Ansible: suited to cases where Jenkins master doesn’t allow Ansible installation or is already having performance crunch.

How to Run Ansible Playbook from Jenkins: Step-by-step

Let’s start with the recommended way to run the Ansible playbook with Jenkins Job or Jenkins Pipeline.

Method #1. Using Default Method (Ansible Configuration on Jenkins Master)

Pre-requisites:

  • Install Ansible Plugin through Manage Plugins in Jenkins UI
  • Install Ansible package on Jenkins Master
  • Configure Ansible Installation Path in Global Tool Configuration in Jenkins UI

So, the very first step to use the Ansible module in Jenkins is to install and configure Ansible in Jenkins.

Step #1. Install Ansible Plugin in Jenkins

To use Ansible and run Ansible’s playbook through Jenkins, you need to:

Install Ansible Plugin, Go to Manage Jenkins.

Manage Jenkins

Go to Manage Plugins.

Manage Jenkins Plugins

Search for Ansible plugin.

Find Ansible Plugin

Install the Ansible plugin.

Install Ansible Plugin in jenkins

Choose the appropriate option.

Ansible Jenkins Plugin Installation Success

You can verify the status of installation from the Installed tab.

Verify installed plugin

Now, install the Ansible on Jenkins master.

Step #2. Install Ansible package on Jenkins Master

Install the ansible through your server’s package manager, such as yum, apt-get, etc.

Install Ansible through Yum
Install Ansible through yum

Verify Ansible Version.

Verify Ansible version

Find the Ansible location which we need to provide in Jenkins Tools configuration.

Verify Ansible location

Create a test playbook ‘ansible.yml’ which prints the username.

---

- name: Run the test playbook
  gather_facts: false
  hosts: localhost
  tasks:
    - name: Get the username running this playbook
      become: false
      local_action: command whoami
      register: username_on_the_host

    - name: Display the User name
      ansible.builtin.debug:
        msg: "Hello User {{ username_on_the_host.stdout }} from Jenkins and Ansible!!"

Now, we need to configure Jenkins Tools Configuration to let Jenkins know the Ansible location on the server.

Step #3. Configure Ansible in Jenkins Global Tool Configuration

Go to Manage Jenkins –> Global Tool Configuration.

Jenkins Global Tool Configuration

Configure the Ansible Tool Configuration.

Ansible installations Add Ansible.

Provide the name; for example: ansible and /usr/bin/ to the Path to ansible executables directory space. This is the path we found when we executed ‘which ansible’ on the server.

Add Ansible location in Jenkins Global Tool Configuration

Click Save button.

Now, once you have done the required configurations, you have two options of running the playbook through Jenkins jobs:

  • Jenkins Freestyle Job
  • Jenkins Pipeline Job

Step #4.1. Run Ansible Playbook as Jenkins Freestyle Job

Let’s move to Jenkins Dashboard

Create the ‘Freestyle Project

Click on New Item and provide Job details.

Jenkins Freestyle Job

Configure the Job to run the Playbook.

Choose the ‘Invoke Ansible Playbook‘ option as Build Step.

Invoke Ansible Playbook as Build step

And configure the same playbook which was created to print the username.

Configure Ansible Jenkins Freestyle Job

Save the Job and click on Build Now.

Verify the Console log.

Jenkins Freestyle Job Console Log

Step #4.2. Run Ansible Playbook with Jenkins Pipeline Job

To run Ansible playbook with Jenkins Pipeline:

Create a Jenkins Pipeline Project

Click on New Item and provide Job details.

Jenkins Pipeline Job

Configure the Pipeline script.

Jenkins Pipeline Script

Here:

  • inventory: ‘ansible inventory file’,
  • installation: ‘Name of the Ansible tool which you had provided in Jenkins Tools configuration’,
  • limit: ‘if need to limit it to any host where you want to run the playbook’,
  • playbook: ‘name along with the path of the ansible playbook’,
  • extras: ‘options and variables which you want to add’

Save the Job and click on Build Now and Verify the Console log.

Jenkins Pipeline Job Console Log

Let’s move to the traditional way of running Ansible playbook through Jenkins freestyle or Pipeline job by executing Shell:

Method #2. Run Ansible Playbook through Jenkins by Executing Shell Command

Pre-requisites:

  • Install Ansible package on Jenkins Master: if you want to execute shell/batch on Master
  • Install Ansible package on dedicated Jenkins Node: if you want to execute shell/batch on Node

This time, instead of choosing the Ansible module as Build Step, choose the ‘execute shell’ as the build step.

Since the node was Linux node, I’ve used ‘Execute Shell’ method whereas, if you need to run the same on Windows machine, you need to use the ‘Execute Windows Batch command’

Choose ‘Execute Shell‘ as Build Step

Provide the appropriate ansible-playbook command along with required parameters, I’ve just used the basic one:

Jenkins Build Step to Execute Shell Command

Save the Job and click on Build Now and Verify the Console log.

Jenkins Job Shell Console Log

All good.

You now know how to integrate Ansible with Jenkins after installing/configuring Ansible on Jenkins master.

However, you might need to explore another option to run the Ansible playbook from the Jenkins Node machine which has Ansible installed on it.

So, let’s check out this option as well:

Method #3. Run Ansible Playbook from Jenkins With a Dedicated User and Jenkins Node

I received a request from one of our readers on our post for the in-depth Jenkins setup, who was facing an issue with running the Ansible playbook from Jenkins Node with a dedicated user on a remote host as per his project requirements.

Though the best practice is to configure Ansible on the Master node through Global Tool Configuration; in some scenarios, we need to consider these kinds of scenarios as well.

So, the request has the below requirements:

  • Jenkins should be able to run Ansible playbook
  • There shouldn’t be any ansible installation or configuration on Jenkins Master node
  • Ansible playbook should be executed through the dedicated user
  • Jenkins agent/node should have these configuration and Job should run on the specific node only

Pre-requisites:

  • Install Ansible Plugin through Manage Plugins in Jenkins UI
  • Install Ansible package on dedicated Jenkins Node

For this demo, I have removed the Ansible from “Jenkins Global Tools Configuration

Remove Ansible from Jenkins Global Tool Configuration

Step #1. Install Ansible Plugin

We have already installed the Ansible plugin, so we can go to the next step.

Step #2. Create Dedicated User on Linux Machine

Now, log in to the Linux server

For this demo, I have used a Linux machine and will turn this machine into Jenkins Node. If you need to run the same scenario on a Windows machine, then please follow our guide on Jenkins Windows Slave setup.

Add Dedicated User as per the requirement and verify it.

Add Unix User

Provide Sudo access to my user since its a blocked instance.

Add user to sudoers list

Login through new user.

Login as new user

Create ssh key for the user.

Generate ssh keys

Create the authorized key and provide appropriate permission.

This is the most important step in order to establish ssh connection based on keys.

Create and updated Authorized key

Step #3. Install Required Java and Ansible Through Yum

(Or, with your relevant package manager)

Install the prerequisite Java.

Install Java through yum package manager

Verify Java Version.

Verify Java version

Install Ansible on Jenkins Node.

Install Ansible through yum package manager

Verify Ansible Version.

verify ansible version

Step #4. Create test Ansible playbook

Create playbook which records the current username and print it.

Create Ansible playbook to records current username and print it

Change playbook permissions.

Change permissions of the playbook

Step #5. Create Jenkins Node directory

Create a directory for Jenkins to run all the nodes activities on this server.

Create Jenkins workspace directory

This completes the server related activities.

Now, let’s go back to Jenkins and add the newly created node to run all the ansible playbooks.

Step #6. Add Jenkins Node

Go to ‘Manage Jenkins

Manage Jenkins

Go to ‘Manage Nodes

Manage Jenkins nodes

Click on ‘New Node

Add new Jenkins Node

Provide Node details and click OK.

Add Ansible Jenkins Slave

Provide the relevant configuration.

** Add the Credentials – choose ‘SSH Username with private key’ option and provide Linux server’s user name and Private key

Jenkins add credential with username and private key

Click on Add and select the same credential on the Agent Configuration page.

Jenkins agent configuration

Click on Save. And click on Launch agent.

Jenkins agent connection success message

Now, proceed for the Jenkins Job creation to run the Ansible playbook on the newly added node/agent.

Step #7. Create Jenkins Job

Click on New Item and provide Job details.

Add Jenkins new job

Configure the Job.

Make sure to restrict this job to run against the Ansible agent/slave/node only.

jenkins job agent restriction

And choose the ‘Invoke Ansible Playbook‘ option in Build.

Jenkins invoke ansible-playbook

Save the Job and then click on Build.

Build Jenkins job

Check the Console log to verify the execution.

Jenkins Console Log to show successful Ansible playbook execution

With this method (#3), there is No need to configure Ansible on the Master server or update Jenkins Tool configuration, just install the plugin.

You can use this approach for both Jenkins Freestyle job as well as a Pipeline job.

You can escape the installation of the plugin as well if you need to execute ansible-playbook as a shell/batch command.

As I mentioned before, this is not the recommended way to use Ansible with Jenkins but sometimes turns out to be a very useful alternative in restricted environments.

All done. You should now be able to run the Ansible playbook with a dedicated user from the node server.

Wrapping Up

Okay, there you have it!

You now know three different ways to run the Ansible playbook from Jenkins.

If you get stuck with any of the methods shown on how to integrate the Ansible Playbook with Jenkins, drop a comment below.

So, which method did you opt for in the end?

If you like this guide, do share this on LinkedIn and subscribe to our free newsletter. You can also consider following us on our LinkedIn page where we regularly share tips and tricks for DevOps engineers.

Keep the quest on!

2 thoughts on “How to Run Ansible Playbook From Jenkins (3 Easy Methods)”

  1. This is interesting, but I like to develop and test playbooks outside of jenkins, commit them to a version control system like github/, then have Jenkin check out the repository and run the commands defined in the Jenkins file.

    Have you written an article about that use case?

    1. Hi @ikomrad,

      Thanks for the feedback.
      I’ve not written an article on this topic yet but have used the same several times with my projects.

      Basically, if you are using AWX tower, then you can set up the Projects and Dynamic inventory, where you can use gitlab/github/or any other VC to store your ansible roles, playbooks, and inventories.

      Or if you are using standalone ansible, then just create the repository in your favorite version control and configure the ‘Source Code Management’ in Jenkins pipeline/Job with the repository details.
      Configure the Build triggers and then use one of the methods provided in this article to run your ansible-playbook.

      Please do give it a try and let me know in case of any issues.

      Happy learning !!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Scroll to Top