Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible remote templates

I have templates for configuration files stored in my project's repositories. What I would like to do is use Ansible's template module to create a configuration file using that template on the remote server, after the project has been cloned from the repository.

Looking at the documentation for the template module it appears that the src attribute only supports local files.

I wanted to avoid storing the configuration template with my Ansible playbook as it makes more sense for me to keep these project specific templates within the project repository.

Is there an alternative to the template module that I could use?

like image 936
trajan Avatar asked Oct 16 '15 05:10

trajan


People also ask

How do templates work in Ansible?

Ansible templates are typically saved as . tpl files and support the use of variables, loops, and conditional expressions. Templates are commonly used to configure services based on variable values that can be set up on the playbook itself, in included variable files, or obtained via facts.

How do I use Ansible template module?

You can use Ansible facts, variables, and user-defined variables in your Jinja2 templates. On your Jinja2 template, you can print the value of a variable using the {{ variableName }} syntax. If the variable is an object, you can print individual object properties using the {{ objectVariable. propertyName }} syntax.

What is Jinja template in Ansible?

Jinja2 templates are simple template files that store variables that can change from time to time. When Playbooks are executed, these variables get replaced by actual values defined in Ansible Playbooks. This way, templating offers an efficient and flexible solution to create or alter configuration file with ease.


1 Answers

You've got two options here if your template is going to be on the remote host.

Firstly, you can use the fetch module which works as pretty much the opposite to the copy module to bring the template back after cloning the repo on the remote host.

A playbook for this might look something like:

- name : clone repo on remote hosts   git  :     repo : {{ git_repo_src }}     dest : {{ git_repo_dest }}  - name     : fetch template from single remote host   run_once : true   fetch    :     src             : {{ template_path }}/{{ template_file }}     dest            : /tmp/{{ template_file }}     flat            : yes     fail_on_missing : yes  - name     : template remote hosts   template :     src   : /tmp/{{ template_file }}     dest  : {{ templated_file_dest }}     owner : {{ templated_file_owner }}     group : {{ templated_file_group }}     mode  : {{ templated_file_mode }} 

The fetch task uses run_once to make sure that it only bothers copying the template from the first host it runs against. Assuming all these hosts in your play are the getting the same repo then this should be fine but if you needed to make sure that it copied from a very specific host then you could combine it with delegate_to.

Alternatively you could just have Ansible clone the repo locally and use it directly with something like:

- name : clone repo on remote hosts   git  :     repo : {{ git_repo_src }}     dest : {{ git_repo_dest }}  - name       : clone repo on Ansible host   hosts      : localhost   connection : local   git  :     repo : {{ git_repo_src }}     dest : {{ git_repo_local_dest }}  - name     : template remote hosts   template :     src   : {{ template_local_src }}     dest  : {{ templated_file_dest }}     owner : {{ templated_file_owner }}     group : {{ templated_file_group }}     mode  : {{ templated_file_mode }} 
like image 82
ydaetskcoR Avatar answered Sep 19 '22 11:09

ydaetskcoR