# host iterate on host rather on groups

**URL:** <https://forum.ansible.com/t/host-iterate-on-host-rather-on-groups/38379>\
**Category:** Ansible Project\
**Created:** [August 28, 2023, 8:52am UTC](https://forum.ansible.com/t/host-iterate-on-host-rather-on-groups/38379 "2023-08-28T08:52:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Monlouis](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@Marc\_Monlouis](https://forum.ansible.com/u/Marc_Monlouis)\
**Post date:** [August 28, 2023, 8:52am UTC](https://forum.ansible.com/t/host-iterate-on-host-rather-on-groups/38379/1 "2023-08-28T08:52:05Z")

</div>

Hello,

I tring to write a role that manage installation of application modules on servers.  
All modules must be installed in one time: installtion command takes list of modules to install.

My inventory file list all servers that must receive a module (module are groups) =\>

module1:  
hosts:  
host\_1.xxx.com:  
host\_2.xxx.com:

module2:  
hosts:  
host\_1.xxx.com:

module3:  
host\_2.xxx.com:

But by default playbook iterate on groups not hosts.

1. i got host\_1.xxx.com and host\_2.xxx.com for installtion module1
2. i got host\_1.xxx.com:for installtion of module2
3. i got host\_2.xxx.com for installtion of module3

But i want:

1. host\_1.xxx.com for installation of module1 and module2
2. host\_2.xxx.com for installation of module1 and module3

note: organize by group is important because some conifguration’s informations are passed to host\_1.xxx.com about host\_2.xxx.com modules and vice versa.

How can iterate on hosts and run install command with list of groups listed in inventory.

In other words i must iterate by host not by groups.

How to do that ?

Many thanks in andvance for any help

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 28, 2023, 6:52pm UTC](https://forum.ansible.com/t/host-iterate-on-host-rather-on-groups/38379/2 "2023-08-28T18:52:15Z")

</div>

It’s going to be something close to the following. No loops necessary.  
Since I don’t know what your module installation task looks like, I just  
made one up and wrapped it inside a debug task.

```auto
---
- name: Install modules on hosts matching module groups
  hosts: module_*
  tasks:
    - name: Install modules by host's groups
      ansible.builtin.debug:
        msg: |
          - name: Module install task
            module_installer:
              modules: "{{ modules }}"
      vars:
        modules: "{{ group_names | select('match', 'module_.*') }}"

```
