Expand group_name in a loop with_items

Hello,
I need to expand the group_name for each host in the iteration because I would like to have different folders for each group.

So if the playbook is doing frontend1 I wan’t to access to frontends templates

Example:

Inventory
[frontend1]
1.2.3.4

[frontend2]
1.2.3.5

[backend1]
2.3.4.5

[backend2]
2.3.4.6

[backends:children]
backend1
backend2

[frontends:children]
frontend1
frontend2

Templates directory inside role

roles/
roles/cron/
roles/cron/backends/
roles/cron/backends/test_cron.j2

roles/cron/frontends/
roles/cron/frontends/test_cron.j2

TASK

  • template: src={{ item.src }} dest={{ item.dest }}

with_items:

  • { src: ‘templates/cron/{{ ansible_groupname }}/test_cron.j2’, dest: ‘/etc/cron.d/crawlers’ }

But it doesn’t work. I don’t know what variable should I use there…

Thanks!

Fixed with:

  • …/templates/cron/{{ group_names[1] }}/test_cron.j2 …