Create the name of the file according to the name of the group. For example,
my_file: "{{ group_names.0.split('_')|first }}.sh"
This will work only if the first group of a host is either master_*
or backup_*
.
Example of a complete playbook for testing
- hosts: all
gather_facts: false
vars:
my_file: "{{ group_names.0.split('_')|first }}.sh"
tasks:
- debug:
msg: "Copy {{ my_file }} to {{ inventory_hostname }}"
gives (abridged)
msg: Copy master.sh to 192.168.10.10
msg: Copy backup.sh to 192.168.10.11
msg: Copy backup.sh to 192.168.10.12
msg: Copy backup.sh to 192.168.10.13