Interview Prep/Ansible/Variables, Facts and Templates

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.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Templates