Skip to content

Commit

Permalink
Addition of Teams Webhook Support
Browse files Browse the repository at this point in the history
  • Loading branch information
modzilla99 committed Jan 12, 2022
1 parent 028e9b1 commit 1711f1e
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 33 deletions.
8 changes: 6 additions & 2 deletions group_vars/all/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ updates_categories:
# Automatically rolls back to snapshot in case of an error
posthooks_rollback: true
# Automatically removes snapshot after update installation
posthooks_autoremove: true
posthooks_autoremove: false

# Enables Mail delivery after playbook run
mail_enabled: true
mail_enabled: false
# Examples for the supported mail delivery settings: https://docs.ansible.com/ansible/latest/collections/community/general/mail_module.html
mail:
host: mail.local
Expand All @@ -80,3 +81,6 @@ mail:
password: {}
from: "[email protected] (Ansible No-Reply)"
to: User <[email protected]>

webhook_enabled: false
webhook_url: 'https://outlook.office.com/webhook/your_webhook_url'
27 changes: 12 additions & 15 deletions roles/posthooks/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,13 @@ posthooks_autoremove: false
mail_enabled: false
mail_status_error:
msg: No status available

mail_success_subject: Patching of {{ inventory_hostname }} succeded
mail_success_message: |
{{ inventory_hostname }} was successfully patched!
This is the status:
{{ win_update | default(mail_status_error) | to_nice_json }}
mail_failure_subject: Patching of {{ inventory_hostname }} failed
mail_failure_message: |
{% if posthooks_rollback == true %}
{% set mail_msg = "As a result, the VM was reverted to the snapshot." %}
mail_subject: |
{% if update_successful | default(true) == false %}
Patching of {{ inventory_hostname }} failed
{% else %}
{% set mail_msg = "The VM was not changed, but it is strongly advised to roll back to the last Snapshot" %}
Patching of {{ inventory_hostname }} succeded
{% endif %}
{{ inventory_hostname }} failed to install updates. {{ mail_msg }}
This is the status of Windows Update:
{{ win_update | default(mail_status_error) | to_nice_json }}
mail_message: "{{ lookup('template', 'mail_message.j2') }}"

mail:
host: mail.local
Expand All @@ -41,3 +31,10 @@ mail:
password: {}
from: "[email protected] (Ansible No-Reply)"
to: User <[email protected]>


webhook_enabled: false
webhook_status_error:
msg: No status available
webhook_url: 'https://outlook.office.com/webhook/your_webhook_url'
message: "{{ lookup('template', 'webhook_message.j2') }}"
27 changes: 11 additions & 16 deletions roles/posthooks/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
uuid: "{{ machine[0].uuid }}"
snapshot_name: "{{ snapshot_name }}"
state: absent
- name: Sending E-Mail (unsuccessful)
when: mail_enabled and update_successful | default(true) == false
- name: Sending E-Mail
when: mail_enabled
delegate_to: localhost
community.general.mail:
host: "{{ mail.host }}"
Expand All @@ -45,18 +45,13 @@
from: "{{ mail.from }}"
secure: "{{ mail.secure }}"
to: "{{ mail.to }}"
subject: "{{ mail_failure_subject }}"
body: "{{ mail_failure_message }}"
- name: Sending E-Mail (successful)
when: mail_enabled and update_successful | default(true)
subject: "{{ mail_subject }}"
body: "{{ mail_message }}"
- name: Log lines to MS Teams
when: webhook_enabled
delegate_to: localhost
community.general.mail:
host: "{{ mail.host }}"
port: "{{ mail.port }}"
username: "{{ mail.username }}"
password: "{{ mail.password }}"
from: "{{ mail.from }}"
secure: "{{ mail.secure }}"
to: "{{ mail.to }}"
subject: "{{ mail_success_subject }}"
body: "{{ mail_success_message }}"
uri:
url: "{{ webhook_url }}"
body: "{{ message }}"
body_format: json
method: POST
11 changes: 11 additions & 0 deletions roles/posthooks/templates/mail_message.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% if update_successful | default(true) == false %}
{% if posthooks_rollback == true %}
{{ inventory_hostname }} failed to install updates. As a result, the VM was reverted to the snapshot.
{% else %}
{{ inventory_hostname }} failed to install updates but the VM was not rolled back, please make sure all Services still work as expected and roll back if needed.
{% endif %}
{% else %}
{{ inventory_hostname }} was successfully patched! Please make sure all Services still work as expected.
{% endif %}
This is the log of the Windows Update module:
{{ win_update | default(mail_status_error) | to_nice_json }}
68 changes: 68 additions & 0 deletions roles/posthooks/templates/webhook_message.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{% if update_successful | default(true) == false %}
{% if posthooks_rollback == true %}
{% set header_msg = "was reverted to the Snapshot" %}
{% else %}
{% set header_msg = "failed to patch" %}
{% endif %}
{% else %}
{% set header_msg = "was sucessfully patched" %}
{% endif %}
{
"type": "message",
"attachments": [
{
"contentType": "application/vnd.microsoft.card.adaptive",
"contentUrl": null,
"content": {
"type": "AdaptiveCard",
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"body": [
{
"type": "TextBlock",
"weight": "Bolder",
"size": "Medium",
"style": "heading",
"wrap": true,
"text": "({{ inventory_hostname }}) System {{ header_msg }}"
},
{
"text": "{{ '%Y-%m-%d %H:%M:%S' | strftime }}",
"type": "TextBlock",
"spacing": "Small",
"size": "Small"
},
{
"text": "Ansible installed {{ win_update.installed_update_count }} Updates.",
"type": "TextBlock"
},
{
"type": "FactSet",
"facts": [
{
"title": "Updates Found",
"value": "{{ win_update.found_update_count }}"
},
{
"title": "Updates filtered",
"value": "{{ win_update.filtered_updates | to_nice_json | replace('"','\\"') }}"
},
{
"title": "Updates Installed",
"value": "{{ win_update.installed_update_count }}"
},
{
"title": "Updates Failed",
"value": "{{ win_update.failed_update_count }}"
},
{
"title": "Full Status",
"value": "{{ win_update | to_nice_json | replace('"','\\"') }}"
}
]
}
]
}
}
]
}

0 comments on commit 1711f1e

Please sign in to comment.