<p>Q: <em><strong>&quot;Is it possible to share variables across instances?&quot;</strong></em></p>
<p>A: Yes. The dictionary <em>hostvars</em> keeps the variables. <em>hostvars</em> 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. For example,</p>
<pre class="lang-yaml prettyprint-override"><code>{{ hostvars['test.example.com']['ansible_facts']['distribution'] }}
</code></pre>
<p>See:</p>
<ul>
<li><p><a href="https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html#information-about-ansible-magic-variables" rel="nofollow noreferrer">Information about Ansible: magic variables</a></p>
</li>
<li><p><a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#scoping-variables" rel="nofollow noreferrer">Scoping variables</a></p>
</li>
<li><p><a href="https://docs.ansible.com/ansible/latest/plugins/cache.html#cache-plugins" rel="nofollow noreferrer">Cache plugins</a></p>
</li>
</ul>
<hr>
<p>With “Fact Caching” disabled, to share information among Ansible playbooks, it's possible to store all <em>hostvars</em> in a file. For example, given the inventory</p>
<pre class="lang-ini prettyprint-override"><code>shell&gt; cat hosts
[test]
test_01
test_02
test_03
</code></pre>
<p>this template</p>
<pre class="lang-yaml prettyprint-override"><code>shell&gt; 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 %}
</code></pre>
<p>and the playbook below</p>
<pre class="lang-yaml prettyprint-override"><code>shell&gt; cat pb1.yml 
- hosts: test

  tasks:

    - set_fact:
        test_var: &quot;test_var_in_{{ inventory_hostname }}&quot;

    - template:
        src: my_hostvars.json.j2
        dest: &quot;{{ playbook_dir }}/my_hostvars.json&quot;
      delegate_to: localhost
      run_once: true
</code></pre>
<p>store <em>hostvars</em> of all hosts into the dictionary
<em>my_hostvars_all</em> and put it into the file</p>
<pre class="lang-yaml prettyprint-override"><code>{{ playbook_dir }}/my_hostvars.json
</code></pre>
<p>at localhost(master). The dictionary can be included in the next playbook. For example, the playbook</p>
<pre class="lang-yaml prettyprint-override"><code>shell&gt; cat pb2.yml
- hosts: test

  tasks:

    - include_vars: my_hostvars.json

    - debug:
        msg: &quot;{{ my_hostvars_all[inventory_hostname]['test_var'] }}&quot;
</code></pre>
<p>gives (abridged)</p>
<pre class="lang-yaml prettyprint-override"><code>ok: [test_01] =&gt; 
  msg: test_var_in_test_01
ok: [test_02] =&gt; 
  msg: test_var_in_test_02
ok: [test_03] =&gt; 
  msg: test_var_in_test_03
</code></pre>

CC BY-SA 4.0
2019-09-02T03:50:38.850Z
2023-12-05T22:25:01.390Z
2023-12-05T22:25:01.390Z
Vladimir Botka
<p>I'm writing <a href="https://testinfra.readthedocs.io/en/latest/" rel="nofollow noreferrer">TestInfra</a> test scripts for mongo cluster instances using <a href="https://molecule.readthedocs.io/en/stable/index.html" rel="nofollow noreferrer">Molecule</a> <a href="https://www.ansible.com/" rel="nofollow noreferrer">Ansible</a>. I want to share variables across all the instances. I created a python class, and initialise and update the variables. Every time <code>molecule verify</code>, each test will run on each instance, so variables are independent on single instance.</p>
<p>How can I share variables across instances?</p>

<ansible><testing><python><molecule>
How to share variables across instances?
Abel
# How to share variables across instances?

(Asked by: username_0 on 2019-09-02)

I'm writing [TestInfra](https://testinfra.readthedocs.io/en/latest/) test scripts for mongo cluster instances using [Molecule](https://molecule.readthedocs.io/en/stable/index.html) [Ansible](https://www.ansible.com/). I want to share variables across all the instances. I created a python class, and initialise and update the variables. Every time `molecule verify`, each test will run on each instance, so variables are independent on single instance.

How can I share variables across instances?

**Tags:** ansible, testing, python, molecule


---

## 1 answers

### Answer by username_1 on 2019-09-02. Score: 3

Q: "Is it possible to share variables across instances?"
A: Yes. The dictionary hostvars keeps the 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. For example,
{{ hostvars['test.example.com']['ansible_facts']['distribution'] }}

See:

Information about Ansible: magic variables

Scoping variables

Cache plugins



With “Fact Caching” disabled, to share information among Ansible playbooks, it's possible to store all hostvars in a file. For example, given the inventory
shell> cat hosts
[test]
test_01
test_02
test_03

this template
shell> 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 %}

and the playbook below
shell> cat pb1.yml 
- hosts: test

  tasks:

    - set_fact:
        test_var: "test_var_in_{{ inventory_hostname }}"

    - template:
        src: my_hostvars.json.j2
        dest: "{{ playbook_dir }}/my_hostvars.json"
      delegate_to: localhost
      run_once: true

store hostvars of all hosts into the dictionary
my_hostvars_all and put it into the file
{{ playbook_dir }}/my_hostvars.json

at localhost(master). The dictionary can be included in the next playbook. For example, the playbook
shell> cat pb2.yml
- hosts: test

  tasks:

    - include_vars: my_hostvars.json

    - debug:
        msg: "{{ my_hostvars_all[inventory_hostname]['test_var'] }}"

gives (abridged)
ok: [test_01] => 
  msg: test_var_in_test_01
ok: [test_02] => 
  msg: test_var_in_test_02
ok: [test_03] => 
  msg: test_var_in_test_03


---

