95 lines
3.5 KiB
YAML
95 lines
3.5 KiB
YAML
|
|
---
|
||
|
|
- name: Patch Ubuntu servers and report to Discord
|
||
|
|
hosts: test_ubuntu_servers
|
||
|
|
become: true
|
||
|
|
gather_facts: true
|
||
|
|
|
||
|
|
vars:
|
||
|
|
# discord_webhook_url: "https://discord.com/api/webhooks/1447800027941437554/WyjT6AH3Pn_1Wgwah7AjMm47l2UVpZCMZ0DbMnpEr3vKPImHZkkmZDtRZrlhPl4MnW73
|
||
|
|
discord_webhook_id: "1507643788326338630"
|
||
|
|
discord_webhook_token: "TkbDnf6naR7KxhqLDIvGJyeesq9OTglxHrLHyy3d6_1WiyI-bbzUGDeVu1yCjZN7fCdd"
|
||
|
|
|
||
|
|
tasks:
|
||
|
|
- name: Update apt cache
|
||
|
|
ansible.builtin.apt:
|
||
|
|
update_cache: true
|
||
|
|
cache_valid_time: 3600
|
||
|
|
register: apt_cache_result
|
||
|
|
|
||
|
|
- name: Upgrade installed packages
|
||
|
|
ansible.builtin.apt:
|
||
|
|
upgrade: dist
|
||
|
|
register: apt_upgrade_result
|
||
|
|
|
||
|
|
- name: Remove unneeded packages
|
||
|
|
ansible.builtin.apt:
|
||
|
|
autoremove: true
|
||
|
|
register: apt_autoremove_result
|
||
|
|
|
||
|
|
- name: Check if reboot is required
|
||
|
|
ansible.builtin.stat:
|
||
|
|
path: /var/run/reboot-required
|
||
|
|
register: reboot_required_file
|
||
|
|
|
||
|
|
- name: Reboot server if required
|
||
|
|
ansible.builtin.reboot:
|
||
|
|
pre_reboot_delay: 60
|
||
|
|
post_reboot_delay: 60
|
||
|
|
reboot_timeout: 600
|
||
|
|
msg: "Rebooting in 60 seconds (intiated by Ansible)"
|
||
|
|
when: reboot_required_file.stat.exists
|
||
|
|
register: reboot_result
|
||
|
|
|
||
|
|
- name: Build per-host summary
|
||
|
|
ansible.builtin.set_fact:
|
||
|
|
patch_summary: |
|
||
|
|
Host: {{ inventory_hostname }}
|
||
|
|
Apt Cache Updated: {{ 'Yes' if (apt_cache_result.changed | default(false)) else 'No' }}
|
||
|
|
Packages Upgraded: {{ 'Yes' if (apt_upgrade_result.changed | default(false)) else 'No' }}
|
||
|
|
Autoremove Changed: {{ 'Yes' if (apt_autoremove_result.changed | default(false)) else 'No' }}
|
||
|
|
Reboot Required: {{ 'Yes' if (reboot_required_file.stat.exists | default(false)) else 'No' }}
|
||
|
|
Reboot Performed: {{ 'Yes' if (reboot_result.changed | default(false)) else 'No' }}
|
||
|
|
|
||
|
|
- name: Send Discord report
|
||
|
|
community.general.discord:
|
||
|
|
webhook_id: "{{ discord_webhook_id }}"
|
||
|
|
webhook_token: "{{ discord_webhook_token }}"
|
||
|
|
username: "Ansible"
|
||
|
|
avatar_url: "https://opreviews.anime-pictures.net/49f/49f1117eec83be2bd5af12772a70d2e4_bp.avif"
|
||
|
|
content: |
|
||
|
|
======================================
|
||
|
|
Test Servers Update Report
|
||
|
|
Generated: {{ lookup('pipe', 'date +%Y-%m-%dT%H:%M:%SZ') }}
|
||
|
|
======================================
|
||
|
|
|
||
|
|
Total Hosts: {{ ansible_play_hosts_all | length }}
|
||
|
|
|
||
|
|
{% for host in ansible_play_hosts_all | sort %}
|
||
|
|
{{ hostvars[host].patch_summary | default('Host: ' ~ host ~ '\n No summary generated\n') }}
|
||
|
|
|
||
|
|
{% endfor %}
|
||
|
|
======================================
|
||
|
|
Summary
|
||
|
|
======================================
|
||
|
|
Hosts Requiring Reboot: {{
|
||
|
|
ansible_play_hosts_all
|
||
|
|
| select('in', ansible_play_hosts_all)
|
||
|
|
| map('extract', hostvars)
|
||
|
|
| selectattr('reboot_required_file.stat.exists', 'defined')
|
||
|
|
| selectattr('reboot_required_file.stat.exists')
|
||
|
|
| list
|
||
|
|
| length
|
||
|
|
}}
|
||
|
|
|
||
|
|
Hosts Rebooted: {{
|
||
|
|
ansible_play_hosts_all
|
||
|
|
| select('in', ansible_play_hosts_all)
|
||
|
|
| map('extract', hostvars)
|
||
|
|
| selectattr('reboot_result.changed', 'defined')
|
||
|
|
| selectattr('reboot_result.changed')
|
||
|
|
| list
|
||
|
|
| length
|
||
|
|
}}
|
||
|
|
delegate_to: localhost
|
||
|
|
run_once: true
|
||
|
|
become: false
|