Setting the apt cache time using Ansible

This article was published 4 years ago. Due to the rapidly evolving world of technology, some concepts may no longer be applicable.

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.

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

Thoughts on this post

Leave a comment?