ansible/ansible — pull request #7813 by sixninetynine (2014-06-17 20:07:48+00:00)
Title: host-pattern as script argument

### Hello!

Very interested in taking Ansible out for a test drive in my current position, however I ran into a problem with the dynamic script implementation that didn't quite work for our current inventory system ([range](https://github.com/ytoolshed/range)).

The way range works (for us) is to provide a cluster name to expand, and you recieve back a list of hosts, for example:

$ range --expand "%datacenter.cluster.instance"
host1.localdomain
host2.localdomain
host3.localdomain

You can also get information about a single host by various means (akin to the --host implementation expected to be returned by dynamic inventory scripts)

While it is possible to return every cluster and subsequent members of each cluster by querying range, it's a somewhat expensive and potentially slow thing to do (especially when dealing with 10s of thousands of hosts).

The cleanest and most obvious way to deal with this would be to provide a clustername to the dynamic inventory query in order to only get back cluster members from specific clusters, akin to:

$ /etc/ansible/range.py --list %datacenter.cluster.instance
{
'%datacenter.cluster.instance': [
'host1.localdomain',
'host2.localdomain',
'host3.localdomain'
],
'_meta': {
'hostvars': {
'host1.localdomain': {},
'host2.localdomain': {},
'host3.localdomain': {}
}
}
}

In order to accomplish this without having to do anything drastic to ansible.inventory.Inventory() I simply added a boolean '-r' argument to bin/ansible that indicates that the host-pattern should be passed to the dynamic inventory script as an argument. This limits the scope of the query to only the cluster I care about, while keeping the usage syntax the same.

I recognize that there are probably better ways to do this than what is represented in my pull request, but think of it more as a proof of concept. All tests pass, all default syntax remains the same, merely adding the option to pass the host-pattern to the dynamic inventory script.

Thanks for looking!

P.S. Sorry about the whitespaces in the diff, I have a VIM plugin that automatically cleans them up :(