# Variable Import Question

**URL:** <https://forum.ansible.com/t/variable-import-question/11458>\
**Category:** Ansible Project\
**Created:** [April 26, 2012, 4:03pm UTC](https://forum.ansible.com/t/variable-import-question/11458 "2012-04-26T16:03:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_Coutu](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@Dave\_Coutu](https://forum.ansible.com/u/Dave_Coutu)\
**Post date:** [April 26, 2012, 4:03pm UTC](https://forum.ansible.com/t/variable-import-question/11458/1 "2012-04-26T16:03:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Matt\_Coddington](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Matt\_Coddington](https://forum.ansible.com/u/Matt_Coddington)\
**Post date:** [April 26, 2012, 5:28pm UTC](https://forum.ansible.com/t/variable-import-question/11458/2 "2012-04-26T17:28:54Z")

</div>

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](http://ansible.github.com/playbooks.html).

matt

---

<div class="post-metadata">

**Author:** ![Dave\_Coutu](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@Dave\_Coutu](https://forum.ansible.com/u/Dave_Coutu)\
**Post date:** [April 26, 2012, 5:48pm UTC](https://forum.ansible.com/t/variable-import-question/11458/3 "2012-04-26T17:48:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Matt\_Coddington](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Matt\_Coddington](https://forum.ansible.com/u/Matt_Coddington)\
**Post date:** [April 26, 2012, 6:08pm UTC](https://forum.ansible.com/t/variable-import-question/11458/4 "2012-04-26T18:08:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [April 26, 2012, 10:49pm UTC](https://forum.ansible.com/t/variable-import-question/11458/5 "2012-04-26T22:49:15Z")

</div>

> 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
