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”
4. Select the parameter type according to your playbook variable.
5. Enter the Meaningful parameter name which will be prompted during the execution. Note that this variable is just within Jenkins level.
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.
8. Click on “Add Extra variable”.
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.
We have successfully mapped the Jenkins parameter to Ansible extra variable.
Test our work :
10. Click on “Build with Parameter”.
11. The job will be prompted to enter the value for FS_NAME. Here, I have passed “/var” as input.
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.
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!!!
Devops says
HI Unixarena
Is it possible to pass jenkins variable without parameter.
vinayak sawant says
Hello UnixArena
Thanks for this article. with the help of this article I created playbook and integrated in Jenkins it work as expected but if i convert the same playbook in ansible role and ran from the Jenkins it does not work
So how to integrate Ansible Roles in Jenkins.
Lingeswaran R says
In Ansible, Roles will be called using playbook like below.
1. Create new playbook and call the role.
Ex: Call_role.yaml
– hosts: all
roles:
– role: ROLE_NAME
2. integrate the Call_role.yaml in Jenkins (Just like playbook integration)
Lingesh
Gen Luna says
Hi Let say i want to check the disk space of more than 1 filesystem
e.g i put in the parameter “/var” “/root”
how to do it?