check if host is part of group

24,753 views
Skip to first unread message

Khuong Dinh Pham

unread,
Nov 27, 2013, 2:24:51 PM11/27/13
to ansible...@googlegroups.com
Hi

I have this setup in my hosts

server1 ansible_ssh_host=server1.xxx.com
server2 ansible_ssh_host=server2.yyy.com
server3 ansible_ssh_host=server3.yyy.com

[frontendservers]
server1
server2
server3

[frontendcluster1]
server1
server2

My playbook:
---
  hosts: frontendservers
  tasks:
    - name: testing
      command: copy some file
      when: $host part of 'frontendcluster1' group

I only want the command to be executed for server1 and server2? How can I achieve that?

Thx for advance.

Michel Blanc

unread,
Nov 27, 2013, 2:43:49 PM11/27/13
to ansible...@googlegroups.com
Hi Khuong,

Just create a group for these hosts, and change your hosts: line in the
playbook :

[onlythose]
server1
server2



- hosts: onlythose
tasks:
- name: testing
command: copy some file


Cheers,

M
--
Michel Blanc
{ :github => "@leucos", :twitter => "@b9m", :gpg => "0X24B35C22" }

Michael DeHaan

unread,
Nov 27, 2013, 2:44:11 PM11/27/13
to ansible...@googlegroups.com
Super easy!

- hosts:  frontendcluster1 
  tasks:
    ...

You can have multiple plays in a playbook file.

For more complex things you can also say stuff like:

- hosts: group1:&group2

Which means "must be in both"




--
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.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Brian Coca

unread,
Nov 27, 2013, 2:46:31 PM11/27/13
to ansible...@googlegroups.com
almost always better to do as mentioned above but if you REAAAAAAALLLLYYYY need it:

    when: inventory_hostname in groups['frontendcluster1']

Khuong Dinh Pham

unread,
Nov 27, 2013, 3:00:18 PM11/27/13
to ansible...@googlegroups.com
Hi

Thx. Just what I need.

It's because I have one playbook for my frontendservers hosts. This playbook include roles but only some of the tasks should be deploy on a subset of the frontendservers. i.e. part of frontendcluster01 group

Michael DeHaan

unread,
Nov 27, 2013, 3:02:48 PM11/27/13
to ansible...@googlegroups.com
    when: inventory_hostname in groups['frontendcluster1']

Also more easily written as:

when: inventory_hostname in groups.frontendcluster1




--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Kahlil Hodgson

unread,
Nov 27, 2013, 5:17:53 PM11/27/13
to ansible...@googlegroups.com
Awesome tip! Much better than the quotation hell that I've been using :-)

when "'frontendcluster1' in group_names"


K

Kahlil (Kal) Hodgson GPG: C9A02289
Head of Technology (m) +61 (0) 4 2573 0382
DealMax Pty Ltd (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing. You must remember that
the parts you are reassembling were disassembled by you. Therefore,
if you can't get them together again, there must be a reason. By all
means, do not use a hammer." -- IBM maintenance manual, 1925
Reply all
Reply to author
Forward
0 new messages