Skip to content

Instantly share code, notes, and snippets.

@stylersnico
Created November 3, 2024 09:15
Show Gist options
  • Select an option

  • Save stylersnico/4fe50887ce9ed120a98b834443823f09 to your computer and use it in GitHub Desktop.

Select an option

Save stylersnico/4fe50887ce9ed120a98b834443823f09 to your computer and use it in GitHub Desktop.
Update and reboot Debian host if needed with ansible and needrestart
- hosts: debian
become: yes
become_method: sudo
tasks:
- name: updates a server
apt: update_cache=yes
- name: upgrade a server
apt: upgrade=dist dpkg_options='force-confold,force-confdef'
- name: Check if a reboot is required
shell: needrestart -k -p > /dev/null; echo $?
register: reboot
- name: Reboot the services
shell: needrestart -ra -l
when: reboot.stdout == "0"
- name: Reboot the server using reboot
command: /sbin/reboot
async: 1
poll: 0
when: reboot.stdout == "2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment