# Run task only if server belong to some group.

**URL:** <https://forum.ansible.com/t/run-task-only-if-server-belong-to-some-group/13561>\
**Category:** Ansible Project\
**Created:** [September 26, 2013, 10:58am UTC](https://forum.ansible.com/t/run-task-only-if-server-belong-to-some-group/13561 "2013-09-26T10:58:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [September 26, 2013, 10:58am UTC](https://forum.ansible.com/t/run-task-only-if-server-belong-to-some-group/13561/1 "2013-09-26T10:58:38Z")

</div>

Hi,

I have question whether is possible to run task only if server belong to specific group?

— Quick scenario —  
Lets say that on Inventory I have:  
[targets]  
srv01  
srv02

[webservers]  
srv01

Now I would like to play ‘common’ roles with tasks following (pseudo code):

- name: Do something  
action: Ansible - action  
when: server is in webservers group.

I know that is possible run another play which runs only on webserves but not quite sure whether this suits to my issue:  
— Long scenario —  
I have to cofigure a lot servers belongs to specific groups (web servers / db servers) and munin plugins have some common plugins / and depends of group (www / db).

- When one is installing munin-node from RPM (RH based systems) a lot of munin-plugins are created:
- Now common role from ansible - removes not requried plugins - restarting munin-node
- Then some additional plugins should be configured for server belongs to group www/db

Do I would like to avoid situation that: Common plugin removes not required plugins from munin-node (including those for www - like apache), and then another play install this back.  
I woud really achive somethign like that:

- name: Remove not required munin plugins  
action:  
when: Server not belong to group WWW

…

- name: Add plugins for WWW  
action:  
when: Server belong to group WWW

I look forward to hearing from you.  
Best regards.

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [September 26, 2013, 11:41am UTC](https://forum.ansible.com/t/run-task-only-if-server-belong-to-some-group/13561/2 "2013-09-26T11:41:27Z")

</div>

Hi Marcin,

You could do

when: ‘webserver’ in group\_names

This would make the task run if the current host belongs to webserver group.

when: ‘webserver’ not in group\_names

would run the task if the current host is not on the webserver group.

Hope this helps.

Regards,  
Benno

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 26, 2013, 12:30pm UTC](https://forum.ansible.com/t/run-task-only-if-server-belong-to-some-group/13561/3 "2013-09-26T12:30:34Z")

</div>

“I know that is possible run another play which runs only on webserves but not quite sure whether this suits to my issue:”

Actually, it does.

hosts: maingroup:&othergroup

Means “all hosts in maingroup that are also in othergroup” (maingroup:othergroup would be an “OR”)

This is cleaner than the when statement and will produce better output, because it won’t show ‘skipped’ for all the other tasks.

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [September 26, 2013, 5:12pm UTC](https://forum.ansible.com/t/run-task-only-if-server-belong-to-some-group/13561/4 "2013-09-26T17:12:11Z")

</div>

Hi,

Thank you for help.  
Both answers are great:

1. Is really nice that ansible supports condition for task based on this whether server belong to group or not.
2. And I agree that is nicer to split tasks on seperate roles to avoid a lot of ‘skipped’ tasks.

One more time - thank you for help.

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [October 9, 2013, 3:02pm UTC](https://forum.ansible.com/t/run-task-only-if-server-belong-to-some-group/13561/5 "2013-10-09T15:02:34Z")

</div>

Hi,

Are you sure that this is working - just tested on ansible 1.3 and I have error:

ERROR: Syntax Error while loading YAML script, /src/ansible/roles/mysql-munin/tasks/main.yml  
Note: The error may actually appear before this position: line 34, column 27

- mysql\_seconds\_behind\_master  
when: ‘dbslave’ in group\_names  
^

I created special role: mysql-munin  
There are 3 tasks which should be run - 2 of them on DB master - 1 on DB slave.  
But so far I am struggeling to run task only on server which belongs to specific group.

Tasks list:  
play #3 (Database | Configurtion for database servers):  
MySQL-munin | Create Munin configuration - Template # Should run on DB servers  
MySQL-munin | Link Official munin plugins # Should run on DB servers  
MySQL-munin | Link Custom munin plugins # Should run ONLY on Slave DB server

Task: #3

- name: MySQL-munin | Link Custom munin plugins  
file: src=/opt/munin/lib/plugins/{{ item }}  
dest=/etc/munin/plugins/{{ item }}  
state=link  
with\_items:
- mysql\_seconds\_behind\_master  
notify:
- restart munin-node  
tags:
- munin\_conf\_plugin\_mysql

# Adding this line

when: ‘dbslave’ in group\_names

- breaks formating for yaml
- Displaying error which I’ve described above.

Best regards,  
Marcin Praczko

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 9, 2013, 3:07pm UTC](https://forum.ansible.com/t/run-task-only-if-server-belong-to-some-group/13561/6 "2013-10-09T15:07:58Z")

</div>

Yep, as this says, this is a YAML syntax error.

Since you started the line with a quote, YAML was expecting the whole line to be quoted.

This can be fixed by changing

when: ‘dbslave’ in group\_names

To:

when: “‘dbslave’ in group\_names”

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [October 9, 2013, 3:47pm UTC](https://forum.ansible.com/t/run-task-only-if-server-belong-to-some-group/13561/7 "2013-10-09T15:47:21Z")

</div>

Hi Michael,

This is working now as expected - thank you very much for help.

Marcin

---

<div class="post-metadata">

**Author:** ![Dhruvin\_Shah](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@Dhruvin\_Shah](https://forum.ansible.com/u/Dhruvin_Shah)\
**Post date:** [January 9, 2015, 10:31pm UTC](https://forum.ansible.com/t/run-task-only-if-server-belong-to-some-group/13561/8 "2015-01-09T22:31:36Z")

</div>

Surprisingly simple and powerful.  
It works like a charm. Thanks Michael and everyone.
