Selecting hosts based on variable

3,797 views
Skip to first unread message

Francois Legrand

unread,
Mar 7, 2014, 10:12:23 AM3/7/14
to ansible...@googlegroups.com
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 location=room1
web2.example.com location=room1
web3.example.com location=room22

[dbservers]
db1.example.com location=room22
db2.example.com location=room1
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.

Brian Coca

unread,
Mar 7, 2014, 10:25:16 AM3/7/14
to ansible...@googlegroups.com
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'

Francois Legrand

unread,
Mar 7, 2014, 11:29:51 AM3/7/14
to ansible...@googlegroups.com
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 !

James Martin

unread,
Mar 7, 2014, 11:42:24 AM3/7/14
to ansible...@googlegroups.com
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 <all your other options> -e 'mylocation=room1'


playbook should contain:

hosts: "{{ mylocation }}"

- James


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/36459bd6-292b-428c-a186-31f4310135e3%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Adam Morris

unread,
Mar 7, 2014, 8:50:00 PM3/7/14
to ansible...@googlegroups.com


On Friday, March 7, 2014 8:42:24 AM UTC-8, James Martin wrote:
You can create dynamic groups by using the group_by module:

- name: Create groups based on location
  group_by: key={{ location }}


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
Reply all
Reply to author
Forward
0 new messages