# Selecting hosts based on variable

**URL:** <https://forum.ansible.com/t/selecting-hosts-based-on-variable/14725>\
**Category:** Ansible Project\
**Created:** [March 7, 2014, 3:12pm UTC](https://forum.ansible.com/t/selecting-hosts-based-on-variable/14725 "2014-03-07T15:12:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francois\_Legrand](https://avatars.discourse-cdn.com/v4/letter/f/5f8ce5/32.png) [@Francois\_Legrand](https://forum.ansible.com/u/Francois_Legrand)\
**Post date:** [March 7, 2014, 3:12pm UTC](https://forum.ansible.com/t/selecting-hosts-based-on-variable/14725/1 "2014-03-07T15:12:23Z")

</div>

Hi all,  
Is there a way to select hosts in inventory based on a variable ?  
More specifically, I have a host file like :

[webservers]  
[web1.example.com](http://web1.example.com) location=room1  
[web2.example.com](http://web2.example.com) location=room1  
[web3.example.com](http://web3.example.com) location=room22

[dbservers]  
[db1.example.com](http://db1.example.com) location=room22  
[db2.example.com](http://db2.example.com) location=room1  
[db3.example.com](http://db3.example.com)

I would like to be able to select all computers with location=room1 (basically I want to shutdown all these machines because we have to cut the power in this room).  
Thus a commande like :

ansible all --list-hosts --filter=location=room1

would be great.

Il could do that more or less in a playbook, but I also use ansible in shell scripts to run specific commands.

F.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [March 7, 2014, 3:25pm UTC](https://forum.ansible.com/t/selecting-hosts-based-on-variable/14725/2 "2014-03-07T15:25:16Z")

</div>

in play:  
hosts: “{{mytarget}}”

on command line:

-e ‘mytarget=myhost:myhost2’

though i you could also use --limit ‘room1’ (and have a room1 group for those hosts) or --limit ‘myhost:myhost2’

---

<div class="post-metadata">

**Author:** ![Francois\_Legrand](https://avatars.discourse-cdn.com/v4/letter/f/5f8ce5/32.png) [@Francois\_Legrand](https://forum.ansible.com/u/Francois_Legrand)\
**Post date:** [March 7, 2014, 4:29pm UTC](https://forum.ansible.com/t/selecting-hosts-based-on-variable/14725/3 "2014-03-07T16:29:51Z")

</div>

Thanks for your answer. I know that I could do another group based on location… but the point is that you get very fast a lot of groups… thus when you add/remove/update a new computer you have to do it at many places in the host file so it’s easy to forget to update a group !

---

<div class="post-metadata">

**Author:** ![James\_Martin2](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@James\_Martin2](https://forum.ansible.com/u/James_Martin2)\
**Post date:** [March 7, 2014, 4:42pm UTC](https://forum.ansible.com/t/selecting-hosts-based-on-variable/14725/4 "2014-03-07T16:42:24Z")

</div>

You can create dynamic groups by using the group\_by module:

- name: Create groups based on location  
group\_by: key={{ location }}

then use something to similar to what Brian had said earlier in the play:

ansible-playbook -e ‘mylocation=room1’

playbook should contain:

hosts: “{{ mylocation }}”

- James

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [March 8, 2014, 1:50am UTC](https://forum.ansible.com/t/selecting-hosts-based-on-variable/14725/5 "2014-03-08T01:50:00Z")

</div>

I have to say that group\_by is a very useful module if you are trying to keep your inventory minimal, but want to be able to sleect servers based on common characteristics (networks, operating systems, hardware types and so on. I suspect that you could combine group\_by and local facts to easily achieve what you want…

Create an /etc/ansible/local/facts.d with a location fact in it and then you can use that in your group\_by quite happily…

Adam
