# How can I run an imported Ansible playbook for each unique value in a set of host vars?

**URL:** <https://forum.ansible.com/t/how-can-i-run-an-imported-ansible-playbook-for-each-unique-value-in-a-set-of-host-vars/29344>\
**Category:** Ansible Project\
**Created:** [December 31, 2018, 11:01pm UTC](https://forum.ansible.com/t/how-can-i-run-an-imported-ansible-playbook-for-each-unique-value-in-a-set-of-host-vars/29344 "2018-12-31T23:01:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ewan](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@Ewan](https://forum.ansible.com/u/Ewan)\
**Post date:** [December 31, 2018, 11:01pm UTC](https://forum.ansible.com/t/how-can-i-run-an-imported-ansible-playbook-for-each-unique-value-in-a-set-of-host-vars/29344/1 "2018-12-31T23:01:38Z")

</div>

I’ve got a playbook which needs to run against my entire inventory, with a list of hostnames as an extra variable (`target_hosts`).

The hosts in `target_hosts` all have a `group_id` hostvar defined on them. I use the whole inventory because some ancillary hosts which correspond to the `group_id` var need per-group configuration to match in one section.

There will often be multiple `group_id` values associated with the hosts in the `target_hosts` list. I need to select the correct inventory group of ancillary hosts and import/run a playbook to configure both sets of servers partway through the main playbook.

This is what I currently do:

include\_playbook: group-configure.yaml  
vars:  
src\_hosts: “group-{{ group\_id }}-ancillary-1”  
dest\_hosts: “{{ target\_hosts }}”

I currently have to manually separate the `target_hosts` by `group_id` manually, then run the main playbook once for each. This has tons of unnecessary overhead.

What I really want to execute is this:

for each group of hosts from `target_hosts` with the same `group_id` hostvar:  
import and run group-configure.yaml with:  
src\_hosts: “ancillary-{{ group\_id }}”  
target\_hosts: restricted to those with that value of `group_id`’

How can I do this? If the current way this is structured won’t work, what’s the best alternative approach?

Thanks for any help!

Ewan

---

<div class="post-metadata">

**Author:** ![Matthieu\_Ducorps](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@Matthieu\_Ducorps](https://forum.ansible.com/u/Matthieu_Ducorps)\
**Post date:** [January 1, 2019, 8:46am UTC](https://forum.ansible.com/t/how-can-i-run-an-imported-ansible-playbook-for-each-unique-value-in-a-set-of-host-vars/29344/2 "2019-01-01T08:46:29Z")

</div>

The easiest way to target a group of host is to sepcify it when you trigger call your ansible-playbook command, to have it working properly you need to target all hosts in the playbook itself,

# group-configure.yaml

- name: group-configure  
hosts: all

tasks:  
…

# /group-configure.yaml

Then run the playbook on the group of your inventory

ansible-playbook -l target\_hosts group-configure.yaml

hope that help.

Matth
