Skip to content

Instantly share code, notes, and snippets.

@Lerentis
Created November 5, 2017 12:08
Show Gist options
  • Save Lerentis/b2825e69045879f720979bede9d79d6d to your computer and use it in GitHub Desktop.
Save Lerentis/b2825e69045879f720979bede9d79d6d to your computer and use it in GitHub Desktop.
Update Debian with Ansible
---
- name: Update apt keys
become: yes
command: apt-key update
- name: Update apt cache
become: yes
apt:
update_cache: yes
cache_valid_time: 3600
- name: Upgrade all packages to the latest version
become: yes
apt:
upgrade: dist
environment:
PATH: "{{ ansible_env.PATH }}:/sbin:/usr/sbin"
- name: Remove useless packages from the cache
become: yes
apt:
autoclean: yes
autoremove: yes
purge: yes
- name: install packages
become: yes
apt:
name: "{{ item }}"
state: installed
with_items:
- "{{ package_list }}"
when: install
- 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
wait_for:
port: 22
host: "{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}"
search_regex: OpenSSH
delay: 10
vars:
ansible_connection: local
when: reboot_required_file.stat.exists == true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment