# Hosts with multiple roles

**URL:** <https://forum.ansible.com/t/hosts-with-multiple-roles/17241>\
**Category:** Ansible Project\
**Created:** [January 12, 2015, 1:33pm UTC](https://forum.ansible.com/t/hosts-with-multiple-roles/17241 "2015-01-12T13:33:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dirk\_Olmes](https://avatars.discourse-cdn.com/v4/letter/d/b38774/32.png) [@Dirk\_Olmes](https://forum.ansible.com/u/Dirk_Olmes)\
**Post date:** [January 12, 2015, 1:33pm UTC](https://forum.ansible.com/t/hosts-with-multiple-roles/17241/1 "2015-01-12T13:33:25Z")

</div>

Hi,

I’m having problems with the ansible inventory, filters and hosts in multiple roles. Let’s assume I have two customers, each with a machine that can be a http server and/or a samba server.This is my inventory:

[customer1]  
[host1.customer1.com](http://host1.customer1.com)

[customer2]  
[host1.customer2.com](http://host1.customer2.com)  
[host2.customer2.com](http://host2.customer2.com)

[http-servers]  
[host1.customer1.com](http://host1.customer1.com)  
[host1.customer2.com](http://host1.customer2.com)

[samba-servers]  
[host1.customer1.com](http://host1.customer1.com)  
[host2.customer1.com](http://host2.customer1.com)

As you can see, customer1 only has a single machine that fulfills both roles and customer2 has a separate machine for each role.

Now I’m trying to run the playbook with filters:

% ansible-playbook -i ./hosts --list-hosts --limit customer1 ./site.yml

playbook: ./site.yml

play #1 (customer1): host count=1  
[host1.customer1.com](http://host1.customer1.com)

play #3 (http-servers): host count=1  
[host1.customer1.com](http://host1.customer1.com)

play #4 (samba-servers): host count=1  
[host1.customer1.com](http://host1.customer1.com)

Why on earth do I get play #3 and #4? I specifically asked for customer1 which only has one entry.

The same goes for role-based groups:

% ansible-playbook -i ./hosts --limit http-servers --list-hosts ./site.yml

playbook: ./site.yml

play #1 (customer1): host count=1  
[host1.customer1.com](http://host1.customer1.com)

play #2 (customer2): host count=1  
[host1.customer2.com](http://host1.customer2.com)

play #3 (http-servers): host count=2  
[host1.customer1.com](http://host1.customer1.com)  
[host1.customer2.com](http://host1.customer2.com)

play #4 (samba-servers): host count=1  
[host1.customer1.com](http://host1.customer1.com)

What’s going on here?

I did read the documentation on inventory, groups and patterns but that page only has examples for hosts in single roles. What am I doing wrong?

-dirk

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 13, 2015, 1:57pm UTC](https://forum.ansible.com/t/hosts-with-multiple-roles/17241/2 "2015-01-13T13:57:53Z")

</div>

--limit does a intersection with existing - hosts: clauses, it looks like

play1: - hosts: customer1  
play2: - hosts: customer2  
play3: - hosts: http-servers  
play4: - hosts: samba-servers

when you do --limit customer1 you should get play1, 3 and 4, as the  
intersection of customer1 and customer1 is 1 host, customer2 and  
customer1 is 0 hosts, http-servers and customer1 is 1 host, etc.

If you want to ONLY run specific plays I suggest you look at tags.
