# Variables from executable inventory not working in playbook

**URL:** <https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534>\
**Category:** Ansible Project\
**Created:** [May 11, 2012, 7:02pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534 "2012-05-11T19:02:17Z")\
**Posts on this page:** 15\
**Page:** 1

<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:** [May 11, 2012, 7:02pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/1 "2012-05-11T19:02:17Z")

</div>

All,  
On the latest devel, I am using an executable inventory that returns this:

% ./hosts testhost  
{  
&nbsp;&nbsp;&nbsp;"webtype" : "webapp"  
}

But when I run a playbook that includes a file with a task that  
includes $webtype like this, it doesn't seem to get replaced by the  
string "webapp";

- name: copy ssl.conf  
&nbsp;&nbsp;action: copy src=$conf/web/templates/$stack/ssl.conf.$webtype  
dest=/foo/ssl.conf owner=root group=root mode=0644

The error I see is this:

fatal: [testhost] =\> file or module does not exist:  
/ansible/conf/web/files/prod/httpd.conf.$webtype

Note that it did replace $conf and $stack appropriately, but $conf is  
set from a vars\_files file, and $stack is set from a "vars:" entry  
directly in the playbook. I know this is supposed to work, right?  
Does anyone have suggestions for troubleshooting?

thanks!  
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:** [May 11, 2012, 7:03pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/2 "2012-05-11T19:03:59Z")

</div>

Wrong return structure. Correct is:

{

whatever: “…”

ansible\_facts: {  
webtype: “webapp”  
}

}

---

<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:** [May 11, 2012, 7:17pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/3 "2012-05-11T19:17:18Z")

</div>

Thanks Michael, I thought for sure that would fix it, but I'm still  
seeing the same error. ansible\_ssh\_port is definitely working, so i  
know this is getting parsed. Here is exact json returned now for one  
of my systems seeing that error:

{  
&nbsp;&nbsp;&nbsp;"ansible\_ssh\_port" : 222,  
&nbsp;&nbsp;&nbsp;"ansible\_facts" : {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"apacheconf" : "/etc/httpd",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"webtype" : "webapp"  
&nbsp;&nbsp;&nbsp;}  
}

thanks,  
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:** [May 11, 2012, 7:20pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/4 "2012-05-11T19:20:53Z")

</div>

Did you call the module before you referenced the fact?

Are you using the devel branch?

---

<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:** [May 11, 2012, 7:25pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/5 "2012-05-11T19:25:45Z")

</div>

> Did you call the module before you referenced the fact?

These variables aren't coming from a module, they're coming from an  
executable inventory. I was following the instructions at  
[http://ansible.github.com/api.html#external-inventory](http://ansible.github.com/api.html#external-inventory), which is why I  
hadn't known about the ansible\_facts key (I also noticed that you need  
to call the inventory with a "--list" argument instead of no argument  
as documented to return the inventory itself). From that  
documentation, I'm assuming I can set a bunch of variables for use  
within the playbook by calling my inventory script with the host as an  
argument.

> Are you using the devel branch?

Yup, I am using the latest devel.

thanks,  
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:** [May 11, 2012, 7:29pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/6 "2012-05-11T19:29:21Z")

</div>

Yeah, those work differently.

The --list thing is a regression that should be fixed.

---

<div class="post-metadata">

**Author:** ![Jeroen\_Hoekx](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@Jeroen\_Hoekx](https://forum.ansible.com/u/Jeroen_Hoekx)\
**Post date:** [May 11, 2012, 7:29pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/7 "2012-05-11T19:29:42Z")

</div>

Hi Matt,

---

<div class="post-metadata">

**Author:** ![Jeroen\_Hoekx](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@Jeroen\_Hoekx](https://forum.ansible.com/u/Jeroen_Hoekx)\
**Post date:** [May 11, 2012, 7:31pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/8 "2012-05-11T19:31:22Z")

</div>

This has been that way since I started using ansible and before any  
inventory rewrite... So the best option is to edit the docs, since  
anyone who's using ansible will be using --list and --host.

Greetings,

Jeroen

---

<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:** [May 11, 2012, 7:32pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/9 "2012-05-11T19:32:18Z")

</div>

Sorry, I reread the doc and see those are only available to templates,  
not to playbooks. Bummer. I'll need to write a module to provide the  
facts to the playbooks. Modules are executed only on the remote host  
though, not on the ansible master? I have a bunch of variables that  
the master knows about in my environment that I'd like to be able to  
use in playbooks.

@Jeroen, saw your update just now: adding --host doesn't seem to help,  
I suspect because I'm trying to use variables in playbooks that are  
only available to templates...

matt

---

<div class="post-metadata">

**Author:** ![Jeroen\_Hoekx](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@Jeroen\_Hoekx](https://forum.ansible.com/u/Jeroen_Hoekx)\
**Post date:** [May 11, 2012, 8:09pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/10 "2012-05-11T20:09:20Z")

</div>

It works for me. Complete example at [https://gist.github.com/2662146](https://gist.github.com/2662146)

Jeroen

---

<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:** [May 11, 2012, 8:11pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/11 "2012-05-11T20:11:46Z")

</div>

Thank you Jeroen! I will report back when I figure out what my problem is.

matt

---

<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:** [May 11, 2012, 8:41pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/12 "2012-05-11T20:41:46Z")

</div>

> Thank you Jeroen! I will report back when I figure out what my problem is.

Figured it out. When using the executable inventory you DO need to  
pass the --host argument to your inventory script, but you should NOT  
return ansible\_facts, just return a flat list of variables as the docs  
describe. [http://ansible.github.com/api.html#script-conventions](http://ansible.github.com/api.html#script-conventions) could  
be fixed by replacing "When the external node script is called with no  
arguments" with "When the external node script is called with the  
argument '--list'", and replacing "When called with a single argument,  
the name of a host from above" with "When called with arguments  
'--host \<hostname\>'".

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:** [May 11, 2012, 9:11pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/13 "2012-05-11T21:11:22Z")

</div>

Agreed

-- Michael

---

<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:** [May 11, 2012, 9:12pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/14 "2012-05-11T21:12:33Z")

</div>

> Sorry, I reread the doc and see those are only available to templates,  
> not to playbooks

I wrote the docs, I believe you misread, but show me.

---

<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:** [May 11, 2012, 9:24pm UTC](https://forum.ansible.com/t/variables-from-executable-inventory-not-working-in-playbook/11534/15 "2012-05-11T21:24:38Z")

</div>

at [http://ansible.github.com/api.html#script-conventions](http://ansible.github.com/api.html#script-conventions) it says "When  
called with a single argument, the name of a host from above, the  
script must return either an empty JSON hash/dictionary, or a list of  
key/value variables to make available to templates". So, you're  
correct it doesn't actually say \_only\_ available to templates. I just  
read it that way when I was having so much trouble getting the  
variables to work in playbooks.

I've submitted a pull request for the changes that were discussed in  
this thread...

[https://github.com/ansible/ansible.github.com/pull/107](https://github.com/ansible/ansible.github.com/pull/107)

matt
