You must render an Nginx config with one `server` block per site from a list.
IntermediateScenarioVariables, Facts and Templates
Answer
Situation read → Template with a loop.
What I'd do →
{# templates/sites.conf.j2 #}
{% for s in sites %}
server {
listen {{ s.port | default(80) }};
server_name {{ s.name }};
root {{ s.root }};
}
{% endfor %}
- ansible.builtin.template:
src: sites.conf.j2
dest: /etc/nginx/conf.d/sites.conf
validate: nginx -t -c /etc/nginx/nginx.conf
Why → Data in YAML, structure in the template; adding a site is a one-line variable change.
Interview tip → Use {%- -%} whitespace control or trim_blocks to keep output tidy.
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.BeginnerA 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.IntermediateA fact you need is not collected by default, such as a custom application version.Intermediate