Passing a variable to ansible tower api from jenkins

Below is my call ansible shell script shared by my colleague. I am trying to pass a user input variable feom jenkins pipeline to ansible template but unable to do so.
print frequency param of debugging

frequency = “$1”
echo “Freq : $frequency”

#pass template id and ansible url for use

templateId = 122807
ansibleurl = “https://stage-ansible-tower

#make api call to launch job template and capture response id
echo “00000000000”

response_id = (curl -f -s -k -H 'Content-Type: application/json' -d "{}" --user {uname}:{passwd} -X POST https://stage-ansible-tower/api/v2/job_templates/{templateId}/launch/)

| python -c “import json,sys;obj=json.load(sys.stdin);print obj[‘job’];”)

#print response id for debugging
echo $response_id

#print response id to a file
echo $response_id > ansible_deployment_id.txt
echo “qqqqqqqqqqq”

#get initial status of the job

status1 = (curl -f -s -k -H 'Content-Type: application/xml' --user {uname}:${passwd} -X GET https://stage-ansible-tower/api/v2/jobs/$response_id/ | python -c “import json,sys;obj=json.load(sys.stdin);print obj[‘status’];”)

#print initial status for debugging
echo $status1

#wait until job status is either successful or failed

until [ “$status1” == “successful” ] || [ “$status1” == “failed” ]
echo "current status is status1" #sleep (( NEXT_WAIT_TIME++ ))
sleep 5
status1=$(curl -f -s -k -H ‘Content-Type: application/xml’ --user /$response_id/ | python -c “import json,sys;obj=json.load(sys.stdin);print obj[‘status’];”)

if [ “$status1” == “successful” ]
echo “ansible job execution is passed, please check log at $ansibleurl/#/jobs/$response_id/”
echo “ansible job execution failed, please check log at $ansibleurl/#/jobs/$response_id/”
exit 1

It would help if you let us know how you call this script in your jenkins pipeline.
Also can you format the script, it is very difficult to read (You can use markdown syntax).

Any parameters to the Jenkins job are available as environment variables in shell scripts, e.g. assume you have a parameter named FREQUENCY then you can do

sh 'echo frequency=$FREQUENCY'
1 Like

Hi Markus,
Thanks. Issue sorted.
I was able to pass the variable to ansible template using prompt on launch option on the ansible job template. I had used extra vars on the curl statement -d switch but i had to select the promt on launch which was missing.