# When does ansible refresh the variables from the host\_vars directory?

**URL:** <https://forum.ansible.com/t/when-does-ansible-refresh-the-variables-from-the-host-vars-directory/23675>\
**Category:** Ansible Project\
**Created:** [February 7, 2017, 8:55pm UTC](https://forum.ansible.com/t/when-does-ansible-refresh-the-variables-from-the-host-vars-directory/23675 "2017-02-07T20:55:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [February 7, 2017, 8:55pm UTC](https://forum.ansible.com/t/when-does-ansible-refresh-the-variables-from-the-host-vars-directory/23675/1 "2017-02-07T20:55:24Z")

</div>

Hi all,

I have a play that adds a line to a file in the host\_vars directory on  
the host running the play. I have a very sporadic error, that the  
contents of the file are not being re-read afterwards.

I'll explain with a mock-up playbook containing three plays:

- hosts: foo  
&nbsp;&nbsp;gather\_facts: true  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: "do something on foo"  
&nbsp;&nbsp;&nbsp;&nbsp;...

- hosts: localhost  
&nbsp;&nbsp;gather\_facts: true  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: "Change the line in host\_vars on the host running the play"  
&nbsp;&nbsp;&nbsp;&nbsp;...

- hosts: localhost  
&nbsp;&nbsp;gather\_facts: true  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: "Do something that involves using this variable"

The third task does see the variable defined in the change file in the  
host\_vars, but not always. And not reproducibly.

Is there a way to trigger re-reading the host\_vars files?

I'll try using set\_fact next, but as this is only valid til the end of  
the play IIRC, I'll have to combine the plays 2 and 3 (which I only  
separeted because I hoped this would trigger the re-read...). And I am  
not sure if the syntax allows setting hostvars for another host...

Any ideas?

Johannes

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 7, 2017, 9:02pm UTC](https://forum.ansible.com/t/when-does-ansible-refresh-the-variables-from-the-host-vars-directory/23675/2 "2017-02-07T21:02:40Z")

</div>

With set\_fact the facts is available for all subsequent plays in a playbook, it only get lost after the playbook is finished.

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [February 7, 2017, 9:19pm UTC](https://forum.ansible.com/t/when-does-ansible-refresh-the-variables-from-the-host-vars-directory/23675/3 "2017-02-07T21:19:14Z")

</div>

Good Evening Kai,

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [February 8, 2017, 10:51am UTC](https://forum.ansible.com/t/when-does-ansible-refresh-the-variables-from-the-host-vars-directory/23675/4 "2017-02-08T10:51:50Z")

</div>

Hi guys,

> Yeah, I just found out, too. But now I have to see how to set a  
> variable for a host, that can not be reached at this point in time.  
> Delegate\_to with set\_fact? I'll try...

I can't get the syntax right for setting a hostvar with set\_fact.

The newly created file in the host\_vars directory is apparently not  
read during a playbook run, even if there are multiple plays inside it.

I tried to include the file with include\_vars, but can't get the  
namespace right, so the variable foobar inside the yml file is put  
into the hostvars of host xyz.

Also, set\_fact with delegate\_to does not set the variable.

The background is that I am creating a new lxc container on a machine,  
whose ssh host pubkey I want to have in my known\_hosts to avoid  
unknown-hostkey-prompt durinng an ansible run...

So my workflow is  
- creating the container  
- ssh-keyscan from the host  
- write the results into a file in host\_vars/xyz/  
- update my known\_hosts which uses a for-loop for all containers on  
the lxc host  
- connect to the container without prompt  
- and bootstrap it

Maybe I am abusing ansible right now, but at the moment this seems to  
be the easiest approach without blindly accepting ssh host keys...

Johannes

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 8, 2017, 2:49pm UTC](https://forum.ansible.com/t/when-does-ansible-refresh-the-variables-from-the-host-vars-directory/23675/5 "2017-02-08T14:49:40Z")

</div>

> Hi guys,

Hi Johannes

> Also, set\_fact with delegate\_to does not set the variable.

Have you tried setting "delegate\_facts: true"?  
[https://docs.ansible.com/ansible/playbooks\_delegation.html#delegated-facts](https://docs.ansible.com/ansible/playbooks_delegation.html#delegated-facts)

It might work on set\_fact to.

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [February 8, 2017, 3:05pm UTC](https://forum.ansible.com/t/when-does-ansible-refresh-the-variables-from-the-host-vars-directory/23675/6 "2017-02-08T15:05:38Z")

</div>

Hi Kai,

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [February 8, 2017, 3:52pm UTC](https://forum.ansible.com/t/when-does-ansible-refresh-the-variables-from-the-host-vars-directory/23675/7 "2017-02-08T15:52:00Z")

</div>

Yay! This did the trick. Thanks Kai, I would not have thought of  
trying that.

If I got some minutes I'll update the docs to include this.

Johannes
