Dynamic inventory script plugin configuration with custom binary

Hi guys,

I have trouble configuring a custom binary dynamic inventory plugin. I have the following configuration in inventory/serf.yml:

---
plugin: ansible.builtin.script
path: ../../serf_inv/target/debug/serf_inv

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!

use -i ../serf_inv/target/debug/serf_inv … the script inventory plugin does not use configuration, it expects any executable file that returns JSON.

3 Likes

Thanks a lot. Not the answer I was hoping for but it is what is is!

not sure what you are trying/hoping to do, if you explain it, maybe i can help

1 Like

Sure maybe there is another way.

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.

The binary itself should go in the inventory directory. The script plugin doesn’t support a YAML file.

$ ls -l inventory/
total 8
-rwxr-xr-x. 1 shertel shertel 61 Jan  3 20:41 inv.sh
-rw-r--r--. 1 shertel shertel 33 Jan  3 20:33 inv.yml
$ ansible-inventory -i inventory/ --graph
@all:
  |--@ungrouped:
  |--@test_yaml_group:
  |--@test_script_group:

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

plugin: ansible.builtin.script
path: "{{ dynamic }}"

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.

1 Like

a symliink should work just fine, no need for a wrapper

1 Like

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!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.