# Is it possible build a file in local by using template module?

**URL:** <https://forum.ansible.com/t/is-it-possible-build-a-file-in-local-by-using-template-module/14859>\
**Category:** Ansible Project\
**Created:** [March 26, 2014, 2:35am UTC](https://forum.ansible.com/t/is-it-possible-build-a-file-in-local-by-using-template-module/14859 "2014-03-26T02:35:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![qiu\_jiawei](https://avatars.discourse-cdn.com/v4/letter/q/ad7895/32.png) [@qiu\_jiawei](https://forum.ansible.com/u/qiu_jiawei)\
**Post date:** [March 26, 2014, 2:35am UTC](https://forum.ansible.com/t/is-it-possible-build-a-file-in-local-by-using-template-module/14859/1 "2014-03-26T02:35:18Z")

</div>

Now,the template module woule build the template file and send to the remote host.  
Is it possible for not send to the remote host?

I just want to see the file in local machine.

---

<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:** [March 26, 2014, 11:47pm UTC](https://forum.ansible.com/t/is-it-possible-build-a-file-in-local-by-using-template-module/14859/2 "2014-03-26T23:47:54Z")

</div>

Yep. The basics are that you could do:

- local\_action: template src=src.j2 dest=/path/to/local/system

However, this is a bit of a trap, if you are iterating over 500 hosts in a group, this will run 500 times, and clobber the file 500 times, possibly from multiple processes at once.

So this is better:

- hosts: webservers  
tasks:

- blah

- blah

- hosts: localhost  
tasks:

- template:

- hosts: webservers  
tasks:

- more

- more

Etc.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [March 27, 2014, 12:21am UTC](https://forum.ansible.com/t/is-it-possible-build-a-file-in-local-by-using-template-module/14859/3 "2014-03-27T00:21:06Z")

</div>

- local\_action: template src=src.j2  
dest=/path/to/local/system/{{inventory\_hostname}}\_templateresult

---

<div class="post-metadata">

**Author:** ![Brice\_Burgess](https://avatars.discourse-cdn.com/v4/letter/b/d2c977/32.png) [@Brice\_Burgess](https://forum.ansible.com/u/Brice_Burgess)\
**Post date:** [March 27, 2014, 2:58am UTC](https://forum.ansible.com/t/is-it-possible-build-a-file-in-local-by-using-template-module/14859/4 "2014-03-27T02:58:55Z")

</div>

Absolutely. I use this technique to help combat the deprecation of include + with\_items. Something like:

```auto
- name: normalize apache_sites_list into variable file
local_action: template dest={{ apache_sites_var_file }} src="apache_sites_normalized.j2"

- name: load normalized `apache_sites_normalized` from variable file
include_vars: "{{ apache_sites_var_file }}"

- name: remove variable file
local_action: file path={{ apache_sites_var_file }} state=absent

```

where apache\_sites\_var\_file is defined as:

apache\_sites\_var\_file: /tmp/ansible.apache\_sites.{{ inventory\_hostname }}.yml

NOTE that it uses {{ inventory\_hostname }} to help prevent issues with parallel execution.

May be an “anti pattern” in this establishment; but helped me write cleaner playbooks.

---

<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:** [March 27, 2014, 6:01pm UTC](https://forum.ansible.com/t/is-it-possible-build-a-file-in-local-by-using-template-module/14859/5 "2014-03-27T18:01:53Z")

</div>

I’m not even sure I’d call it a pattern 🙂

Writing variables to a template should be totally unneccessary if you’re just going to load it in that way, because of the way lazy-loading in Ansible already works.

If you want dog to contain the value of cat, you can just predeclare dog:

dog: “{{ cat }}”

You don’t have to write a variable file to load it.
