有没有更优雅的方式在运行时访问 Job Number?

Is there a more elegant way to access Job Number during runtime?

我正在尝试访问工作编号,以便通过电子邮件向工作塔日志发送 link。它必须在剧本中具有访问权限并且在运行时可用。这已被证明是非常困难的,因为 ansible tower 本身没有这个功能。这是调用我的剧本的脚本的一部分:

#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 包含剧本使用的参数集。作业编号只能在调用真实作业后才能访问。第二个作业使用 JobURL 创建一个文本文件。真正的工作将访问该文件以读取它的工作 URL 然后删除它。

这显然不理想,会产生很多不可见的依赖关系,并且很难构建。有没有更好的办法? Ansible-Tower 中是否有我可能看过的原生功能?

事实证明,您可以在剧本中的任何位置调用 {{ tower_job_id }},它会起作用!