Ansible apt automation

From snippet wiki
Jump to navigation Jump to search

Some script to use for apt automation. Create a host group named debian and run this on a daily basis. The optional sendxmpp step reports into an automation chat room for logging.

---
- hosts: debian
  tasks:

  - name: Update and upgrade apt packages
    become: true
    apt:
      upgrade: "yes"
      update_cache: yes
      autoremove: yes
      cache_valid_time: 86400 #One day

  - name: Send information
    shell:
      cmd: "echo did run apt on {{ ansible_facts['nodename'] | quote }} | sendxmpp -f /etc/sendxmpprc -t {{ xmpp_options }} {{ xmpp_to | quote }}"
      chdir: /var/lib/awx
    args:
      executable: /usr/bin/bash
    delegate_to: localhost
    ignore_errors: yes

  - name: check if a reboot is required
    shell: "[ -f /var/run/reboot-required ]"
    failed_when: False
    register: reboot_required
    changed_when: reboot_required.rc == 0
    notify: reboot

  handlers:
   - name: reboot
     shell: "sleep 5 && /sbin/reboot"
     async: 1
     poll: 0