> On May 5, 2016, at 4:37 PM, Matt Martz <[email protected]> wrote:
> 
> In general neither dynamic inventory scripts nor modules have access to 
> host/group vars.
> 
> host/group vars are part of inventory, so for me it feels weird that an 
> inventory script would need access to them, as opposed to providing them.

I can see why this might seem weird.

But what if  a dynamic inventory script could benefit from a variable defined 
elsewhere, for example, in a static inventory file?
Sure I could define yet another config file, or use environment variables, but 
that is just spreading my configuration around in more places.

To give a highly contrived example, what if I was writing a dynamic inventory 
that was going to dispatch to various cloud-based dynamic inventory scripts, 
where the list of cloud providers I currently use is defined in a high level 
text inventory file
My contrived dispatching cloud inventory script would obtain that list,
and then call each specific cloud inventory script.  Again, this is just an 
example and not really what I am doing,

So there is no way for an inventory script to ask ansible for the value of a 
variable?
I guess I could shell out to ansible, and give it ad-hoc args to lookup and 
return the variable, and parse the result.  Yuck!

> As far as modules, you should require that the var be passed to the module.  
> The only way to get this by default is to also write an action plugin that 
> could pass the var to the module for you.

This is helpful, thanks!

I just found this:  Ansible: Modules and Action Plugins 
<http://ndemengel.github.io/2015/01/20/ansible-modules-and-action-plugins/>, 
which uses 

        
        template_string = '' % fact_name
        res = template.template(self.basedir, template_string, inject)
        return None if res == template_string else res
to look up the value of a variable.  Is this the best way?

It seems to me that there is plenty of configured state that some combination 
of Ansible itself and modules leverage, things like ansible_python_interpreter, 
etc.


> 
> On Thursday, May 5, 2016, Donald Clark Jackson <[email protected] 
> <mailto:[email protected]>> wrote:
> 
> Hello,
> 
> I'm developing both an Ansible module and a dynamic inventory script, in 
> Python.
> 
> In each case, I need to lookup/obtain the value of a host/group variable.
> 
> I've looked through the documentation, and tried a bunch of online searches, 
> but haven't really found anything very illuminating.
> 
> Any advice/pointers would be greatly appreciated.
> 
> Don
> 
> --
> You received this message because you are subscribed to the Google Groups 
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] <javascript:;>.
> To post to this group, send email to [email protected] 
> <javascript:;>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/8C6DC569-7B75-4E84-B84C-2A9F06C1003B%40drivescale.com
>  
> <https://groups.google.com/d/msgid/ansible-project/8C6DC569-7B75-4E84-B84C-2A9F06C1003B%40drivescale.com>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.
> 
> 
> -- 
> Matt Martz
> @sivel
> sivel.net <http://sivel.net/>
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] 
> <mailto:[email protected]>.
> To post to this group, send email to [email protected] 
> <mailto:[email protected]>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/CAD8N0v-wMQ-5-EM%2BxJN3A%3DCC6rko85Sdi9irsrVrL-XOQVktNA%40mail.gmail.com
>  
> <https://groups.google.com/d/msgid/ansible-project/CAD8N0v-wMQ-5-EM%2BxJN3A%3DCC6rko85Sdi9irsrVrL-XOQVktNA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/F8D6331D-ACC2-486B-8F2E-5326E838B688%40drivescale.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to