Skip to content

Instantly share code, notes, and snippets.

@kiview
Last active November 3, 2023 12:52
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save kiview/eb82c422d341b168f12cbe0776fef41b to your computer and use it in GitHub Desktop.
Save kiview/eb82c422d341b168f12cbe0776fef41b to your computer and use it in GitHub Desktop.
Ubuntu upgrade with Ansible
---
- hosts:
- all
become: true
tasks:
- name: Update apt cache
apt: update_cache=yes
- name: Upgrade packages
apt: upgrade=dist
- name: Check if a reboot is required
register: reboot_required_file
stat: path=/var/run/reboot-required get_md5=no
- name: restart machine
become: yes
shell: sleep 2 && shutdown -r now "Ansible updates triggered"
async: 1
poll: 0
ignore_errors: true
when: reboot_required_file.stat.exists == true
- name: Waiting for server to come back
become: no
local_action: wait_for
port=22
host={{ inventory_hostname }}
search_regex=OpenSSH
delay=10
@fferreira-lucid
Copy link

Thanks for this gist. it helped me a lot.
I had to tweak a little bit as I prefer using Ansible instructions instead of shell, command or raw

- name: update apt cache
  apt:
    update_cache: yes

- name: upgrade packages
  apt:
    upgrade: dist

- name: check if reboot is required
  register: reboot_required_file
  stat:
    path: /var/run/reboot-required get_md5=no

- name: restart machine
  reboot:
    msg: "Reboot initiated by Ansible"
  when: reboot_required_file.stat.exists == true

- name: waiting for machine to come back
  wait_for_connection:
    delay: 10
    connect_timeout: 300
  when: reboot_required_file.stat.exists == true

@kiview
Copy link
Author

kiview commented Mar 4, 2020

Wow cool, I totally forgot about this Gist and I am happy that it was to help for someone 🙂
And great improvement with using the reboot taks (not sure if it existed back then when I wrote this).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment