I am trying to access the job number in order to send a link to the jobs tower logs via email. It must have access within the playbook and be available at runtime. This has proven to be extremely difficult as ansible tower does not have this function natively. Here is a portion of the script that calls my playbook:
#Stores the output of the job
tmp = "/tmp/runTowerJob"+str(rand(0,1))
#Stores the input for the job
extravars = " ".join([x+'='+inputDic[x] for x in inputDic])
cmd = 'tower-cli job launch --job-template=' + migration_template + ' --inventory=' + migration_inventory
#Launch first job
system(cmd + ' --extra-vars "' + extravars + '" > ' + tmp + ' 2> /dev/null')
jobNum = open(tmp).read().split('\n')[-3].split(' ')[0]
jobURL = 'https://tower.companyname.ca/#/jobs/playbook/' + jobNum
#Append the real job URL to the parameter file
extravars += ' jobURL=' + jobURL
cmd = 'tower-cli job launch --job-template=' + recordJob_template + ' --inventory=' + recordJob_inventory
cmd = cmd + ' --extra-vars="' + extravars + '" > /dev/null 2> /dev/null'
#Launch second job
system(cmd)
Extravars contains the set of parameters used by the playbook. Job number can only be accessed after the real job is called. The second job creates a text file with JobURL. The real job will access that file to read it's job URL and then delete it.
This is obviously not ideal, creates a lot of invisible dependencies and is difficult to build upon. Is there a better way? Is there native functionality in Ansible-Tower that I may have looked over?
It turns out you can call {{ tower_job_id }} at any point in the playbook and it will work!