Puppet trick: Running apt-get update only when needed

If you’re running an environment where your private Debian apt repository is constantly changing with new packages being added or upgraded, you may want to have those packages deployed via Puppet as soon as possible.

For example if you have puppet manifests to install the latest package version available:

This will trigger the apt-get-update-private-repo exec resource on every puppet run even if nothing changed in the repository. It also marks the resource as changed in the report; when you view Puppet Dashboard, you wonder why the servers have changed every 30 mins, even though the configuration of the server hasn’t physically changed.

The solution: pre-check for changes in the Packages file

A Debian repository generates an Packages file which is downloaded everytime an apt-get update is executed. See the Debian wiki for the information on how a Debian repository works.

The Packages file change on the repository server when the repository has changed, so before an apt-get update we can check whether the Packages file on the server has been modified and compare it with the local Packages file.
Here is the modified apt-get-update exec resource:

Thanks to the onlyif the apt-get-update-private-repo command will only run if the Packages file has been downloaded and it’s contents are different to the local Packages file.

onlyif must return true for the command to run, however a cmp will return true if there are no changes hence the ! is added before cmp to invert the exit value.

In terms of network usage – it’s almost exactly the same as a normal apt-get update, but with this trick system administrators can be peace and know when a puppet run has actually changed the server.

Leave a Reply

Your email address will not be published. Required fields are marked *