"Tuning the External Inventory Script"

61 views
Skip to first unread message

Andy Baker

unread,
Aug 11, 2015, 12:21:39 PM8/11/15
to Ansible Project
This passage: http://docs.ansible.com/ansible/developing_inventory.html#tuning-the-external-inventory-script

seems to say you have two options:

1. Return a list of groups when called with --list and then return all the vars for a single group when called with --host
2. Return all groups and all vars in one go when called with --list (the 'tuned' version)

However in both cases you're forced to generate all groups and all vars.

If I'm running a command on a single host or group - I rather expected there would be a way to avoid generating the vars for all the groups or hosts that I have no intention of connecting to.

Have I misunderstood something?

Timothy Appnel

unread,
Aug 11, 2015, 7:38:33 PM8/11/15
to Ansible Project
Yes you have. In option #1, when --host is call you only return that one host and it's host variables, if any. (You said a single group.) You don't return any groups or group variables. 

The dynamic inventory plugin API is a coarse-grained one that slurps in everything it can get from the inventory source. There are many reasons for that such as tasks that reference other host resources not targeted by the play. 


photo
Timothy Appnel
Senior Solutions Architect

Ansible, Inc.  www.ansible.com

--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/99d61d16-f33d-453d-a23c-4663befaa9c5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andy Baker

unread,
Aug 12, 2015, 7:31:18 AM8/12/15
to Ansible Project
>  In option #1, when --host is call you only return that one host and it's host variables, if any. 

So - I'm logging how my dynamic inventory script is called. When I run the following: ansible-playbook -i dynamic_inventory/get_hosts test.yaml -e "group=mygroup"
    
get_hosts is first called with --list

I tried putting some vars in the list output under _meta.hostvars. I was wondering if that would stop Ansible querying get_hosts with --host for every single group. However it stops it calling --host for *any* group so I'm back to outputting all vars for all groups.

I can't seem to find a way to only need to generate vars for a single group (or host).

One thought - I'm currently using a separate group for each host as that seemed to simplest way to do handle my particular requirements (I'm only ever running a playbook against a single host). You can see above I'm conflating 'host' and 'group' somewhat.

Is this part of the problem or is that unrelated?


Is this part of the problem or is that unrelated?

Claude Robitaille

unread,
Sep 1, 2020, 7:33:51 PM9/1/20
to Ansible Project
I have the exact same need; I want to target a single host, for which I provide a label to the play as an extra-args. I do not need to pull the whole list of hosts in my inventory. It takes too much time and is unnecessary. Just having my inventory script called with the host label is enough to return the needed data (including the IP address in my case).

Was there a solution since 2015?
Reply all
Reply to author
Forward
0 new messages