Hello,
So I recently opened a PR against ansible with the intention of passing the ‘host-pattern’ command line argument to my dynamic inventory script in order to scope responses to just the set of hosts I intended to work on. I was able to modify ansible-playbooks to do the same by passing the ‘args’ variable of ansible-playbook to my inventory script and then building my json response by inspecting the YAML files and scoping the response to what was represented in the ’ - hosts’ key of my playbooks.
The reason I wanted this functionality is because I use an inventory system that returns data as needed / on demand. We have 10s of thousands of hosts across a variety of environments/datacenters and getting all hostnames / clusters back with every call to ansible is not scaleable. The suggestions in my PR are to use local caching, which is obviously a good suggestion, it would certainly speed up getting a response, and limit API calls to my inventory system, but the problem is then consistency and adding an additional layer of cache to a system that is already cached.
Here’s an example of what I’m talking about:
/etc/ansible/hosts --list %prod.xyz-service
{
“%prod.xyz-service”: [
“host1”,
“host2”,
“host3”
],
“_meta”: {
“hostvars”: {
“host1”: {},
“host2”: {},
“host3”: {}
}
}
}
which allows me to call ansible like:
ansible -r %prod.xyz-service -m ping
likewise, if I have playbooks, it works similarly:
cat ~/test-playbook.yaml
- hosts: ‘%prod.xyz-service’
sudo: True
tasks: - name: install keyczar
yum: name=python-keyczar state=latest
/etc/ansible/hosts --list /home/loren/test-playbook.yaml /home/loren/test-playbook2.yaml
{
“%prod.xyz-service”: [
“host1”,
“host2”,
“host3”
],
“%prod.foo-service”: [
“foo-host1”,
“foo-host2”,
“foo-host3”
],
}
ansible-playbook -r /home/loren/test-playbook.yaml /home/loren/test-playbook2.yaml
So my question is – is anyone using some method of scoping their inventory scripts with success? I have a fork with the changes described above (note the ‘-r’), but I get a real “icky” feeling when using tools with hacks. I’d love to use ansible vanilla without hacking around my problem, the suggestions in my PR were to use environment variables, which is perfectly reasonable, except I would have to then be issuing 2 commands every time I wanted to invoke ansible, and it would make the host-pattern argument itself redundant. The only other option, as far as I can tell, is to use local caching in my inventory script and actually make a large call to my inventory backend upon first invocation. This is less than ideal because now I (and the other 100+ members of my org) now have to maintain a local cache of an already cached inventory system that is consumed by 100s of other tools, simply shifting the hackery from ansible to the inventory system, instead of working in harmony in some way.
Thanks!