You want to read a YAML file on the controller and use its contents as variables.
IntermediateScenarioVariables, Facts and Templates
Answer
Situation read → External data source.
What I'd do →
- ansible.builtin.include_vars:
file: "vars/{{ env_name }}.yml"
name: env_cfg
- ansible.builtin.debug: { var: env_cfg.api_url }
Or lookup('ansible.builtin.file', ...) | from_yaml.
Why → include_vars loads at run time and name: namespaces it to avoid precedence surprises.
Interview tip → include_vars outranks group_vars and play vars (it's a "role/include vars" level).
More on Variables, Facts and Templates
A variable you set in `group_vars` is being overridden and you do not know where. How do you find the source?IntermediateYou need different values per environment without copying playbooks.BeginnerYou must render an Nginx config with one `server` block per site from a list.IntermediateA template needs the IPs of all hosts in the `db` group, for example for a replication config.IntermediateNothing should be rendered unless a mandatory variable is supplied.BeginnerPer-host values, like `server_id` for MySQL, must be unique and stable.Intermediate