# How to merge inventories from other ansible projects?

**URL:** <https://forum.ansible.com/t/how-to-merge-inventories-from-other-ansible-projects/37243>\
**Category:** Ansible Project\
**Created:** [October 19, 2022, 2:54am UTC](https://forum.ansible.com/t/how-to-merge-inventories-from-other-ansible-projects/37243 "2022-10-19T02:54:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glen\_Huang](https://avatars.discourse-cdn.com/v4/letter/g/edb3f5/32.png) [@Glen\_Huang](https://forum.ansible.com/u/Glen_Huang)\
**Post date:** [October 19, 2022, 2:54am UTC](https://forum.ansible.com/t/how-to-merge-inventories-from-other-ansible-projects/37243/1 "2022-10-19T02:54:17Z")

</div>

I have existing two ansible projects:

proj1/inventory/host.yml  
all:  
vars:…  
hosts: …  
children: …

proj2/inventory/host.yml  
all:  
vars:…  
hosts: …  
children: …

I want to create a new ansible project, whose inventory is the automatic merging of the existing two, which should be something like:

all:  
children:  
proj1:  
vars: … # from proj1’s all.vars  
hosts: … # from proj1’s all.hosts  
children: … # from proj1’s all.children  
proj2:  
vars: … # from proj2’s all.vars  
hosts: … # from proj2’s all.hosts  
children: … # from proj2’s all.children

Is this something easily achievable in ansible?

Thanks in advance.

---

<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:** [October 19, 2022, 1:18pm UTC](https://forum.ansible.com/t/how-to-merge-inventories-from-other-ansible-projects/37243/2 "2022-10-19T13:18:57Z")

</div>

You can just reference both when calling ansible-playbook:

ansible-playbook -i /inv1 -i /inv2 play.yml

Or If you want a static copy:

ansible-inventory -i /inv1 -i /inv2 --exports --list --yaml \> newmerged.yml

---

<div class="post-metadata">

**Author:** ![Glen\_Huang](https://avatars.discourse-cdn.com/v4/letter/g/edb3f5/32.png) [@Glen\_Huang](https://forum.ansible.com/u/Glen_Huang)\
**Post date:** [October 19, 2022, 2:09pm UTC](https://forum.ansible.com/t/how-to-merge-inventories-from-other-ansible-projects/37243/3 "2022-10-19T14:09:53Z")

</div>

Thanks for the quick reply. However I don’t think it works.

Maybe I was not clear. Here is a small test case to demonstrate the issue:

proj1/inventory/hosts.yml  
all:  
vars:  
bar: from 1 all  
hosts:  
[example1.com](http://example1.com):

proj2/inventory/hosts.yml  
all:  
vars:  
bar: from 2 all  
hosts:  
[example2.com](http://example2.com):

proj3/playbook.yml

- hosts: [example1.com](http://example1.com)  
connection: local  
tasks:
- debug:  
var: bar

And run the playbook with  
ansible-playbook -i …/proj1/inventory -i …/proj2/inventory playbook.yml

Apparently proj2’s all.vars will override proj1’s, so bar shows “from 2 all”, which is not what I want.

I want proj3’s inventory to have two groups, group1 and group2, where proj1’s all.vars become group1’s group vars, ditto for proj2, so all.vars don’t get overridden. In the test case, I’d like bar to be “from 1 all”

Is this something possible to do?

---

<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:** [October 19, 2022, 2:29pm UTC](https://forum.ansible.com/t/how-to-merge-inventories-from-other-ansible-projects/37243/4 "2022-10-19T14:29:54Z")

</div>

nothing will autogenerate discrete groups that way, conflicts will be  
resolved by overwriting.  
You'll have to script something that processes each inventory and  
rewrites the entries to what you want.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [October 19, 2022, 2:40pm UTC](https://forum.ansible.com/t/how-to-merge-inventories-from-other-ansible-projects/37243/5 "2022-10-19T14:40:41Z")

</div>

> conflicts will be resolved by ~~overwriting~~ precedence.

Walter

---

<div class="post-metadata">

**Author:** ![Glen\_Huang](https://avatars.discourse-cdn.com/v4/letter/g/edb3f5/32.png) [@Glen\_Huang](https://forum.ansible.com/u/Glen_Huang)\
**Post date:** [October 20, 2022, 11:20am UTC](https://forum.ansible.com/t/how-to-merge-inventories-from-other-ansible-projects/37243/6 "2022-10-20T11:20:27Z")

</div>

Guess I’ll use a script to generate the inventory, thanks for the heads up.
