# build groups based on hosts and host-variables

**URL:** <https://forum.ansible.com/t/build-groups-based-on-hosts-and-host-variables/38820>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [July 1, 2024, 1:17pm UTC](https://forum.ansible.com/t/build-groups-based-on-hosts-and-host-variables/38820 "2024-07-01T13:17:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![astrakid](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/astrakid/32/1789_2.png) [@astrakid](https://forum.ansible.com/u/astrakid)\
**Post date:** [July 1, 2024, 1:17pm UTC](https://forum.ansible.com/t/build-groups-based-on-hosts-and-host-variables/38820/1 "2024-07-01T13:17:26Z")

</div>

hi,  
i would like to organize my inventory (which is set up and maintained manually):

- one inventory file per os (currently linux and windows) with one group in each file (e.g. alllinux and allwindows)
- put group-information into each host (in the inventory file or in host-vars-directory)

my goal: i want to configure information per host to control if a playbook may or may not work on a host. but i don’t want to have different inventory files. i think it would be easier to have it “my way” (kind of tags per host).

is that possible? i didn’t find a way to do it.

regards,  
andre

---

<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:** [July 1, 2024, 2:56pm UTC](https://forum.ansible.com/t/build-groups-based-on-hosts-and-host-variables/38820/2 "2024-07-01T14:56:51Z")

</div>

[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/constructed\_inventory.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/constructed_inventory.html)  
^ use 3rd inventory source that can construct groups and assign group  
vars based on data from previous inventory sources.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 1, 2024, 4:05pm UTC](https://forum.ansible.com/t/build-groups-based-on-hosts-and-host-variables/38820/3 "2024-07-01T16:05:42Z")

</div>

Hi!,

---

<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:** [July 1, 2024, 4:32pm UTC](https://forum.ansible.com/t/build-groups-based-on-hosts-and-host-variables/38820/4 "2024-07-01T16:32:44Z")

</div>

The problem was posed with setting variables per host in inventory,  
you can use the constructed inventory to use those variables to create  
groups, the plays can filter only for the groups they are known to  
work with or not, i.e hosts: valid\_groups or hosts:  
normalgropus:!invalid\_for\_this\_play.

if you are getting teh variables dynamically, you can do same/similar  
with group\_by, but then you need 3 plays, 1 to gather the info, 2 to  
use the group\_by 3 the original play that processes the 'valid  
group(s)'

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 1, 2024, 7:27pm UTC](https://forum.ansible.com/t/build-groups-based-on-hosts-and-host-variables/38820/5 "2024-07-01T19:27:15Z")

</div>

> ... put group-information into each host ... per host to  
> control if a playbook may or may not work on a host

The playbook name is not available to the inventory  
plugins. The below solution provides the inventory plugin  
with the playbook name in the extra variable. The allowed  
hosts are listed in a dictionary in group\_vars.

\* Create a dictionary with the lists of the allowed hosts

> cat inventory/alllinux/01-hosts.yml

alllinux:  
&nbsp;&nbsp;hosts:  
&nbsp;&nbsp;&nbsp;&nbsp;test\_01:  
&nbsp;&nbsp;&nbsp;&nbsp;test\_02:  
&nbsp;&nbsp;&nbsp;&nbsp;test\_03:  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;play\_allow:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playbook\_01: [test\_01, test\_02]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playbook\_02: [test\_02, test\_03]

For example, the below playbook shall run on the hosts  
test\_01 and test\_02 only

> cat playbook\_01.yml

- name: playbook\_01  
&nbsp;&nbsp;hosts: my\_hosts  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Start play

\* Use the inventory plugin \*constructed\* and create the  
&nbsp;&nbsp;group \*my\_hosts\*

> cat inventory/alllinux/02-constructed.yml

plugin: ansible.builtin.constructed  
use\_extra\_vars: true  
use\_vars\_plugins: true  
compose:  
&nbsp;&nbsp;my\_hosts\_allow: play\_allow[my\_play|d('default')]  
groups:  
&nbsp;&nbsp;my\_hosts: inventory\_hostname in my\_hosts\_allow

\* Unfortunately, the name of the playbook is not available  
&nbsp;&nbsp;in the inventory plugins. Therefor, you have to provide  
&nbsp;&nbsp;it explicitly. For example, in the extra vars \*my\_play\*

> ansible-playbook -i inventory/alllinux -e my\_play=playbook\_01 playbook\_01.yml

gives abridged

ok: [test\_01] =\>  
&nbsp;&nbsp;msg: Start play  
ok: [test\_02] =\>  
&nbsp;&nbsp;msg: Start play

HTH,

---

<div class="post-metadata">

**Author:** ![astrakid](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/astrakid/32/1789_2.png) [@astrakid](https://forum.ansible.com/u/astrakid)\
**Post date:** [July 2, 2024, 8:35pm UTC](https://forum.ansible.com/t/build-groups-based-on-hosts-and-host-variables/38820/6 "2024-07-02T20:35:59Z")

</div>

thx a lot for the input. i will read and test all the proposals. THX  
regards,  
andre
