Site icon UnixArena

Jenkins – Passing Extra variables for Ansible Playbook

Ansible - Jenkins Variable Mapping

Ansible - Jenkins Variable Mapping

Ansible Playbooks are often written with many extra variables to pass the human inputs. In Ansible Tower /AWX,  “SURVEY” feature is used to pass the extra variable to the playbook. If you are using Jenkins as your front-end graphical interface, you might be wondering how to pass the ansible extra variable. This article will walk through how to get the variable input in Jenkins to build and passing to Ansible playbook.

You must install “Build With Parameters” Plugin to enable the option in a job. Let’s see the demonstration.

Capturing Variable input in Jenkins:

1. Login to Jenkins web page.

 

2. Edit the Jenkins job to enable the parameterized build option. If you do not have an Ansible playbook – Jenkins job create a one as described here.

 

3.  Click on the configure job and select “This project is parameterized”  option. Click on “Add Parameter”

Check Project – Parameterized

 

4. Select the parameter type according to your playbook variable.

Jenkins – String Parameter – Ansible extra-vars

 

5. Enter the Meaningful parameter name which will be prompted during the execution. Note that this variable is just within Jenkins level.

Parameter – Jenkins – String – Extra- vars – AnsibleNo

 

Ansible sample Playbook with an extra variable:

6. Here is my sample playbook with an extra variable. Here FS_NAME is an extra variable which needs to be passed during the playbook execution.

---
- hosts: all
  gather_facts: no

  tasks:
  -  name: Root FS usage
     shell: df -h {{ FS_NAME }} |awk ' { print $5 } ' |grep -v Use
     register: dfroot

  -  debug:
       msg: "System {{ inventory_hostname }}'s {{ FS_NAME }} FS utiliation is {{ dfroot.stdout }}"

 

Mapping Jenkins variable with Ansible Extra variable: 

7.  Click on Jenkins job build tab and navigate to “Invoke Ansible Playbook”. Click on “Advanced” tab.

Invoke Ansible Playbook – Advanced -Jenkins

 

8. Click on “Add Extra variable”.

Add Extra Variable – Invoke Ansible Playbook Plugin

 

9. Update the ansible playbook extra variable in key field (which is FS_NAME – Refer step 6).  In the value field, update the Jenkins variable which we have created in step 5.  Save the job.

Enter Ansible playbook – extra variable

 

We have successfully mapped the Jenkins parameter to Ansible extra variable.

 

Test our work :

10. Click on “Build with Parameter”.

Build with Parameters – Jenkins – Ansible

 

11. The job will be prompted to enter the value for FS_NAME.  Here, I have passed “/var” as input.

Jenkins Build job with Parameter – Ansible

 

12. Here are the job results.  In this screenshot, you could see that FS_NAME variable got the value which I have passed in the above step.

Console output – Ansible Playbook

Similar to this, you could add as many as variable and map to ansible playbook’s extra variables.

Hope this article is informative to you. Share it! Comment it!! Be Sociable!!!

Exit mobile version