Conditionally executing tasks if host is in a given role?

Say I have a backup role that sets up my preferred backup solution. Also say I have a separate Docker role that installs Docker on a given host. If a Docker host is in the backup role, I want to add /var/lib/docker to the list of directories to be backed up.

The only way I can think to do this is creating a separate xBackup role that backs up the given role. Is there a better way? I only need to execute a single task, typically a lineinfile appending a new backup directory. It just seems excessive to create a new role for one task, or to have to scatter the logic for a given set of tasks across several locations.

Thanks.

Easiest way to do is assign a group, which may contain other groups as child groups.

Then just:

hosts: backup_servers
roles:

  • back_me_up

Of course if you don’t like that, you can just you can do just:

when: inventory_hostname in groups.foo

But it’s a bit of an anti-pattern.

Roles contain tasks and behaviors, so it’s a bit weird for them to need to be cognizant of their names.