It is essential to keep your system up to date and to apply all security patches. If you are tasked with managing multiple servers, the process of logging in to each and every server to perform the task can be cumbersome. You can use Ansible to achieve the functionality. The Ansible apt module can be used to manage apt updates and apt upgrades. The module uses either aptitude or the apt-get command on the remote server for package management.
Similarly, if there are kernel updates that require OS reboot, it would be best to use the reboot ansible module to reboot the machine, wait for it to go down, come back up and to respond to commands
In this guide, we will create a script to update Debian based systems cache and upgrade packages installed. We will also include a task to restart the server if there were kernel updates that requires reboot.
Also check:
Using Ansible apt module to update all packages
Before doing a package upgrade in Debian based systems, it is always recommended to perform an apt cache refresh. This can be achieved using this command:
|
|
We can achieve the same using Ansible with this task:
|
|
Where,
update_cache: yes
- Run the equivalent ofapt-get update
command on all serversforce_apt_get: yes
- Do not use the aptitude command, instead use the apt-get command on Debian/Ubuntu boxescache_valid_time: 3600
- Update the apt cache if it’s older than thecache_valid_time
. This option is set in seconds. In this examples, it is set to 3600 seconds.
Next we do the upgrade. We normally run this apt-get command to achieve the function:
|
|
This is the Ansible task to achieve the upgrade:
|
|
Where,
upgrade: dist
- Run the equivalent ofapt-get upgrade
command on all Ubuntu or Debian Linux servers. In other words, upgrade all packages to latest version.force_apt_get: yes
- Use apt-get instead of aptitude.
Rebooting the system if there are Kernel Upgrades
If there are Kernel upgrades, we would need to reboot the system to apply those changes. If a reboot is required a file with this path /var/run/reboot-required
will be created.
What we would want to do is check if that file exist, then reboot the system. We can register a new variable if file /var/run/reboot-required exists on the system using the ansible stat:
|
|
Where:
register: reboot_required_file
- Theregister
keyword decides what variable to save a result in and we are going to use it as follows to reboot the box.stat: path: /var/run/reboot-required
- Determine if a path (/var/run/reboot-required
) existsget_md5: no
- Algorithm to determine checksum of file. In this example, I am usingmd5
, but you can usesha1
,sha224
,sha256
,sha384
, andsha512
.
Now that we know if the server is to be rebooted or not, let us add a task to reboot the server if the file exist:
|
|
Where,
test_command: uptime
- Execute uptime command on the rebooted server and expect success from to determine the machine is ready for further tasks.when: reboot_required_file.stat.exists
- First, check that the file named/var/run/reboot-required
exists using a variable namedreboot_required_file
. The reboot module will only work if that file exists and it is enforced usingwhen: reboot_required_file.stat.exists
ansible condition.
Creating the hosts file
Now that we have the logic in place, let us create a hosts.yaml
file. Save this content to a file called hosts.yaml
.
|
|
The whole playbook
We can put the whole logic in a playbook. Save this content to the file upgrade.yaml
|
|
Running the playbook
Make sure you set up ssh keys and run it as follows:
|
|
This is the output on my server
|
|
Wrapping up
You learned how to update all packages on your Debian and Ubuntu Linux boxes and reboot the server if required using Ansible playbooks.