Content from multiple hosts to one file

Hi to all,

I need to generate some file, and inside that file is some content from multiple hosts.
I’ve done that using template module, so I generate one file on every host, delegate that to localhost, and with assemble module all that files I merge to one single file.
Is that OK, or is it the better way to do the same.
I try with blockinfile module, but it always override current content of some file, and I’m not sure is it possible to generate new file with blockinfile module.
I would like to run playbook on the group, collect some data with custom facts, and those custom facts from all hosts use to populate one single file on control machine for example.

Tnx for any help…

I think what you’re after is the “magic” groups and hostvars variables: http://docs.ansible.com/ansible/latest/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts

These let you access the information about what hosts are in a group and the facts about those hosts. Combining these with a loop using the template module should give you what you want.

For example, lets assume you wanted to produce a list of the webservers in your loadbalancer configuration. You could do something like

  • hosts: loadbalancer
    tasks:
  • template:
    src: mytemplate.j2
    dest: list-of-hosts

Then the template file would look something like:

{% for host in groups[‘webservers’] %}
{{ hostvars[host][‘ansible_hostname’] }}
{% endfor %}

What this is doing is looping over all the hosts in the webservers group, then pulling out the ansible_hostname fact for each host. You can pull out whatever fact it is you want from the other hosts.

Hope that helps
Ed