Variable Import Question

Hey Folks,

So in a simple test playbook I am playing with that only plays against 1 host, I do 2 vars_files imports. When I run the playbook (from the devel branch pulled a day ago), I get an unexpected behavior that seems odd to me. Based on the output, it would appear that regardless of how many hosts are declared in the playbook, it does a variable import for all hosts found in the inventory file. Am I correct in my assumption? It seems wasteful on a few levels, as it adds to the runtime of the playbook as it iterates over all the hosts (basically unnecessary work for hosts not used in the playbook) and then as it outputs it adds to console spam that is unnecessary and cluttered, imo. This would result in variable import routines and corresponding console output for X (# of imported variable files) times N hosts found in the inventory file, which could grow to be quite large depending on the size of your inventory. I run a YAML inventory file tho I would presume this behavior would happen for however the inventory file is declared (flat, YAML, or script pull in), but I haven’t tested the other two cases yet. Thoughts?

Here’s an example of what I am seeing…

VARIABLE IMPORT PHASE ******************

hosta01: importing site/vars/my_vars.yml
hosta01: importing site/release.yml
hosta02: importing site/vars/my_vars.yml
hosta02: importing site/release.yml
hostd01: importing site/vars/my_vars.yml
hostd01: importing site/release.yml
hostd02: importing site/vars/my_vars.yml
hostd02: importing site/release.yml
hostp01: importing site/vars/my_vars.yml
hostp01: importing site/release.yml
hostp02: importing site/vars/my_vars.yml
hostp02: importing site/release.yml
hostk01: importing site/vars/my_vars.yml
hostk01: importing site/release.yml
ok: [hostk01]

hostk01 is the only host in the playbook, yet it runs through for all hosts in my inventory. Cheers!

Dave

Hi Dave,
I think this is due to the fact that hosts can access variables from
other hosts. See "Variables From Other Hosts" at
http://ansible.github.com/playbooks.html.

matt

I don't think this is the case, as it doesn't happen when I don't declare
any variable import files. The variables from other hosts I believe is an
explicit call to a specific host and specific fact from an action line or
template file. It would be very inefficient to fetch facts from all hosts
every time for every playbook if they aren't going to be used, and I think
I remember that behavior being avoided in one of the recent email threads
on the topic.

Cheers!

Dave

Ah, yup, looks like you're right. FWIW, I see the same thing
(everyone being imported in the VARIABLE IMPORT PHASE) when I create a
setup like you describe.

matt

Hey Folks,

So in a simple test playbook I am playing with that only plays against 1 host, I do 2 vars_files imports. When I run the playbook (from the devel branch pulled a day ago), I get an unexpected behavior that seems odd to me. Based on the output, it would appear that regardless of how many hosts are declared in the playbook, it does a variable import for all hosts found in the inventory file. Am I correct in my assumption?

Imports happen per play.

It seems wasteful on a few levels, as it adds to the runtime of the playbook as it iterates over all the hosts (basically unnecessary work for hosts not used in the playbook)

That’s probably a regression from the new inventory support. It should also be reasonably easy to fix. File a bug, if something isn’t in github’s tracker, I don’t know about it.

–Michael