--- - name: Update packages on Ubuntu servers hosts: all gather_facts: yes become: yes tasks: - name: Perform a dist-upgrade. ansible.builtin.apt: upgrade: dist update_cache: yes - name: Generate list of updated packages shell: dpkg --list | grep "^ii" | awk '{print $2"="$3}' > /home/ansible@AD.HAGOOD.US/updates.txt - name: Copy updates list to control node fetch: src: /home/ansible@AD.HAGOOD.US/updates.txt dest: /home/ansible@AD.HAGOOD.US/updates.txt flat: yes delegate_to: preprod-group - name: Check if a reboot is required. ansible.builtin.stat: path: /var/run/reboot-required get_md5: no register: reboot_required_file - name: Reboot the server (if required). ansible.builtin.reboot: when: reboot_required_file.stat.exists == true - name: Remove dependencies that are no longer required. ansible.builtin.apt: autoremove: yes - name: Append update and reboot information to a text file lineinfile: path: "/path/to/{{ inventory_hostname }}_update_log.txt" line: | *** Starting Check for Updates *** Updates installed on {{ ansible_date_time.date }} at {{ ansible_date_time.time }} Updated Packages: {% for package in updated_packages.stdout_lines %} - {{ package }} {% endfor %} Update Result: {{ 'Successful' if package_update_result.changed else 'Failed' }} Reboot Required: {{ 'Yes' if reboot_required.changed else 'No' }} *********************************************************** Process_Complete create: yes insertafter: EOF - name: Copy update log to local machine copy: src: "/path/to/{{ inventory_hostname }}_update_log.txt" dest: "/local/path/to/{{ inventory_hostname }}_update_log.txt" # - name: Copy update log to network shared drive # copy: # src: "/path/to/{{ inventory_hostname }}_update_log.txt" # dest: "/network/shared/drive/{{ inventory_hostname }}_update_log.txt"