Share variables across instances
Q: Is it possible to share variables across instances?


A: Yes. The dictionary hostvars keeps the variables. Quoting from Accessing information about other hosts with magic variables


  hostvars lets you access variables for another host, including facts that have been gathered about that host. You can access host variables at any point in a playbook. ...


{{ hostvars['test.example.com']['ansible_facts']['distribution'] }}


For details see Scoping variables and Caching facts.



With “Fact Caching” disabled, to share information among Ansible playbooks, it's possible to store all hostvars in a file. 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(master)

  - 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] =&gt; {
      "my_hostvars.test_var": "test_var_in_test_01"
  }
  ok: [test_02] =&gt; {
      "my_hostvars.test_var": "test_var_in_test_02"
  }
  ok: [test_03] =&gt; {
      "my_hostvars.test_var": "test_var_in_test_03"
  }