Skip to content

Commit a779c6f

Browse files
committed
Add support for dict style roledefs
1 parent d243ebb commit a779c6f

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ Neilen Marais
5757
Rory Geoghegan
5858
Alexey Diyan
5959
Kamil Kisiel
60+
Jonas Lundberg

fabric/task_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def merge(hosts, roles, exclude, roledefs):
5252
role_hosts = []
5353
for role in roles:
5454
value = roledefs[role]
55+
# Handle dict style roledefs
56+
if isinstance(value, dict):
57+
value = value['hosts']
5558
# Handle "lazy" roles (callables)
5659
if callable(value):
5760
value = value()

sites/docs/usage/execution.rst

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,29 @@ loading other fabfiles which also modify it, of course)::
133133
'dns': ['ns1', 'ns2']
134134
}
135135

136-
In addition to list/iterable object types, the values in ``env.roledefs`` may
137-
be callables, and will thus be called when looked up when tasks are run instead
138-
of at module load time. (For example, you could connect to remote servers
139-
to obtain role definitions, and not worry about causing delays at fabfile load
140-
time when calling e.g. ``fab --list``.)
136+
Role definitions are not necessary configuration of hosts only, but could hold
137+
other role specific settings of your choice. This is achieved by defining the
138+
roles as dicts and host strings under a ``hosts`` key::
139+
140+
from fabric.api import env
141+
142+
env.roledefs = {
143+
'web': {
144+
'hosts': ['www1', 'www2', 'www3'],
145+
'foo': 'bar'
146+
},
147+
'dns': {
148+
'hosts': ['ns1', 'ns2'],
149+
'foo': 'baz'
150+
}
151+
}
152+
153+
In addition to list/iterable object types, the values in ``env.roledefs``
154+
(or value of ``hosts`` key in dict style definition) may be callables, and will
155+
thus be called when looked up when tasks are run instead of at module load
156+
time. (For example, you could connect to remote servers to obtain role
157+
definitions, and not worry about causing delays at fabfile load time when
158+
calling e.g. ``fab --list``.)
141159

142160
Use of roles is not required in any way -- it's simply a convenience in
143161
situations where you have common groupings of servers.

sites/www/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Changelog
33
=========
44

5+
* :feature:`1098` Add support for dict style roledefs.
6+
Thanks to Jonas Lundberg, `@lundberg`
57
* :release:`1.9.1 <2014-08-06>`
68
* :release:`1.8.5 <2014-08-06>`
79
* :release:`1.7.5 <2014-08-06>`

tests/test_main.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,21 @@ def command():
271271
eq_hosts(command, ['a', 'b'], env={'roledefs': spaced_roles})
272272

273273

274+
dict_roles = {
275+
'r1': {'hosts': ['a', 'b']},
276+
'r2': ['b', 'c'],
277+
}
278+
279+
def test_hosts_in_role_dict():
280+
"""
281+
Make sure hosts defined in env.roles are cleaned of extra spaces
282+
"""
283+
@roles('r1')
284+
def command():
285+
pass
286+
eq_hosts(command, ['a', 'b'], env={'roledefs': dict_roles})
287+
288+
274289
def test_hosts_decorator_expands_single_iterable():
275290
"""
276291
@hosts(iterable) should behave like @hosts(*iterable)

0 commit comments

Comments
 (0)