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

6,670 views
Skip to first unread message

qiu jiawei

unread,
Mar 25, 2014, 10:35:18 PM3/25/14
to ansible...@googlegroups.com
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.

Michael DeHaan

unread,
Mar 26, 2014, 7:47:54 PM3/26/14
to ansible...@googlegroups.com
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.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/45f12f53-8c6f-45b2-be8b-a080217af30b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Mar 26, 2014, 8:21:06 PM3/26/14
to ansible...@googlegroups.com

    - local_action: template src=src.j2 dest=/path/to/local/system/{{inventory_hostname}}_templateresult


--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

Brice Burgess

unread,
Mar 26, 2014, 10:58:55 PM3/26/14
to ansible...@googlegroups.com
Absolutely. I use this technique to help combat the deprecation of include + with_items. Something like:

```
- 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.

Michael DeHaan

unread,
Mar 27, 2014, 2:01:53 PM3/27/14
to ansible...@googlegroups.com
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.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages