In our previous articles, we learned how to install Jenkins and Ansible and how to play with them. In this article, We are going to learn how to run Ansible Playbooks from Jenkins. You can run the Ansible playbook through Jenkins in multiple ways. In this tutorial we are going to learn about the default method which is best and the recommended method that Ansible to be configured on Jenkins Master to run Ansible job.

Prerequisites

  • Jenkins Server up and running
  • You can follow this tutorial and install Jenkins

  • Ansible installed on Jenkins server
  • You can follow this tutorialand install Ansible

    Install Ansible Plugin in Jenkins

    To use Ansible and run Ansible’s playbook through Jenkins, you need to, install Ansible Plugin. To install Ansible Plugin, you need to follow the steps below:
  • Go to Manage Jenkins

  • alt text
  • Go to Manage Plugins

  • alt text
  • Search for Ansible plugin

  • alt text
  • Install the Ansible plugin
  • Configure Ansible in Jenkins Global Tool Configuration

  • Go to Manage Jenkins –> Global Tool Configuration

  • alt text
  • Then search for Ansible
  • After that:Ansible installations > Add Ansible

  • alt text

    Provide the name as ansible (you can use any name) and /usr/bin/as Path to ansible executables directory space. (You can find this path by execusting ‘which ansible’ on the server

  • Click Save button
  • Now, we have almost finished with our configurations, we have two methods of running the playbook through Jenkins jobs:

  • Jenkins Freestyle Job
  • Jenkins Pipeline Job
  • Method 01: Run Ansible Playbook as Jenkins Freestyle Job

  • Navigate to Jenkins Dashboard
  • Then click on New Item
  • Then provide a name for Enter an item name field & choose Freestyle project option
  • Then click on Ok button
  • After that it will redirect you to the Job Configure section. In that section navigate to Build section and choose Invoke Ansible Playbook as Add build step option

  • alt text
  • After that configure the rest acording to your Ansible Playbook configuration
  • (As an example, we are going to use name ansible for Ansible Installtion because we used ansible as name in the Ansible installations > Add Ansible step and I am going use /home/ubuntu/ansible-playbook/ansible.yml as Playbook path because my Playbook is located at that directory)

  • Save the Job and click on Build Now
  • Verify the Console log
  • Method 02: Run Ansible Playbook with Jenkins Pipeline Job

  • Navigate to Jenkins Dashboard
  • Then click on New Item
  • Then provide a name for Enter an item name field & choose Pipelinet option
  • Then click on Ok button
  • After that it will redirect you to the Job Configure section. In that section navigate to Advanced Project Options section and choose Pipeline Script as Definition

  • In Script section paste below script:

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    pipeline {
    agent any
    stages {
    stage(" execute Ansible") {
    steps {
    ansiblePlaybook (
    installation: 'ansible', //Name of the Ansible tool which you had provided in Jenkins Tools configuration
    inventory: '', //Ansible inventory file path
    playbook: '/home/ubuntu/ansible-playbook/ansible.yml' //Name along with the path of the ansible playbook
    )
    }
    }
    }
    }
  • Save the Job and click on Build Now
  • Verify the Console log
  • What's next

    In our next tutorial we are going to learn How we can run Ansible Playbook through Jenkins by Executing Shell Command

    Conclusion

    In this tutorial, we learned how to run Ansible Playbook using Jenkins. If you have any issue regarding this tutorial, mention your issue in comment section or reach me through my E-mail. You can obtain all the related script files for this tutorial from this GitHub repository.

    Happy Coding