# Copy files with hostname to respective hosts

**URL:** <https://forum.ansible.com/t/copy-files-with-hostname-to-respective-hosts/27436>\
**Category:** Ansible Project\
**Created:** [May 3, 2018, 10:51am UTC](https://forum.ansible.com/t/copy-files-with-hostname-to-respective-hosts/27436 "2018-05-03T10:51:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bubul\_Medhi](https://avatars.discourse-cdn.com/v4/letter/b/90ced4/32.png) [@Bubul\_Medhi](https://forum.ansible.com/u/Bubul_Medhi)\
**Post date:** [May 3, 2018, 10:51am UTC](https://forum.ansible.com/t/copy-files-with-hostname-to-respective-hosts/27436/1 "2018-05-03T10:51:50Z")

</div>

Hi,

I have three files,  
host1.zip, host2.zip, host3.zip which are in the ansible server itself in a directory named “deploy”.  
And I have to copy the files the respective hosts.

host1.zip -----to-----\> host1  
host2.zip -----to-----\> host2  
host3.zip -----to-----\> host3

I want ansible to look into the directory “deploy”, check the files, match it with the hostnames and copy to the respective hosts.

How do I achieve the same ?

Regards,  
Uday

---

<div class="post-metadata">

**Author:** ![Andy\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@Andy\_Smith](https://forum.ansible.com/u/Andy_Smith)\
**Post date:** [May 3, 2018, 10:05pm UTC](https://forum.ansible.com/t/copy-files-with-hostname-to-respective-hosts/27436/2 "2018-05-03T22:05:00Z")

</div>

Hello,

> host1.zip -----to-----\> host1  
> host2.zip -----to-----\> host2  
> host3.zip -----to-----\> host3
> 
> I want ansible to look into the directory "deploy", check the files, match  
> it with the hostnames and copy to the respective hosts.
> 
> How do I achieve the same ?

How about a simple copy module usage with the ansible\_hostname,  
inventory\_hostname or ansible\_nodename facts?

- name: Copy correct zip file to host  
&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ ansible\_hostname }}.zip"  
&nbsp;&nbsp;&nbsp;&nbsp;dest: /srv/{{ ansible\_hostname }}.zip

[http://docs.ansible.com/ansible/latest/modules/copy\_module.html](http://docs.ansible.com/ansible/latest/modules/copy_module.html)

[http://docs.ansible.com/ansible/latest/modules/setup\_module.html](http://docs.ansible.com/ansible/latest/modules/setup_module.html)

[http://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#magic-variables-and-how-to-access-information-about-other-hosts](http://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts)

Cheers,  
Andy
