# create remote file only if local template exists

**URL:** <https://forum.ansible.com/t/create-remote-file-only-if-local-template-exists/16713>\
**Category:** Ansible Project\
**Created:** [November 6, 2014, 8:08pm UTC](https://forum.ansible.com/t/create-remote-file-only-if-local-template-exists/16713 "2014-11-06T20:08:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jordi\_Funollet](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@Jordi\_Funollet](https://forum.ansible.com/u/Jordi_Funollet)\
**Post date:** [November 6, 2014, 8:08pm UTC](https://forum.ansible.com/t/create-remote-file-only-if-local-template-exists/16713/1 "2014-11-06T20:08:21Z")

</div>

I'm trying to have a template uploaded only if the template exists  
\*locally\*. My use case is having a bunch of items with common parts  
configured on the same role. Something like

&nbsp;&nbsp;&nbsp;&nbsp;- template: src=nginx/{{ item }}.j2 dest={{ nginx\_sites\_dir }}/{{  
&nbsp;&nbsp;&nbsp;&nbsp;item }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: apps  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: file\_exists|nginx/{{ item }}.j2

&nbsp;&nbsp;&nbsp;&nbsp;- template: src=uwsgi/{{ item }}.j2 dest={{ uwsgi\_dir }}/{{ item }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: apps  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: file\_exists|uwsgi/{{ item }}.j2

So for every 'app' it would configure Nginx only if a template exists on  
'templates/nginx/' for this specific app, and likewise for uwsgi.

Is there some conditional that could be used here? I could probably live  
with 'ignore\_errors: yes' but this could mask other unintended problems.

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [November 6, 2014, 8:15pm UTC](https://forum.ansible.com/t/create-remote-file-only-if-local-template-exists/16713/2 "2014-11-06T20:15:07Z")

</div>

You can combine the stat module with local\_action and save the  
results. Then use those results in the conditional.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 6, 2014, 8:37pm UTC](https://forum.ansible.com/t/create-remote-file-only-if-local-template-exists/16713/3 "2014-11-06T20:37:33Z")

</div>

or just run with ignore\_errors: true, it will fail when the template source dooesn’t exist

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [November 7, 2014, 2:24pm UTC](https://forum.ansible.com/t/create-remote-file-only-if-local-template-exists/16713/4 "2014-11-07T14:24:45Z")

</div>

“or just run with ignore\_errors: true, it will fail when the template source dooesn’t exist”

That’s not always true with action plugins. Usually true of modules that return failed error codes.

The better way to do this would be to use the “stat” module with “local\_action”, check for existance, and stick a “when” on the template item.

---

<div class="post-metadata">

**Author:** ![Jordi\_Funollet](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@Jordi\_Funollet](https://forum.ansible.com/u/Jordi_Funollet)\
**Post date:** [November 10, 2014, 10:10am UTC](https://forum.ansible.com/t/create-remote-file-only-if-local-template-exists/16713/5 "2014-11-10T10:10:07Z")

</div>

I see how the 'stat + when' pattern would work for a single item, but  
not when there's a list of templates that must be checked. This code:

&nbsp;&nbsp;&nbsp;&nbsp;- local\_action: stat path=nginx/{{ item }}.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: nginx\_sites\_exist  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: apps

would register a var like

&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"nginx\_sites\_exist": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": "All items completed",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"results": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"invocation": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"module\_args": "path=nginx/cc-insert.j2",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"module\_name": "stat"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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