How do you install packages portably and keep apt fast?
IntermediateConceptAd-hoc Commands, Modules and Idempotency
Answer
ansible.builtin.package picks the right backend from facts, but package names often differ by OS, so you may still need per-family variables. Use apt/dnf directly when you need their options.
- ansible.builtin.apt:
name: [nginx, curl]
state: present
update_cache: true
cache_valid_time: 3600
- ansible.builtin.dnf:
name: nginx
state: latest
cache_valid_time avoids apt update on every task. Pass name as a list, because one transaction is faster than a loop with one package per task. state: latest is not idempotent over time, so use present for repeatable builds.
More on Ad-hoc Commands, Modules and Idempotency
What is an ad-hoc command and what is its syntax?BeginnerWhat does `ansible all -m ping` actually test?BeginnerGive practical use cases for ad-hoc commands.IntermediateWhat is the difference between the `command` and `shell` modules?BeginnerCompare `raw`, `command`, `shell` and `script`.IntermediateWhy prefer a dedicated module over `shell`/`command`?Beginner