Ansible apt update all packages on Ubuntu / Debian Linux

Originally published at: https://www.cyberciti.biz/faq/ansible-apt-update-all-packages-on-ubuntu-debian-linux/

I have a bunch of servers powered by Ubuntu and Debian Linux. How do I update all of them using the apt module of Ansible? How can I use Ansible for system updates and reboot the box when kernel upgrades took place?

Great playbook. Anyone else getting error on the reboot check module? I’m receiving below:

    TASK [Reboot the box if kernel updated] *****************************************************************************************************
fatal: [landscape]: FAILED! => {"msg": "The conditional check 'reboot_required_file.stat.exist' failed. The error was: error while evaluating conditional (reboot_required_file.stat.exist): 'dict object' has no attribute 'exist'\n\nThe error appears to be in '/etc/ansible/update_ubuntu.yaml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Reboot the box if kernel updated\n      ^ here\n"}
fatal: [miab]: FAILED! => {"msg": "The conditional check 'reboot_required_file.stat.exist' failed. The error was: error while evaluating conditional (reboot_required_file.stat.exist): 'dict object' has no attribute 'exist'\n\nThe error appears to be in '/etc/ansible/update_ubuntu.yaml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Reboot the box if kernel updated\n      ^ here\n"}
fatal: [librenms]: FAILED! => {"msg": "The conditional check 'reboot_required_file.stat.exist' failed. The error was: error while evaluating conditional (reboot_required_file.stat.exist): 'dict object' has no attribute 'exist'\n\nThe error appears to be in '/etc/ansible/update_ubuntu.yaml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Reboot the box if kernel updated\n      ^ here\n"}
fatal: [nakivo]: FAILED! => {"msg": "The conditional check 'reboot_required_file.stat.exist' failed. The error was: error while evaluating conditional (reboot_required_file.stat.exist): 'dict object' has no attribute 'exist'\n\nThe error appears to be in '/etc/ansible/update_ubuntu.yaml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Reboot the box if kernel updated\n      ^ here\n"}

Post your ansible version:

ansible --version

You need at ansible version 2.7 or above to work with reboot module.

ansible 2.9.1
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

Seems like version is correct. Try running in dry mode (--dry-run) and use debugger https://docs.ansible.com/ansible/latest/user_guide/playbooks_debugger.html. I tested that playbook and it worked perfectly.

What is the proper syntax for the dry run command? I’ve tried below but get unrecognized command error:

ansible-playbook --dry-run update_ubuntu.yaml

Here are the instructions for the test (dry-run)

https://docs.ansible.com/ansible/latest/user_guide/playbooks_checkmode.html

So the right syntax is:

ansible-playbook update_ubuntu.yaml --check

Ok, playbook is running and failed on reboot task. I don’t know how to proceed on the debug question:

TASK [Reboot the box if kernel updated] *****************************************************************************************************
fatal: [landscape]: FAILED! => {“msg”: “The conditional check ‘reboot_required_file.stat.exist’ failed. The error was: error while evaluating conditional (reboot_required_file.stat.exist): ‘dict object’ has no attribute ‘exist’\n\nThe error appears to be in ‘/etc/ansible/update_ubuntu.yaml’: line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Reboot the box if kernel updated\n ^ here\n”}
[landscape] TASK: Reboot the box if kernel updated (debug)>

Can you post the whole update_ubuntu.yaml here please.

- hosts: ubuntu_servers

become: true
become_user: root
tasks:
- name: Update apt repo and cache on all Debian/Ubuntu boxes
apt: update_cache=yes force_apt_get=yes cache_valid_time=3600

- name: Upgrade all packages on servers
  apt: upgrade=dist force_apt_get=yes

- name: Check if a reboot is needed on all servers
  register: reboot_required_file
  stat: path=/var/run/reboot-required get_md5=no

- name: Reboot the box if kernel updated
  reboot:
    msg: "Reboot initiated by Ansible for kernel updates"
    connect_timeout: 5
    reboot_timeout: 300
    pre_reboot_delay: 0
    post_reboot_delay: 30
    test_command: uptime
  when: reboot_required_file.stat.exist

Can you please try the following.

- name: Reboot the box if kernel updated
  reboot:
    msg: "Reboot initiated by Ansible for kernel updates"
    connect_timeout: 5
    reboot_timeout: 300
    pre_reboot_delay: 0
    post_reboot_delay: 30
    test_command: uptime
  when: reboot_required_file.stat.exist == true

When this is not working you can try.

  - name: Reboot the server
    command: /sbin/reboot
    when: reboot_required_file.stat.exists == true

My apologies, just getting back to this. Looks like that did the trick. I did get error on first code, but it seems to have rebooted some of the servers anyway. I put in the second code in place of and didn’t get any errors, but none of the servers needed a reboot, so I’ll have to wait a bit until there are some new updates to confirm.

Thanks!


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki