我正在寻找一种方法如何将 Ansible Tower / AWX 的通知集成到 Rocket.Chat?我找不到适合 Rocket.Chat 集成的脚本。
2 回答
首先在管理 > 集成中进入 Rocket.Chat,然后创建一个新的传入 webhook。根据需要配置它(名称、机器人、频道等)启用脚本并添加以下脚本:
class Script {
process_incoming_request({ request }) {
// UNCOMMENT THE BELOW LINE TO DEBUG IF NEEDED.
// console.log(request.content);
let body = request.content.body;
if (!body) {
let id = request.content.id;
let name = request.content.name;
let url = request.content.url;
let status = request.content.status;
let type = request.content.friendly_name;
let project = request.content.project;
let playbook = request.content.playbook;
let hosts = request.content.hosts;
let created_by = request.content.created_by;
let started = request.content.started;
let finished = request.content.finished;
let traceback = request.content.traceback;
let inventory = request.content.inventory;
let credential = request.content.credential;
let limit = request.content.limit;
let extra_vars = request.content.extra_vars;
let message = "";
message += "AWX "+type+" "+name+" ("+id+") ";
message += "on project _"+project+"_ ";
message += "running playbook _"+playbook+"_ ";
message += "has status *"+status+"*.";
message += "\n";
message += type+" was created by _"+created_by+"_ for inventory _"+inventory+"_ ";
if (limit !== "") {
message += "with limit _"+limit+"_ ";
}
message += " and using the _"+credential+"_ credentials.\n";
if (Object.keys(hosts).length != 0) {
message += "Hosts: "+Object.keys(hosts).length+" (ok/changed/skipped/failures)\n";
for (let [name, host] of Object.entries(hosts)) {
message += "- "+name+" ("+host.ok+"/"+host.changed+"/"+host.skipped+"/"+host.failures+")";
if (host.failed === false) {
message += " is *ok*\n";
} else {
message += " has *failed*\n";
}
}
}
return {
content: {
"text": "AWX notification *"+status+"* on "+type+" "+name+" ("+id+")",
"attachments": [
{
"title": type+": "+name+"",
"title_link": url,
"text": message,
"color": "#764FA5"
}
]
}
};
} else {
return {
content: {
text: "AWX notification: " + request.content.body
}
};
}
}
}
保存并激活 webhook。现在您从 Rocket.Chat 获得一个 Webhook URL。复制该网址。
转到您的 AWX 实例并创建一个 Webhook 类型的新通知,然后从 Rocket.Chat 粘贴 Webhook URL。您可以在 AWX 中测试通知。
该脚本不会打印额外的变量,因为它们可能包含密码等。但是您会看到失败的主机和有关作业的更多信息。
AWX/Tower 无需任何自定义脚本即可向 Rocket.chat 发送通知。
在 Tower 中,转到 Notifications 并添加一个类型为“Rocket.Chat”的新通知,然后将 Target URL 设置为 Rocket.Chat 中空白传入 webhook 的 URL(确保它在顶部启用)。
(注意:请注意 Rocket.Chat 为您提供的用于集成的 URL,我没有在 URL 中给我一个正确端口为 3000 的 URL,因此它一开始就失败了)
以下是通知内容:
机器人 - 下午 3:13
塔通知测试 1 https://ruupansi01
机器人 - 下午 3:15
项目更新 #2“测试项目”成功:https://tower/#/jobs/project/1