问题描述
我正在尝试从 Ansible AWX 中的 Python 脚本启动工作流模板,我的脚本如下所示
function makeKey(obj,props) {
const result = {};
for (p of props) {
result[p] = obj[p];
}
return JSON.stringify(result);
}
function groupBy(arr,fields) {
const intermediate = {};
for (let a of arr) {
const key = makeKey(a,fields);
const val = intermediate[key];
if (val !== undefined) val.push(a);
else intermediate[key] = [a];
}
return Object.values(intermediate);
}
let data = [
{
"estimated_cost": 1.14,"inventory_type": "power","cost_center_name": "Ex Hall",},{
"estimated_cost": 1.19,{
"estimated_cost": 1.08,"inventory_type": "fuel",{
"estimated_cost": 1.17,{
"estimated_cost": 1.03,{
"estimated_cost": 1.20,"cost_center_name": "Mac","inventory_type": "water",{
"estimated_cost": 1.14,{
"estimated_cost": 1.18,}
];
function sumCosts(group) {
return {
inventory_type: group[0].inventory_type,cost_center_name: group[0].cost_center_name,estimated_cost: group.map(g=>g.estimated_cost).reduce((accum,n) => accum + n,0)
}
}
console.log(groupBy(data,['inventory_type','cost_center_name']).map(sumCosts));
这很好地启动了工作流程,但它不包括来自 net_vars 变量的额外变量。我的 workflow_job_template 为来自 AWX 前端的 EXTRA VARIABLES 字段启用了“启动时提示”。
是否可以使用额外变量启动工作流?
解决方法
如果你想通过 REST API 使用额外的变量,myvars 需要有 extra_vars 键。
例如,这里就像。
#!/usr/bin/env python
import requests
def main():
url = "http://awx_server/api/v2/job_templates/14/launch/"
username = "admin"
passwd = "secret"
headers = {
"Content-Type": "application/json",}
data = {}
data['extra_vars'] = {
"extra_string": "test message"
}
req = requests.post(url,headers=headers,auth=(username,passwd),json=data,verify=False)
print(req.status_code)
print(req.text)
if __name__ == "__main__":
main()
在上面的示例中,将 test message
设置为 extra_string
变量。
extra_string 将通过 REST API 作为额外变量提供。