Setting the apt cache time using Ansible

08 Dec 2015 in TIL

When I'm writing Ansible roles, I usually set update_cache to yes in the apt task to make sure that everything is up to date. This is very useful but gets painful very quickly if you start including several roles, all of which refresh the cache.

Ansible provides a cache_valid_time option for the apt module. If you enable it, Ansible will check the cache age and if the cache age is less than your value, apt-get update will not be run. In this example, I tell Ansible not to update the cache if it's been updated in the last hour.

yaml
- name: Installing useful packages
apt:
name: ""
update_cache: yes
cache_valid_time: 3600
with_items:
- htop
- ngrep
- vim