Nested variables and "with_items: groups"

Hi,

I used to have this:
with_items: groups[‘masters’]

I have now changed the group name to reflect the project name, which is entered into the “project_name” variable when the playbook is run.
e.g. if the project is “test”, the group will be “test-masters”

In the “with_items:” statement, I would like to be able to replace “masters” with something like {{ project_name }}-masters.

The latest attempt is:
with_items: groups[‘“ansible_” + project_name + “-masters”’]

I have been trawling this group and have tried various incantations, all to no avail so far.

For bonus points, I also have this in a jinja2 template:

{% for host in groups[‘masters’] %}

  • which will need the same treatment…

Any help appreciated!
Best regards,
David

OK, figured it out finally:

with_items: “{{ groups[project_name + ‘-masters’] }}”

{% for host in groups[project_name + ‘-masters’] %}