fabric/fabric — pull request #824 by flupke (2013-01-16 14:57:10+00:00)
Title: Add a 'role' attribute to env.host_string

This allows to know the 'current' role in a task.

Here is an example use case:

```python
from fabric.api import env, sudo
from fabric.decorators import roles

env.roledefs = {
'web': ['webhost.example.com'],
'worker': ['webhost.example.com', 'workers.example.com'],
}

def prod():
env.prog_name = {
'web': 'webapp',
'worker': 'webapp-worker',
}

def preprod():
env.prog_name = {
'web': 'webapp-preprod',
'worker': 'webapp-worker-preprod',
}

@roles('web', 'worker')
def restart():
prog_name = env.prog_name[env.host_string.role]
sudo('supervisorctl restart %s' % prog_name)
```

If env.host_string had no 'role' attribute we would have to guess it by searching env.host_string in env.roledefs. It would not work in this case because 'webhost.example.com' is present in both 'web' and 'worker' roles.

I included a test in the commit, let me know if you accept the pull request and I will write documentation too.