My approach is totally different than what Michael proposes
In this case, I would have a per group 'nameservers' list variable in
your inventory. Use a for loop in the jinja template like:
{% for server in nameservers %}
nameserver {{ server }}
{% endfor %}
There are cases where this approach is not desirable. In that case you
can have the logic in the template:
{% if 'virt-managed' in group_names %}
do one thing
{% else %}
do the other thing
{% endif %}
I generally use the first approach. I use the second approach when all
systems except a few ones need the same configuration.
I don't want too much logic in my playbooks.
Greetings,
Jeroen