diff --git a/wake-and-update-ubuntu.yml b/wake-and-update-ubuntu.yml new file mode 100644 index 0000000..2ca1d01 --- /dev/null +++ b/wake-and-update-ubuntu.yml @@ -0,0 +1,39 @@ +--- + - name: Update and Shutdown Ubuntu Server + hosts: ubuntu_server + become: yes + gather_facts: no + + tasks: + - name: Wake up the server using Wake-on-LAN + local_action: + module: wakeonlan + mac: "MAC_ADDRESS_HERE" + broadcast: "BROADCAST_ADDRESS_HERE" + delegate_to: localhost + + - name: Wait for the server to come online + wait_for: + host: "{{ inventory_hostname }}" + port: 22 + state: started + timeout: 300 + + - name: Update all packages to the latest version + apt: + update_cache: yes + upgrade: dist + autoremove: yes + autoclean: yes + + - name: Reboot the server + reboot: + + - name: Check if updates were successful + command: apt list --upgradable + register: upgradable_packages + changed_when: false + failed_when: upgradable_packages.stdout != "" + + - name: Shut down the server + command: shutdown now