Interview Prep/Ansible/Ad-hoc Commands, Modules and Idempotency

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.

All Ansible questions Roadmap: Ansible

More on Ad-hoc Commands, Modules and Idempotency