Template - load default file if specific not exists.

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

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 }}”
  • .

Hi,

Great - this is what I was looking for. Read about this ‘with_first_found’ but totally forgot.

Best regards,
Marcin Praczko

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’ :frowning:

Best regards,
Marcin Praczko

​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​

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

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

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