When does ansible refresh the variables from the host_vars directory?

1,162 views
Skip to first unread message

Johannes Kastl

unread,
Feb 7, 2017, 3:55:43 PM2/7/17
to ansible...@googlegroups.com
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
gather_facts: true
tasks:
- name: "do something on foo"
...

- hosts: localhost
gather_facts: true
tasks:
- name: "Change the line in host_vars on the host running the play"
...

- hosts: localhost
gather_facts: true
tasks:
- 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

signature.asc

Kai Stian Olstad

unread,
Feb 7, 2017, 4:02:51 PM2/7/17
to ansible...@googlegroups.com
On 07. feb. 2017 21:55, Johannes Kastl wrote:
> 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...

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

--
Kai Stian Olstad

Johannes Kastl

unread,
Feb 7, 2017, 4:19:28 PM2/7/17
to ansible...@googlegroups.com
Good Evening Kai,

On 07.02.17 22:02 Kai Stian Olstad wrote:

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

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...

Johannes


signature.asc

Johannes Kastl

unread,
Feb 8, 2017, 5:52:01 AM2/8/17
to ansible...@googlegroups.com
Hi guys,

On 07.02.17 22:19 Johannes Kastl wrote:

> 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

signature.asc

Kai Stian Olstad

unread,
Feb 8, 2017, 9:49:59 AM2/8/17
to ansible...@googlegroups.com
On 08. feb. 2017 11:51, Johannes Kastl wrote:
> 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

It might work on set_fact to.

--
Kai Stian Olstad

Johannes Kastl

unread,
Feb 8, 2017, 10:05:57 AM2/8/17
to ansible...@googlegroups.com
Hi Kai,

On 08.02.17 15:49 Kai Stian Olstad wrote:

> Have you tried setting "delegate_facts: true"?
> It might work on set_fact to.

I haven't, but I will give it a try. I was not sure if this was
applicable to a task not gathering facts, but setting them. I'll try.

Thanks for your help!

Johannes

signature.asc

Johannes Kastl

unread,
Feb 8, 2017, 10:52:20 AM2/8/17
to ansible...@googlegroups.com
On 08.02.17 16:05 Johannes Kastl wrote:
> On 08.02.17 15:49 Kai Stian Olstad wrote:
>
>> Have you tried setting "delegate_facts: true"? It might work on
>> set_fact to.
>
> I haven't, but I will give it a try. I was not sure if this was
> applicable to a task not gathering facts, but setting them. I'll
> try.

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

signature.asc
Reply all
Reply to author
Forward
0 new messages