Run task only if server belong to some group.

12,266 views
Skip to first unread message

Marcin Prączko

unread,
Sep 26, 2013, 6:58:38 AM9/26/13
to ansible...@googlegroups.com
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.

benno joy

unread,
Sep 26, 2013, 7:41:27 AM9/26/13
to ansible...@googlegroups.com
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




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

unread,
Sep 26, 2013, 8:30:34 AM9/26/13
to ansible...@googlegroups.com
"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.



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

Marcin Prączko

unread,
Sep 26, 2013, 1:12:11 PM9/26/13
to ansible...@googlegroups.com
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.

Marcin Prączko

unread,
Oct 9, 2013, 11:02:34 AM10/9/13
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
Oct 9, 2013, 11:07:58 AM10/9/13
to ansible...@googlegroups.com
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"




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

Marcin Prączko

unread,
Oct 9, 2013, 11:47:21 AM10/9/13
to ansible...@googlegroups.com
Hi Michael,

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

Marcin

Dhruvin Shah

unread,
Jan 9, 2015, 5:31:36 PM1/9/15
to ansible...@googlegroups.com
Surprisingly simple and powerful.
It works like a charm. Thanks Michael and everyone.
Reply all
Reply to author
Forward
0 new messages