Ansible copy respective file with {{ item }}

I have a yml file for variables which goes like this.

- newHosts     
   - hostIP: 192.168.1.22
     filename: file1 
   - hostIP: 192.168.1.23   
     filename: file2   

I am using add_host: {{ item.hostIP }} with_items {{ newHosts }} to add hosts on the runtime.

I want to copy respective file to respective hosts with something like {{ item.filename }} but it copies all files to each host. How I just copy only the corresponding file to the node. How can I do that?

Sorry Im a noob. But need an urgent help.

Thanks

add_host allows you to add per host facts

- add_host: name={{ item.hostIP }} copyfile={{ item.filename }}

- copy: src={{copyfile}} ...

Thanks a lot Brian. It worked. :slight_smile: