check if host is part of group

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:

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

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”

See http://ansibleworks.com/docs/intro_patterns.html

almost always better to do as mentioned above but if you REAAAAAAALLLLYYYY need it:

when: inventory_hostname in groups[‘frontendcluster1’]

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

when: inventory_hostname in groups[‘frontendcluster1’]

Also more easily written as:

when: inventory_hostname in groups.frontendcluster1

Awesome tip! Much better than the quotation hell that I've been using :slight_smile:

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