How does ansible.builtin.include_tasks process its task file?
It doesn't read the file until the play is running and execution reaches the include task. Then it reads the file and runs its tasks right there.
- name: Install web server
hosts: webservers
tasks:
- name: Include webserver tasks
ansible.builtin.include_tasks: webserver_tasks.yml
During parsing, the include is just one ordinary task. When it is reached at run time, Ansible evaluates it for each host: its when, its loop, its variables. Only if it runs does Ansible load webserver_tasks.yml and add those tasks to the run for that host.
Because the file is read so late, the include can react to everything that happened before it: registered results, facts, variables set by earlier tasks. The price is that Ansible can't see the included tasks in advance, which matters for listing tasks, starting at a task and notifying handlers.
Go deeper:
ansible.builtin.include_tasks module (official docs) — the module reference, including the file and apply options.
Includes: dynamic reuse (official docs) — why included tasks can react to the results of earlier tasks.