• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

UnixArena

  • Home
  • kubernetes
  • DevOps
    • Terraform
    • Jenkins
    • Docker
    • Openshift
      • OKD
    • Ansible engine
    • Ansible Tower
      • AWX
    • Puppet
  • Cloud
    • Azure
    • AWS
    • Openstack
    • Docker
  • VMware
    • vCloud Director
    • VMware-Guests
    • Vcenter Appliance 5.5
    • vC OPS
    • VMware SDDC
    • VMware vSphere 5.x
      • vSphere Network
      • vSphere DS
      • vShield Suite
    • VMware vSphere 6.0
    • VSAN
    • VMware Free Tools
  • Backup
    • Vembu BDR
    • Veeam
    • Nakivo
    • Azure Backup
    • Altaro VMBackup
    • Spinbackup
  • Tutorials
    • Openstack Tutorial
    • Openstack Beginner’s Guide
    • VXVM-Training
    • ZFS-Tutorials
    • NetApp cDot
    • LVM
    • Cisco UCS
    • LDOM
    • Oracle VM for x86
  • Linux
    • How to Articles
    • Q&A
    • Networking
    • RHEL7
  • DevOps Instructor-led Training
  • Contact

Jenkins – Passing Extra variables for Ansible Playbook

April 1, 2019 By Cloud_Devops 4 Comments

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
Check Project – Parameterized

 

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

Jenkins - String Parameter - Ansible extra-vars
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 - Ansible
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
Invoke Ansible Playbook – Advanced -Jenkins

 

8. Click on “Add Extra variable”.

Add Extra Variable - Invoke Ansible Playbook Plugin
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
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
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
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
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!!!

Filed Under: Ansible engine, Automation, Jenkins Tagged With: Ansible, Jenkins

Reader Interactions

Comments

  1. Devops says

    June 7, 2021 at 2:08 pm

    HI Unixarena

    Is it possible to pass jenkins variable without parameter.

    Reply
  2. vinayak sawant says

    September 20, 2019 at 6:01 pm

    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.

    Reply
    • Lingeswaran R says

      September 27, 2019 at 11:45 pm

      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

      Reply
    • Gen Luna says

      February 18, 2021 at 10:37 pm

      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?

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Follow UnixArena

  • Facebook
  • LinkedIn
  • Twitter

Copyright © 2025 · UnixArena ·

Go to mobile version