Use the constructed inventory plugin. See
shell> ansible-doc -t inventory constructed
Create the inventory files
shell> tree inventory/
inventory/
├── 01-TestBoxes
├── 02-ProdBoxes
└── 03-Primary.yml
0 directories, 3 files
shell> cat inventory/01-TestBoxes
[TestBox:children]
groupA
groupB
[groupA]
host_1
[groupB]
host_2
shell> cat inventory/02-ProdBoxes
[ProdBox:children]
groupPA
groupPB
[groupPA]
host_P1
[groupPB]
host_P2
shell> cat inventory/03-Primary.yml
plugin: constructed
groups:
Primary: (group_names | intersect(['ProdBox', 'TestBox'])) | length > 0
The playbook
shell> cat pb.yml
- hosts: Primary
gather_facts: false
tasks:
- debug:
var: inventory_hostname
reads files from the directory inventory in alphabetic order. The file Primary.yml creates the group Primary from hosts that are members of the groups ProdBox and TestBox
shell> ansible-playbook -i inventory pb.yml
PLAY [Primary] *******************************************************************************
TASK [debug] *********************************************************************************
ok: [host_1] =>
inventory_hostname: host_1
ok: [host_2] =>
inventory_hostname: host_2
ok: [host_P1] =>
inventory_hostname: host_P1
ok: [host_P2] =>
inventory_hostname: host_P2
PLAY RECAP ***********************************************************************************
host_1: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host_2: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host_P1: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host_P2: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Q: " I have 100+ files like this. and I want to create primary group with few of the files."
A: Create links
shell> tree inventory/
inventory/
├── 01-TestBoxes -> ../hostFiles/TestBoxes
├── 02-ProdBoxes -> ../hostFiles/ProdBoxes
└── 03-Primary.yml