How to merge inventories from other ansible projects?

104 views
Skip to first unread message

Glen Huang

unread,
Oct 18, 2022, 10:54:17 PM10/18/22
to Ansible Project
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.

Brian Coca

unread,
Oct 19, 2022, 9:20:40 AM10/19/22
to ansible...@googlegroups.com
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


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

Glen Huang

unread,
Oct 19, 2022, 10:09:53 AM10/19/22
to Ansible Project
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:

proj2/inventory/hosts.yml
all:
  vars:
    bar: from 2 all
  hosts:
    example2.com:

proj3/playbook.yml
- hosts: 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?

Brian Coca

unread,
Oct 19, 2022, 10:31:47 AM10/19/22
to ansible...@googlegroups.com
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.
--
----------
Brian Coca

Rowe, Walter P. (Fed)

unread,
Oct 19, 2022, 10:40:55 AM10/19/22
to ansible...@googlegroups.com
conflicts will be resolved by overwriting precedence.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2FCACVha7e1dqMiyipGFx7bdLYMtGd2rzaUaYSt%252BUkb1MJVk1S6Tw%2540mail.gmail.com&data=05%7C01%7Cwalter.rowe%40nist.gov%7Cd305eba53ad5465dbfa208dab1de9dc0%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638017866942435065%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Amt%2F0E%2BroVjBQKDg58khNKMmxYEvQ7RBt4LHoFeGDlI%3D&reserved=0.

Glen Huang

unread,
Oct 20, 2022, 7:20:27 AM10/20/22
to Ansible Project
Guess I'll use a script to generate the inventory, thanks for the heads up.
Reply all
Reply to author
Forward
0 new messages