Ansible: Run task if server belongs to a group


What’s the sane way run a task only if the host belongs to one or more groups?

Currently, I’m using a boolean within the relevant group, e.g.:

Inventory file


What’s the right condition in the when clause to check whether the current host belongs to the db_servers group?


Run task when a host server is a member of a specific group

Ansible contains special or magic variablesone of the most common is group_names which is a list (array) of all the groups the current host is in.

The above Ansible task will only run if the host is a member of the db_servers group.

Leave a Reply