~xenrox/ansible

d9eb1fa4289bf4417baf8962e811f0f7b1b0aab0 — Thorben Günther 1 year, 6 months ago e94a4a1
misc/run: Use ntfy for notifications
1 files changed, 17 insertions(+), 16 deletions(-)

M misc/run.py
M misc/run.py => misc/run.py +17 -16
@@ 14,25 14,26 @@ playbook = "playbooks/" + playbook_name + ".yml"
r = ansible_runner.run(private_data_dir=".", playbook=playbook)

build_url = environ["JOB_URL"]
gotify_token = environ["GOTIFY_TOKEN"]
ntfy_auth = environ["NTFY_BASIC_AUTH"]
changes_list = list(r.stats["changed"].values())
changes = 0
priority = "4"

if len(changes_list) > 0:
    changes = changes_list[0]

message = f"Playbook deployed with {changes} changes."

if changes == 0:
    sys.exit()
message = "[Playbook]({}) deployed with **{}** changes.".format(build_url, changes)

if changes > 0:
    post(
        "https://gotify.xenrox.net/message?token=" + gotify_token,
        json={
            "message": message,
            "priority": 7,
            "Title": playbook_name + " status",
            "extras": {
                "client::display": {"contentType": "text/markdown"},
            },
        },
    )
    priority = "1"

post(
    "https://ntfy.xenrox.net/infrastructure",
    headers={
        "Authorization": "Basic " + ntfy_auth,
        "X-Title": "Ansible " + playbook_name,
        "X-Priority": priority,
        "X-Click": build_url,
    },
    data=message,
)