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

Where do lookups run, and what plugin types exist in Ansible?

AdvancedConceptAd-hoc Commands, Modules and Idempotency
Answer

Lookup plugins always execute on the control node, and lookup('file', 'x') reads a file from the controller, not the target. This surprises people who expect it to read from the remote host. For remote data use slurp, command or stat.

Plugin types: lookup, filter, test, callback, connection, inventory, strategy, vars, cache, become, action, shell. A lookup is evaluated when the template is rendered, which may be many times if a variable is used repeatedly.

db_password: "{{ lookup('ansible.builtin.env', 'DB_PASSWORD') }}"
motd: "{{ lookup('ansible.builtin.file', 'files/motd.txt') }}"

Use query() (or wantlist=True) when you need a list back.

All Ansible questions Roadmap: Ansible

More on Ad-hoc Commands, Modules and Idempotency