# Appending Hostname to a File

**URL:** <https://forum.ansible.com/t/appending-hostname-to-a-file/27737>\
**Category:** Ansible Project\
**Created:** [June 7, 2018, 2:56pm UTC](https://forum.ansible.com/t/appending-hostname-to-a-file/27737 "2018-06-07T14:56:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcalfa1](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@marcalfa1](https://forum.ansible.com/u/marcalfa1)\
**Post date:** [June 7, 2018, 2:56pm UTC](https://forum.ansible.com/t/appending-hostname-to-a-file/27737/1 "2018-06-07T14:56:04Z")

</div>

I have playbook that search for file in specific folder then fetch the files to my ansible host. During the fetch process, is there a way to append the hostname of the server the file came from for each file that’s being save to my ansible host? Below is the playbook i’m currently using to to find and fetch files.

Thanks in advance.

- hosts: all  
gather\_facts: yes  
tasks:

- win\_shell: dir /b C:\Temp  
args:  
executable: cmd  
register: results

- debug:  
var: results.stdout\_lines

- name: get the files  
fetch:  
src: “C:/Temp/{{ item }}”  
dest: /tmp/files/  
flat: yes  
with\_items: “{{ results.stdout\_lines }}”

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 7, 2018, 3:29pm UTC](https://forum.ansible.com/t/appending-hostname-to-a-file/27737/2 "2018-06-07T15:29:41Z")

</div>

You have the variable inventory\_hostname  
So you could set your dest to /tmp/files/{{ item}}\_{{ inventory\_hostname }}

---

<div class="post-metadata">

**Author:** ![marcalfa1](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@marcalfa1](https://forum.ansible.com/u/marcalfa1)\
**Post date:** [June 7, 2018, 8:32pm UTC](https://forum.ansible.com/t/appending-hostname-to-a-file/27737/3 "2018-06-07T20:32:43Z")

</div>

Thank you! Your suggestion worked 🙂
