Setting the apt cache time using Ansible

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
    name: "{{ item }}"
    update_cache: yes
    cache_valid_time: 3600
    - htop
    - ngrep
    - vim

Michael is a polyglot software engineer, committed to reducing complexity in systems and making them more predictable. Working with a variety of languages and tools, he shares his technical expertise to audiences all around the world at user groups and conferences. You can follow @mheap on Twitter

Thoughts on this post

Leave a comment?

Leave a Reply