Run task only if server belong to some group.

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.

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

“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.

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.

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

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”

Hi Michael,

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

Marcin

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