# Would It Make Sense to Allow Multiple Inventories?

**URL:** <https://forum.ansible.com/t/would-it-make-sense-to-allow-multiple-inventories/23640>\
**Category:** Ansible Project\
**Created:** [February 4, 2017, 6:55pm UTC](https://forum.ansible.com/t/would-it-make-sense-to-allow-multiple-inventories/23640 "2017-02-04T18:55:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Forrest](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Jon\_Forrest](https://forum.ansible.com/u/Jon_Forrest)\
**Post date:** [February 4, 2017, 6:55pm UTC](https://forum.ansible.com/t/would-it-make-sense-to-allow-multiple-inventories/23640/1 "2017-02-04T18:55:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [February 5, 2017, 8:27am UTC](https://forum.ansible.com/t/would-it-make-sense-to-allow-multiple-inventories/23640/2 "2017-02-05T08:27:05Z")

</div>

I wonder if the issue you describe could be solved with a symlink?

---

<div class="post-metadata">

**Author:** ![Jon\_Forrest](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Jon\_Forrest](https://forum.ansible.com/u/Jon_Forrest)\
**Post date:** [February 5, 2017, 6:14pm UTC](https://forum.ansible.com/t/would-it-make-sense-to-allow-multiple-inventories/23640/3 "2017-02-05T18:14:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [February 5, 2017, 7:26pm UTC](https://forum.ansible.com/t/would-it-make-sense-to-allow-multiple-inventories/23640/4 "2017-02-05T19:26:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jon\_Forrest](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Jon\_Forrest](https://forum.ansible.com/u/Jon_Forrest)\
**Post date:** [February 5, 2017, 9:01pm UTC](https://forum.ansible.com/t/would-it-make-sense-to-allow-multiple-inventories/23640/5 "2017-02-05T21:01:59Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 6, 2017, 2:18pm UTC](https://forum.ansible.com/t/would-it-make-sense-to-allow-multiple-inventories/23640/6 "2017-02-06T14:18:38Z")

</div>

I've thought about expanding the -i option to work as you ask, it  
shouldn't be too hard.

---

<div class="post-metadata">

**Author:** ![Ted\_Zlatanov](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@Ted\_Zlatanov](https://forum.ansible.com/u/Ted_Zlatanov)\
**Post date:** [February 6, 2017, 2:23pm UTC](https://forum.ansible.com/t/would-it-make-sense-to-allow-multiple-inventories/23640/7 "2017-02-06T14:23:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jon\_Forrest](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Jon\_Forrest](https://forum.ansible.com/u/Jon_Forrest)\
**Post date:** [February 6, 2017, 3:25pm UTC](https://forum.ansible.com/t/would-it-make-sense-to-allow-multiple-inventories/23640/8 "2017-02-06T15:25:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 6, 2017, 3:42pm UTC](https://forum.ansible.com/t/would-it-make-sense-to-allow-multiple-inventories/23640/9 "2017-02-06T15:42:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jon\_Forrest](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Jon\_Forrest](https://forum.ansible.com/u/Jon_Forrest)\
**Post date:** [February 7, 2017, 5:40am UTC](https://forum.ansible.com/t/would-it-make-sense-to-allow-multiple-inventories/23640/10 "2017-02-07T05:40:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 9, 2017, 11:04pm UTC](https://forum.ansible.com/t/would-it-make-sense-to-allow-multiple-inventories/23640/11 "2017-02-09T23:04:36Z")

</div>

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
