Would It Make Sense to Allow Multiple Inventories?

56 views
Skip to first unread message

Jon Forrest

unread,
Feb 4, 2017, 1:55:23 PM2/4/17
to ansible...@googlegroups.com
The Ansible documentation says "you can also use multiple inventory
files at the same time" but, AFAIK, that isn't entirely accurate.
If you give the '-i' flag a directory, then any inventory files under
that directory are indeed used, which is nice. However, what if you
want to use several inventory files that are *not* under a specific
parent directory? For example, maybe you want to use /etc/ansible/hosts
and ~/ansible/my_inventory. From what I can see, this isn't currently
possible. Is this correct?

I would think it would be nice if the '-i' option were changed to
accept multiple directories. I know you can currently give multiple
comma-separated hosts so maybe a different character would be used
to separate directories.

What do you think?

Cordially,
Jon Forrest

J Hawkesworth

unread,
Feb 5, 2017, 3:27:05 AM2/5/17
to Ansible Project
I wonder if the issue you describe could be solved with a symlink?

Jon Forrest

unread,
Feb 5, 2017, 1:14:51 PM2/5/17
to ansible...@googlegroups.com
On 2/5/2017 12:27 AM, 'J Hawkesworth' via Ansible Project wrote:
> I wonder if the issue you describe could be solved with a symlink?

I don't think so. A symlink points to a single file (unlike logical
names in good old VAX/VMS). So, it wouldn't be possible to use both
a system inventory and a personal one.

Jon Forrest



J Hawkesworth

unread,
Feb 5, 2017, 2:26:30 PM2/5/17
to Ansible Project
Maybe if you made your user inventory a dir and then had a symlink in there to the shared inventory?

I have remembered now that you can use an executable python script as a source of inventory, so you could read both user and shared inventory in that, which might be a cleaner alternative.

Jon Forrest

unread,
Feb 5, 2017, 4:02:05 PM2/5/17
to ansible...@googlegroups.com
On 2/5/2017 11:26 AM, 'J Hawkesworth' via Ansible Project wrote:
> Maybe if you made your user inventory a dir and then had a symlink in
> there to the shared inventory?

I see what you mean. That might be a good workaround but it sure
seems to me that expanding the '-i' option would be a better
solution.

> I have remembered now that you can use an executable python script as
> a source of inventory, so you could read both user and shared
> inventory in that, which might be a cleaner alternative.

Again, that sounds like a fine workaround but there should be
a clean and simple way of doing this.

Jon



Brian Coca

unread,
Feb 6, 2017, 9:18:51 AM2/6/17
to Ansible Project
I've thought about expanding the -i option to work as you ask, it
shouldn't be too hard.


----------
Brian Coca

Ted Zlatanov

unread,
Feb 6, 2017, 9:23:36 AM2/6/17
to ansible...@googlegroups.com
On Sat, 4 Feb 2017 10:55:05 -0800 Jon Forrest <nob...@gmail.com> wrote:

JF> The Ansible documentation says "you can also use multiple inventory
JF> files at the same time" but, AFAIK, that isn't entirely accurate.
JF> If you give the '-i' flag a directory, then any inventory files under
JF> that directory are indeed used, which is nice. However, what if you
JF> want to use several inventory files that are *not* under a specific
JF> parent directory? For example, maybe you want to use /etc/ansible/hosts
JF> and ~/ansible/my_inventory. From what I can see, this isn't currently
JF> possible. Is this correct?

JF> I would think it would be nice if the '-i' option were changed to
JF> accept multiple directories. I know you can currently give multiple
JF> comma-separated hosts so maybe a different character would be used
JF> to separate directories.

Simply accepting multiple `-i` options would be enough, no need for
special characters.

In addition, inventory locations could be URLs in addition to file
locations. That would decouple the inventory so it can be easily
distributed to new machines etc.

Obviously the inventory script could download a URL, but that still
requires a local executable to be created and maintained.

Ted


Jon Forrest

unread,
Feb 6, 2017, 10:25:15 AM2/6/17
to ansible...@googlegroups.com
On 2/6/2017 6:18 AM, Brian Coca wrote:
> I've thought about expanding the -i option to work as you ask, it
> shouldn't be too hard.

Maybe I'll give it a try. The only thing I can think of that
might be a problem is distinguishing arguments that are hostnames
from those that are inventory files. In a way it's too bad that
hostnames are allowed in a '-i' argument.

Jon


Brian Coca

unread,
Feb 6, 2017, 10:42:19 AM2/6/17
to Ansible Project
the distinction is easy, hostnames are only allowed when there is a ,
. so if the string after -i does not have a comma, its a path.


----------
Brian Coca

Jon Forrest

unread,
Feb 7, 2017, 12:40:21 AM2/7/17
to ansible...@googlegroups.com
On 2/6/2017 7:42 AM, Brian Coca wrote:
> the distinction is easy, hostnames are only allowed when there is a ,
> . so if the string after -i does not have a comma, its a path.

I'm not so sure. The help for '-i' says

"specify inventory host path (default=/etc/ansible/hosts) or comma
separated host list."

How would it be possible to distinguish a comma separated host list
from a comma separated inventory host path list?

For example, would

-i a,b,c

be the hosts "a", "b", and "c", or the files (or directories) "a", "b",
and "c"?

A very quick glance at inventory/__main__.py(parse_inventory) makes me
think that the elements of the string are first checked to see if
they're a host, and then checked to see if they're a directory or file.
The presence of a comma in itself isn't enough to differentiate paths
an host. Of course, I just started reading Ansible source so I'm not
100% sure I have this right.

I was thinking that maybe requiring that host paths contain slash
characters, as part of either an absolute or relative path, would work.

Or maybe saying if the string argument to "-i" ends in a comma, then
the whole string would be considered a host list, but if it ends in
anything other than a comma, it would be a inventory host path list.

Anyway, thanks for your input.

Jon



Brian Coca

unread,
Feb 9, 2017, 6:04:43 PM2/9/17
to Ansible Project
We do not support comma separated host paths, adding multiple paths
can be done by allowing multiple -i

ansible -i ./myhosts -i /etc/ansible/hosts


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages