# Dynamic inventory script plugin configuration with custom binary

**URL:** <https://forum.ansible.com/t/dynamic-inventory-script-plugin-configuration-with-custom-binary/3127>\
**Category:** Get Help\
**Created:** [January 2, 2024, 10:35pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-plugin-configuration-with-custom-binary/3127 "2024-01-02T22:35:53Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pizzapill](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@pizzapill](https://forum.ansible.com/u/pizzapill)\
**Post date:** [January 2, 2024, 10:35pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-plugin-configuration-with-custom-binary/3127/1 "2024-01-02T22:35:53Z")

</div>

Hi guys,

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

```auto
---
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!

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [January 2, 2024, 11:17pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-plugin-configuration-with-custom-binary/3127/2 "2024-01-02T23:17:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pizzapill](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@pizzapill](https://forum.ansible.com/u/pizzapill)\
**Post date:** [January 3, 2024, 7:47am UTC](https://forum.ansible.com/t/dynamic-inventory-script-plugin-configuration-with-custom-binary/3127/3 "2024-01-03T07:47:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [January 3, 2024, 2:26pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-plugin-configuration-with-custom-binary/3127/4 "2024-01-03T14:26:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pizzapill](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@pizzapill](https://forum.ansible.com/u/pizzapill)\
**Post date:** [January 3, 2024, 3:02pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-plugin-configuration-with-custom-binary/3127/5 "2024-01-03T15:02:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [January 4, 2024, 1:47am UTC](https://forum.ansible.com/t/dynamic-inventory-script-plugin-configuration-with-custom-binary/3127/6 "2024-01-04T01:47:06Z")

</div>

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

```auto
$ 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

```

```auto
$ ansible-inventory -i inventory/ --graph
@all:
  |--@ungrouped:
  |--@test_yaml_group:
  |--@test_script_group:

```

---

<div class="post-metadata">

**Author:** ![pizzapill](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@pizzapill](https://forum.ansible.com/u/pizzapill)\
**Post date:** [January 4, 2024, 10:10am UTC](https://forum.ansible.com/t/dynamic-inventory-script-plugin-configuration-with-custom-binary/3127/7 "2024-01-04T10:10:45Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [January 4, 2024, 2:22pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-plugin-configuration-with-custom-binary/3127/8 "2024-01-04T14:22:55Z")

</div>

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](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-host-list) 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`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/auto_inventory.html) as a shortcut to avoid configuring [additional inventory plugins](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#inventory-enabled). 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

```auto
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.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [January 4, 2024, 2:23pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-plugin-configuration-with-custom-binary/3127/9 "2024-01-04T14:23:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pizzapill](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@pizzapill](https://forum.ansible.com/u/pizzapill)\
**Post date:** [January 4, 2024, 2:53pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-plugin-configuration-with-custom-binary/3127/10 "2024-01-04T14:53:08Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 3, 2024, 2:53pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-plugin-configuration-with-custom-binary/3127/11 "2024-02-03T14:53:42Z")

</div>

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