Ansible  Plugin For Jenkins

The Ansible Plugin for Jenkins allows you to trigger jobs when new code is committed to a GitHub repository, or when existing code is changed in a GitHub repository. Ansible uses SSH to communicate over the network, so you will need to setup your GitHub repositories to allow public key based authentication or use a private key that has access to all of your repositories.

Ansible has a plugin for Jenkins. The two were meant to be together. This very short post will show you how to get up and running with the Ansible Plugin For Jenkins.

Hi, this is Dinesh Gill, an offshore DevOps engineer from Mumbai. Welcome to my blog for Ansible Plugin for Jenkins, the most important automation tool. I will start the blog with a brief intro about Jenkins followed by installation guide and use case scenarios. My last piece of advice would be to register yourself at Ansible Tower Training in Ahmedabad and get certified with the industry-leading tools.

Ansible  Plugin For Jenkins

In this article, we are talking about Jenkins Integration with Ansible. How we can execute Ansible Playbook using Jenkins.

As we all know Jenkins is a well-known open-source continuous integration and continuous development automation tool, I talked a lot about Jenkins and today also I’m talking about Jenkins.

In my previous article, we talked about how to configure Jenkins with the help of the Ansible Playbook.

Configure Jenkins With Ansible

Today we will gonna talk about How to configure Jenkins with the help of Ansible.

medium.com

We are using this ansible-playbook for demo.

Let’s Move on Jenkins now

If you are new with Jenkins I recommend go through my previous article in which I talked about how to configure Jenkins.

Setup Jenkins on CentOS with Docker for Selenium

I haven’t found any walk-through about setting up Jenkins on CentOS with Docker for Selenium, and since I got to do it…

medium.com

There is 2 way to execute ansible-playbook on Jenkins.

1. Use shell/bash script

2. Use the Ansible plugin

1. Use shell/bash script

The playbook execution is defined in the Build > Execute Shell section of the job. Generation of input values and playbook execution are scripted in this section and that would look like this:

#env is Jenkins build parameter$ `gen-hosts-list $env` > /path/to/hosts_list$ ansible-playbook /path/to/ansible-playbook.yml -i /path/to/hosts_list -u AUTO_USER --private-key=/path/to/private-key

2. Use the Ansible plugin

Step 1) For Jenkins Pipeline we need to install the Ansible plugin.

Go to Manage Jenkins > Manage Plugins >Available > search Ansible.

If you are already installed Ansible Plugin on your Jenkins It will display in the Installed section.

Now we can see the Invoke Ansible Playbook option in the Build Environment section but we need to configure Ansible path for Jenkins.

Now let’s configure Ansible on our Jenkins.

Go to Manage Jenkins > Global Tool Configuration > It will display Ansible on the list.

Now let’s Create New project to execute Ansible playbook.

Step 2) Goto Jenkins Home > New Item > Create New Freestyle Project.

We create a new freestyle project now lets configure our project.

Goto source code management section and add your code repository here you are free to use any of the source code management platforms like Github, Gitlab and also Bit bucket.

Now let’s configure ansible plugin.

Goto Build section and select Invoke Ansible Playbook. Once you select that option it will display ansible-playbook configuration option like below.

Let see all option provided by Ansible Plugin.

  • Ansible Installation: It will display all Ansible installation list that we are configured in Global Tool Configuration > Ansible.
  • Playbook path: We have to provide the Absolute or Relative path of our Ansible-playbook file.
  • Inventory: It gives 3 options to configure our host file.

Do not specify Inventory: We have to give our host list in the subdomain option.

File or host list: add a path of our HOST file.

Inline content: It means we are set our host list on the starting of our ansible-playbook file.

  • Host subset: If we want to filter a specific set of the host from the provided Host file.
  • Credentials: Configure added host’s username and password.
  • Vault Credentials: IF you are using ansible vault then configure that credentials.
  • become: If you want to set a specific user for ansible-playbook execution select and add the name of that user.
  • sudo: If you need to use a sudo user(root) then select this and add the name of that user.

In advance View.

  • Tags to run: If you are using tag base script then Jenkins will run only specified tag.
  • Tags to skip: If you are using tag base script then Jenkins will skip the specified tag.
  • Task to start at: If you are using tag base script then Jenkins will start execution with the specified task.
  • Number of parallel processes: Specified number of tasks that can run parallel.
  • Disable the host SSH key check: Disable the validation of the host’s SSH server keys.
  • Unbuffered stdout: Skip standard output buffering for the ansible process. The ansible output is directly rendered into the Jenkins console. This option can be useful for long-running operations.
  • Colorized stdout: Check this box to allow ansible to render ANSI color codes in the Jenkins console.
  • Extra Variables: Want to add some variable for ansible-playbook execution.
  • Additional parameters: Any additional parameters to pass to the ansible command.

Press Save 🎊 🎉🤖🎊 🎉

We have successfully integrated an ansible-playbook in Jenkins, Now Click on Build Now. It will execute ansible-playbook successfully.

Thank you for reading, if you have anything to add please send a response or add a note!

Leave a Comment