How to run Ansible playbook from Jenkins job

Fri, Oct 14, 2016

#Ansible #Jenkins #devops

Using Jenkins and Ansible in your development environment can be really powerful. And now I am going to show you how to invoke an Ansible playbook from a Jenkins job.

First of all, we create a simple Ansible playbook, like the following.

- hosts: localhost
    - name: Print message
      debug: msg="Hello from Ansible!!"

When we run it, the outcome will look something like this

$ ansible-playbook ansible.yml
 [WARNING]: Host file not found: /etc/ansible/hosts

 [WARNING]: provided hosts list is empty, only localhost is available

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [Print message] ***********************************************************
ok: [localhost] => {
    "msg": "Hello from Ansible!!"

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0 

Note: In newer version of Ansible, you can run the playbook without the hosts file, but in earlier version of Ansible, you might get the “ERROR: provided hosts list is empty” error. To overcome this, either upgrade your Ansible distribution or specify the hosts file when running the playbook.

After we have the Ansible script, let’s configure the Jenkins instance. First we need to make sure the Jenkins Ansible plugin is installed.

  1. click on manage plugins
  2. go to available tab and search for Ansible.
  3. Select the plugin and click on the install button. –insert image here

Once we have the plugin installed, we will start create a new Jenkins job. –create new jenkins job –in the configuration,

  1. prepare an ansible script
  2. install jenkins ansible plugin
  3. configure a new job
  4. specify the hosts(this is optional if you are using an older version of Ansible)
  5. run and voila!


So we’ve learned how to

  • install Ansible plugin in Jenkins
  • configure a Jenkins job to run Ansible playbook(and how to specify hosts file)