# Template - load default file if specific not exists.

**URL:** <https://forum.ansible.com/t/template-load-default-file-if-specific-not-exists/13726>\
**Category:** Ansible Project\
**Created:** [October 21, 2013, 10:04am UTC](https://forum.ansible.com/t/template-load-default-file-if-specific-not-exists/13726 "2013-10-21T10:04:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [October 21, 2013, 10:04am UTC](https://forum.ansible.com/t/template-load-default-file-if-specific-not-exists/13726/1 "2013-10-21T10:04:18Z")

</div>

Hi,

I have some question about template module.  
Is there any way to use specific file in template which depends of environment and if file doesn’t exist (or condition is not valid) - default file will be used?

For example this is syntax for variables:  
vars\_files:

- [“vars/os\_{{ ansible\_distribution }}.yml”, “vars/os\_defaults.yml”]

Simple - load file related for OS - if this doesn’t exists load default file.

How to to that for template - for example?

- name: Setup system hosts file  
template: src=etc/hosts.j2 dest=/etc/hosts owner=root group=root mode=0644

I would like to use specific host file for some environments and use default template if wiles are not available.

My issue is that:

- On production servers application is using DNS and everything is working well - so ‘/etc/hosts’ files are very basic.
- On development we’re using domains which must be added to ‘hosts’ files.

And I would like to achieve something like that:

- If production environment - use ‘default’ hosts for template
- if development - use hosts file for environment.

Is this possible to achieve or I have to write different tasks per environment (when: evn == dev do tasks1, when evn=prod do task2)

Best regards?  
Marcin Praczko

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [October 21, 2013, 10:10am UTC](https://forum.ansible.com/t/template-load-default-file-if-specific-not-exists/13726/2 "2013-10-21T10:10:33Z")

</div>

I think the first\_found template is what you need. Example:

- name: TOMCAT | copy setenv template  
template: src={{ item }} dest={{ tomcat\_home\_dir }}/bin/setenv.sh owner=tomcat group=tomcat mode=755  
with\_first\_found:
- files:
- setenv-{{ zuil }}.sh
- setenv.sh  
paths:
- “{{ app }}”
- .

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [October 21, 2013, 10:21am UTC](https://forum.ansible.com/t/template-load-default-file-if-specific-not-exists/13726/3 "2013-10-21T10:21:54Z")

</div>

Hi,

Great - this is what I was looking for. Read about this ‘with\_first\_found’ but totally forgot.

Best regards,  
Marcin Praczko

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [October 21, 2013, 11:08am UTC](https://forum.ansible.com/t/template-load-default-file-if-specific-not-exists/13726/4 "2013-10-21T11:08:27Z")

</div>

Hi,

Could you help me what I am doing wrong please?  
Ansbile: 1.3.2

- name: Setup system hosts file  
template: src={{ item }} dest=/etc/hosts owner=root group=root mode=0644  
with\_first\_found:
- files:
- hosts-{{ env }}.j2
- hosts.j2  
paths:
- etc

Error during run task:  
TASK: Setup system hosts file] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [srv-www] =\> input file not found at /src/ansible/roles/system/templates/None or /src/ansible/None

It looks like with\_first\_found doesn’t read templates as relative path and item seems be as ‘None’ ☹

Best regards,  
Marcin Praczko

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [October 21, 2013, 11:26am UTC](https://forum.ansible.com/t/template-load-default-file-if-specific-not-exists/13726/5 "2013-10-21T11:26:00Z")

</div>

​I'm not sure where the None comes from, probably because "None" of the  
files/path combo's match.

​Templates in roles are looked for in the folder templates/ beneath the  
role dir, or relative to the calling playbok file.  
Can you confirm where you put your templates? Where that etc folder is  
located?

Serge​

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [October 21, 2013, 11:29am UTC](https://forum.ansible.com/t/template-load-default-file-if-specific-not-exists/13726/6 "2013-10-21T11:29:34Z")

</div>

Hi,

I am following best practices from ansible.  
I have roles in this format:  
role/tasks/  
role/templates/  
role/files/

etc is located for this role in role/tasks/etc/.

As I can see this code has issue to find proper relative path, when I am using full hardcoded path, everything is working well.

The same path worked without any issues on template module with this with\_first\_found - no path has changed.

Best regards,  
Marcin Praczko

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [October 21, 2013, 11:55am UTC](https://forum.ansible.com/t/template-load-default-file-if-specific-not-exists/13726/7 "2013-10-21T11:55:49Z")

</div>

> I am following best practices from ansible.  
> I have roles in this format:  
> role/tasks/  
> role/templates/  
> role/files/
> 
> etc is located for this role in role/tasks/etc/.

You're calling a template, so you should have this in role/templates/etc/​​

As I can see this code has issue to find proper relative path, when I am

> using full hardcoded path, everything is working well.

"Hardcoding" a full absolute path will indeed always work, but then you are  
bound to the same location ​​for you playbooks.

The same path worked without any issues on template module with this

> with\_first\_found - no path has changed.

I suspect you were trying this without using roles?​​ With and without  
roles have different assumptions.

Serge

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [October 21, 2013, 12:52pm UTC](https://forum.ansible.com/t/template-load-default-file-if-specific-not-exists/13726/8 "2013-10-21T12:52:52Z")

</div>

Hi Serge,

Sorry, I made mistake in post - my path for etc is:  
/src/ansible/roles/system/templates/etc

(Not tasks).

And I know that using absolute path is not recommended, but so far this is only one way which is working for me.  
Will try work on this when more time allows.

Best regards,  
Marcin Praczko
