How to update the package cache only once per playbook run?

Every time I install a package, I want to make sure I have the latest version. That means I need to run apt-get update, or it’s equivalent, before I install the package. Currently I have an 'apt update_cache=yes" task running before every package install. That results in multiple ‘apt-get update’ calls. Is there a good way to make that only happen once per ansible-playbook --sudo /etc/ansible/webservices.yml ?

Would state=latest update the cache only once per run?

Would that work with other package management modules, like yum?

I would just put in a common role, but see also the ‘cache_valid_time’ option on the apt module.

http://ansibleworks.com/docs/modules.html#apt

Thanks, ‘cache_valid_time’ helps.

Though, sticking it in the common role is probably the better idea… :slight_smile: