Support for shell process substitution or /dev/stdin inventory source

Hello everyone,

I’m brand new to this list and this is my first message.
Apologize if this subject has already been mentioned anywhere.

I’m trying to use shell Process substitution as an Ansible inventory source.

Example:
$ ansible -i <(curl https://cmdb/.../) all …

I’ve made a few tests with different versions of Ansible and this used to work with v1.5.x. This does not work with v1.9.x or v2.x.
I’ve created a gist ( [1] ) where the tests results are detailed.

This ability of using process substitution was very useful and allowed one to:

  • quickly create dynamic inventories
  • read inventory from STDIN

I know that dynamic inventories exist.
However, being able to use shell process substitution could be very useful for quick ad-hoc operations or quick integration with existing CMDB, etc…

After digging into the code, it seems that the problem is due to this pattern:

  1. open() inventory file
  2. readlines() and get first line
  3. close() inventory file
  4. re-open() inventory file

When dealing with shell process substitution the problem is that you can’t re-access the data as in a standard file.

For what I know, those are kind of “ephemeral inputs” where you can’t re-open() or seek().

IMHO, this could be a cool new feature to ansible and a cheap way to get “dynamic inventory” for “quick and dirty operations”.

The fastest and cheapest way I see to handle that kind of “ephemeral inventory sources” could be to implement a new boolean CLI option.
That option could trigger a “copy of the inventory file” to a temporary file that will be used as a classic ansible inventory file.

I’ve done a quick POC here [2] and everything seems to work like I expect (see [3]). I’ve not written any unit test since I wanted the community opinion
about this very option.

[1] https://gist.github.com/riton/b393a2942b859be666e7
[2] https://github.com/ansible/ansible/compare/devel...ccin2p3:feature/ephemeral_inventory?expand=1
[3] https://gist.github.com/riton/21a3fea5f90ffbb2a1f6

Cheers

Rémi

This is exactly why I submitted this issue and subsequent PR:

https://github.com/ansible/ansible/issues/13931

https://github.com/ansible/ansible/issues/13932

If you apply my commit, you can do exactly what you’re looking for with a minor adjustment:

$ ansible -i <(curl … | tr “\n” “,”) all …