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.
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