Script - Updating a Linux Server
To update a server (or server group) add the following to a YML/YAML script file.
---
- hosts: linuxservers proxmoxservers
remote_user: ansible
become: yes
tasks:
- name: 'update'
apt:
update_cache: yes
- name: 'Install aptitude'
apt:
name: aptitude
update_cache: yes
# register: OUT
# - name: Debug OUT
# debug: msg={{ OUT.stdout_lines}}
- name: 'upgrade'
apt:
name: "*"
state: latest
# register: OUT
# - name: Debug OUT
# debug: msg={{ OUT.stdout_lines}}
- name: 'dist-upgrade'
apt:
upgrade: dist
# register: OUT
# - name: Debug OUT
# debug: msg={{ OUT.stdout_lines}}
- name: 'autoremove'
apt:
autoremove: yes
# register: OUT
# - name: Debug OUT
# debug: msg={{ OUT.stdout_lines}}
- name: 'autoclean'
apt:
autoclean: yes
upgrade: dist
# register: OUT
# - name: Debug OUT
# debug: msg={{ OUT.stdout_lines}}
- name: 'check if reboot needed'
stat: path=/var/run/reboot-required
register: reboot_required_file
- name: Rebooting machine
shell: sleep 2 && shutdown -r now "Ansible updates triggered"
async: 1
poll: 0
ignore_errors: true
when: reboot_required_file.stat.exists
- name: Waiting for the machine to come back
local_action: wait_for host={{ ansible_host }} state=started port=22 delay=30
become: no
when: reboot_required_file.stat.exists
And execute it on the console
$ ansible-playbook act.yml -K
That’s it!