Delegate to a group with_items or another way?

I’ve got a case where I need to run a command on a specific group of hosts when a certain file exists. The playbook is being run on two host groups (a and b). I’m trying to figure out the best way to create the logic to make this work.

Currently I’ve got this:

  • name: Run A on each group if file exists
    shell: script.sh
    when: file.stat.exists == true

delegate_to: “{{ item }}”
with_items: groups.a

What I’m not sure of is if this would work. Basically, I was script.sh to run on every host in group A when a certain file exists. Will this task work? If not, is there a correct way of doing this? One example has the delegate_to with items moved off into a handler, which is fine as long as I get the syntax right on the with_items.

Any ideas?