# Copy condition based on presence in playbook templates folder

**URL:** <https://forum.ansible.com/t/copy-condition-based-on-presence-in-playbook-templates-folder/33715>\
**Category:** Ansible Project\
**Created:** [September 1, 2020, 4:21pm UTC](https://forum.ansible.com/t/copy-condition-based-on-presence-in-playbook-templates-folder/33715 "2020-09-01T16:21:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LUCA\_CARANGELO](https://avatars.discourse-cdn.com/v4/letter/l/c5a1d2/32.png) [@LUCA\_CARANGELO](https://forum.ansible.com/u/LUCA_CARANGELO)\
**Post date:** [September 1, 2020, 4:21pm UTC](https://forum.ansible.com/t/copy-condition-based-on-presence-in-playbook-templates-folder/33715/1 "2020-09-01T16:21:06Z")

</div>

I need to copy a configuration file to hosts based on its custom presence in playbook templates folder.

As example :  
CONSIDER TO APPLY TO AN INVENTORY

My hostname is HOST001 {{ inventory\_hostname }}

if in my playbook tamplates folder there is a file named : configuration–HOST001  
then copy module with :  
src : “./templates/configuration–{{ inventory\_hostname }}”  
dst: "/tmp/

if ./templates/configuration–HOST001 does not exists  
then copy module with :

src : “./templates/configuration”  
dst: "/tmp/

Is there any way to do something like this ?  
Please let me know

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 1, 2020, 5:11pm UTC](https://forum.ansible.com/t/copy-condition-based-on-presence-in-playbook-templates-folder/33715/2 "2020-09-01T17:11:59Z")

</div>

Use "first\_found". For example  
[https://docs.ansible.com/ansible/latest/plugins/lookup/first\_found.html#first-found-return-first-file-found-from-list](https://docs.ansible.com/ansible/latest/plugins/lookup/first_found.html#first-found-return-first-file-found-from-list)

&nbsp;&nbsp;- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ lookup('first\_found', params) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;files:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "configuration--{{ inventory\_hostname }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- configuration  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paths:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ playbook\_dir }}/templates"

---

<div class="post-metadata">

**Author:** ![LUCA\_CARANGELO](https://avatars.discourse-cdn.com/v4/letter/l/c5a1d2/32.png) [@LUCA\_CARANGELO](https://forum.ansible.com/u/LUCA_CARANGELO)\
**Post date:** [September 2, 2020, 6:35am UTC](https://forum.ansible.com/t/copy-condition-based-on-presence-in-playbook-templates-folder/33715/3 "2020-09-02T06:35:54Z")

</div>

Hi Vladimir,

I’m going to test it immediately !

Thank you very much , I really appreciate it.

---

<div class="post-metadata">

**Author:** ![LUCA\_CARANGELO](https://avatars.discourse-cdn.com/v4/letter/l/c5a1d2/32.png) [@LUCA\_CARANGELO](https://forum.ansible.com/u/LUCA_CARANGELO)\
**Post date:** [September 3, 2020, 6:19pm UTC](https://forum.ansible.com/t/copy-condition-based-on-presence-in-playbook-templates-folder/33715/4 "2020-09-03T18:19:21Z")

</div>

It works perfectly !

Thanks again
