On Thu, 29 Aug 2019 10:50:11 -0700 (PDT) pk s <[email protected]> wrote:
> Thanks. It works for localhost.
>
> But let's say i need to save information from a task on all remote hosts in
> a local file (or files), how do i do it? So that in the next yml file, i
> can load the local file for all remote hosts.
It's possible to store all "hostvars". For example with this template
$ cat my_hostvars.json.j2
my_hostvars_all:
{% for my_host in ansible_play_hosts_all %}
{{ my_host }}:
{{ hostvars[my_host]|to_nice_json }}
{% endfor %}
the playbook below stores "hostvars" of all hosts in the dictionary
"my_hostvars_all" and put it into the file
"{{ inventory_dir }}/my_hostvars.json" at localhost
- hosts: test_jails
tasks:
- set_fact:
test_var: "test_var_in_{{ inventory_hostname }}"
- template:
src: my_hostvars.json.j2
dest: "{{ inventory_dir }}/my_hostvars.json"
delegate_to: localhost
run_once: true
The dictionary can be included in the next playbook. For example the playbook
below
- hosts: test_jails
tasks:
- include_vars: my_hostvars.json
- set_fact:
my_hostvars: "{{ my_hostvars_all[inventory_hostname] }}"
- debug:
var: my_hostvars.test_var
gives
ok: [test_01] => {
"my_hostvars.test_var": "test_var_in_test_01"
}
ok: [test_02] => {
"my_hostvars.test_var": "test_var_in_test_02"
}
ok: [test_03] => {
"my_hostvars.test_var": "test_var_in_test_03"
}
Cheers,
-vlado
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/20190829221253.275b4fdc%40gmail.com.
pgpfW3IgDuXZy.pgp
Description: OpenPGP digital signature
