Interview Prep/Ansible/Variables, Facts and Templates

A cluster needs a per-host list of all peers except itself.

IntermediateScenarioVariables, Facts and Templates
Answer

Situation read → Peer lists in templates.

What I'd do →

{% for h in groups['cluster'] if h != inventory_hostname %}
peer {{ hostvars[h].ansible_host | default(h) }}
{% endfor %}

Or in YAML: {{ groups['cluster'] | difference([inventory_hostname]) }}.

Why → difference is clean and testable; the loop-with-if form is fine inside templates.

Interview tip → ansible_play_hosts is better than groups[...] when you want only hosts still alive in this run.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Templates