# Conditionally executing tasks if host is in a given role?

**URL:** <https://forum.ansible.com/t/conditionally-executing-tasks-if-host-is-in-a-given-role/13327>\
**Category:** Ansible Project\
**Created:** [August 18, 2013, 4:20pm UTC](https://forum.ansible.com/t/conditionally-executing-tasks-if-host-is-in-a-given-role/13327 "2013-08-18T16:20:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nolan\_Darilek](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@Nolan\_Darilek](https://forum.ansible.com/u/Nolan_Darilek)\
**Post date:** [August 18, 2013, 4:20pm UTC](https://forum.ansible.com/t/conditionally-executing-tasks-if-host-is-in-a-given-role/13327/1 "2013-08-18T16:20:09Z")

</div>

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.

---

<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:** [August 18, 2013, 7:32pm UTC](https://forum.ansible.com/t/conditionally-executing-tasks-if-host-is-in-a-given-role/13327/2 "2013-08-18T19:32:43Z")

</div>

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.
