Skip to content

Instantly share code, notes, and snippets.

@h0tw1r3
Last active March 27, 2022 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h0tw1r3/e577654ebf0e690d7b84c7edf4e6fcd8 to your computer and use it in GitHub Desktop.
Save h0tw1r3/e577654ebf0e690d7b84c7edf4e6fcd8 to your computer and use it in GitHub Desktop.
ansible playbook to update all apt or yum packages
---
- hosts: all
tasks:
- name: "yum: upgrade packages"
yum:
name: '*'
state: latest
update_cache: yes
update_only: yes
register: yum_update_status
when: ansible_facts.pkg_mgr == 'dnf' or ansible_facts.pkg_mgr == 'yum'
- name: "yum: remove automatically installed packages without dependencies"
yum:
autoremove: yes
when: ansible_facts.pkg_mgr == 'dnf' or ansible_facts.pkg_mgr == 'yum'
- name: "apt: upgrade installed packages"
apt:
name: '*'
state: latest
update_cache: yes
only_upgrade: yes
register: apt_update_status
when: ansible_facts.pkg_mgr == 'apt'
- name: "apt: remove automatically installed packages without dependencies"
apt:
autoremove: yes
when: ansible_facts.pkg_mgr == 'apt'
- name: reboot if packages were updated
reboot:
post_reboot_delay: 60
when: (apt_update_status.changed or yum_update_status.changed) and update_reboot
@h0tw1r3
Copy link
Author

h0tw1r3 commented Mar 27, 2022

ansible-playbook playbook-system-update.yml -e update_reboot=1

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