How to configure Jenkins Pipeline with Ansible
Jenkins is an automation server which enables the developer to reliably build, test and deploys their software. It allows users to setup a Continuous Integration or Continuous Delivery pipeline for almost any combination of languages and source code repositories in a simple way.

In this article, I will be showing you how you can use Jenkins to install packages to your target servers via Ansible. This article is a continuation of the article How to use Ansible AWS EC2 Dynamic Inventory Plugin. In this part of the series, I will be demonstrating how you can integrate Jenkins with Ansible to execute Ansible playbook from Jenkins with a click. Some prior knowledge about Jenkins and Ansible can be a bonus when following this article. Here are the prerequisites for this article.

Prerequisites:

In Jenkins Server, ensure the following tools are installed.

Ansible version >= 2.9.13

Boto3

Botocore

git

AWS CLI

For this demonstration, I have spun up two EC2 instances in AWS, which will serve as my Jenkins and the Target server. Ensure you open the necessary ports in your security group to enable communication between your Jenkins and Target Server. Furthermore, I will be using Ansible AWS EC2 dynamic inventory plugin for inventory management; therefore, I will be tagging my target server with the following tags.

The source code used in the demo can be found in GitHub https://github.com/DevMasterPro/ansible-demo.

Make sure to tag your resources with meaningful tags as you are going to use these tags further down the article to target your hosts where the ansible-playbook will be running.

Now that everything is ready, so let’s go ahead and start implementing the Jenkins pipeline.

Step1: Install the Ansible Plugin in Jenkins.

Manage Jenkins > Manage Plugins > Available > and search for ansible

Once you locate the plugin, install it and proceed to the next step.

Step2: Now that you have installed the plugin, let’s go ahead and store the secrets Ansible will use to authenticate against the target server to execute the playbooks.

Manage Jenkins > Manage Credentials > System > GLobal Credentials > Add Credentails

Once you create the Credential, note the credential ID as it will be needed later in the article.

Step3: Create a Jenkins pipeline

New Item > Give the pipeline a name > Pipeline

Step4: Configure pipeline source code

In this step, you would need to use your Ansible repository as the source. I will be using the following git repository https://github.com/DevMasterPro/ansible-demo.git as the source code of this pipeline. In the repository, under the folder Jenkins, I have created a Jenkins file called jenkinsfile.ansible_demo :

In the Jenkinsfile, I have defined two parameters TagKey and TagValue. I will be using these parameters to create my inventory list dynamically with the help of Ansible EC2 dynamic inventory plugin. In the stages section, I have referenced the ansiblePlaybook function, which is provided by the Ansible plugin you have installed at step1.

Note: The credentialsId needs to be replaced with the credential you have created in Step2.

In the script path put the path to your Jenkinsfile.

Once you have configured everything, click apply and save.

Step5: Now that you have everything configured, let’s go ahead and run the pipeline.

Step6: The outcome of the execution should look something like the below screenshots:

Step7: let's go one step further to check if the Ansible playbook has run to install the Nginx in the target server.

If you see the above Nginx web page in your browser, you have completed this article successfully.

Note: Whilst running the Jenkins pipeline if you encounter issues related to Host key verification, you can eliminate it by setting the host key verification to false in the ansible.cfg file in your Jenkins server.

If you have any further query you can connect with me on Linkedin →
['Shahin Ahmed']
['AWS', 'Jenkins', 'Ansible', 'Ansible Playbook', 'Pipeline']