<p>Use the <em>constructed</em> inventory plugin. See</p>
<pre class="lang-bash prettyprint-override"><code>shell&gt; ansible-doc -t inventory constructed
</code></pre>
<p>Create the inventory files</p>
<pre class="lang-bash prettyprint-override"><code>shell&gt; tree inventory/
inventory/
├── 01-TestBoxes
├── 02-ProdBoxes
└── 03-Primary.yml

0 directories, 3 files
</code></pre>
<pre class="lang-bash prettyprint-override"><code>shell&gt; cat inventory/01-TestBoxes 
[TestBox:children]
groupA
groupB

[groupA]
host_1

[groupB]
host_2
</code></pre>
<pre class="lang-bash prettyprint-override"><code>shell&gt; cat inventory/02-ProdBoxes 
[ProdBox:children]
groupPA
groupPB

[groupPA]
host_P1

[groupPB]
host_P2
</code></pre>
<pre class="lang-bash prettyprint-override"><code>shell&gt; cat inventory/03-Primary.yml 
plugin: constructed
groups:
  Primary: (group_names | intersect(['ProdBox', 'TestBox'])) | length &gt; 0
</code></pre>
<p>The playbook</p>
<pre class="lang-yaml prettyprint-override"><code>shell&gt; cat pb.yml 
- hosts: Primary
  gather_facts: false
  tasks:
    - debug:
        var: inventory_hostname
</code></pre>
<p>reads files from the directory <em>inventory</em> in alphabetic order. The file <em>Primary.yml</em> creates the group <em>Primary</em> from hosts that are members of the groups <em>ProdBox</em> and <em>TestBox</em></p>
<pre class="lang-yaml prettyprint-override"><code>shell&gt; ansible-playbook -i inventory pb.yml 

PLAY [Primary] *******************************************************************************

TASK [debug] *********************************************************************************
ok: [host_1] =&gt; 
  inventory_hostname: host_1
ok: [host_2] =&gt; 
  inventory_hostname: host_2
ok: [host_P1] =&gt; 
  inventory_hostname: host_P1
ok: [host_P2] =&gt; 
  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
</code></pre>
<p>Q: <em><strong>&quot; I have 100+ files like this. and I want to create primary group with few of the files.&quot;</strong></em></p>
<p>A: Create links</p>
<pre class="lang-bash prettyprint-override"><code>shell&gt; tree inventory/
inventory/
├── 01-TestBoxes -&gt; ../hostFiles/TestBoxes
├── 02-ProdBoxes -&gt; ../hostFiles/ProdBoxes
└── 03-Primary.yml
</code></pre>

CC BY-SA 4.0
2022-11-07T09:08:34.867Z
2022-11-07T09:08:34.867Z
Vladimir Botka
<p>I have two host files with different hosts, hostGroups and super-set in each file like below.</p>
<p>/ets/hostFiles/TestBoxes</p>
<pre><code>[TestBox:children]
groupA
groupB

[groupA]
...
...

[groupB]
...
...
</code></pre>
<p>/ets/hostFile/ProdBoxes</p>
<pre><code>[ProdBox:children]
groupPA
groupPB

[groupPA]
...
...

[groupPB]
...
...
</code></pre>
<p>Now i would like to create a Primary host group with TestBox and ProdBox</p>
<p>Please let me know if this is possible.</p>
<p>Note: This is just an example... I have 100+ files like this. and I want to create primary group with few of the files. <code>hosts=all</code> is not my requirement.</p>

<ansible><ansible-inventory>
Creating a hostgroup from a super-set of hosts
Siva
# Creating a hostgroup from a super-set of hosts

(Asked by: username_0 on 2022-11-07)

I have two host files with different hosts, hostGroups and super-set in each file like below.
/ets/hostFiles/TestBoxes
[TestBox:children]
groupA
groupB

[groupA]
...
...

[groupB]
...
...

/ets/hostFile/ProdBoxes
[ProdBox:children]
groupPA
groupPB

[groupPA]
...
...

[groupPB]
...
...

Now i would like to create a Primary host group with TestBox and ProdBox
Please let me know if this is possible.
Note: This is just an example... I have 100+ files like this. and I want to create primary group with few of the files. hosts=all is not my requirement.

**Tags:** ansible, ansible-inventory


---

## 1 answers

### Answer by username_1 on 2022-11-07. Score: 1

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


---

