When I try to run it with ansible-inventory -i inventory/ --list -v I get a bunch of errors.
The documentation ansible-doc -t inventory script has no example and names only a single parameter: always_show_stderr. Therefore I’m pretty sure I’m doing something fundamentally wrong.
The binary works when I pass it directly: ansible-inventory -i ../../serf_inv/target/debug/serf_inv --list.
I tried to find examples etc. on Google but I couldn’t get any good results, partly because there is alot of documentation for the builtin script task. Thanks a lot for your help!
Basically I have a binary that communicates with Serf to collect hosts, does some customization and spits out inventory JSON. This is written in Rust and I don’t want to touch Ansible plugin integration code and/or Python.
I would like to use this as a dynamic inventory source. This works when passing the binary directly as argument to the CLI programs (e.g. ansible-playbook -i path/binary playbook.yml). Basically what you suggested.
I rather have this configured as just another dynamic inventory source via yaml in my inventory directory. So I would have a inventory/serf.yml which calls the binary and i can just call ansible-playbook -i inventory/ playbook.yml.
As far as I understand currently I can provide either dynamic inventories via the available plugins (e.g. AWS) and/or static inventory files. No way to point to a custom binary as “Plugin” for dynamic inventory w/o writing a full plugin.
Thanks @shertel I tried it and it works. I guess I go with your approach, just replacing the copied binary with a shell script that points to the binary and passes parameters.
Thanks again to everybody for your time and insights!
Anytime, glad that works for you. You shouldn’t actually need the indirection of calling a script to from a new script. If you don’t want to move the binary into inventory/, you could create a symlink. cd into the inventory directory and run sudo ln -s /path/to/the/binary_inv binary_inv.
-i can also be provided multiple times, so -i inventory/ -i /path/to/the/scripts/ could combine inventory sources in separate locations. The inventory sources can be configured if you don’t want to pass multiple via the CLI.
This is a tangent, but I just want to clarify what the YAML inventory format plugin: ns.col.plugin_name helps with. That syntax is normally parsed by the ansible.builtin.auto as a shortcut to avoid configuring additional inventory plugins. The script inventory plugin is enabled by default though, so even if the script plugin did supported parsing a YAML config file to find and run the actual script, the auto shortcut is not needed.
In the future the YAML config files will probably handle templating variables, and then I could see a case where
would make the script more dynamic. But currently the YAML config files are normally full of hardcoded options, so until then I don’t think adding YAML config support to the script inventory plugin adds any new functionality.
Thanks again you guys gave me a lot to consider. I was tempted by the bash file direction because there I can do other shenanigans like choosing the latest build (debug vs. release builds) etc. Stuff that concerns interactively developing the system etc.
But ansible.cfg might be a alternative to it. Thanks for your time and input!