How to use additional env.roledefs keys in Fabric?

Like this question, I tried to access additional keys in env.roledefs that Fabric provides:

from fabric.api import env

env.roledefs = {
    'web': {
        'hosts': ['www1', 'www2', 'www3'],
        'foo': 'bar'
    },
    'dns': {
        'hosts': ['ns1', 'ns2'],
        'foo': 'baz'
    }
}

However , the documentation does not explain how to access these foo keys. Super hulks seem to need access to them through:

env.roledefs[env.effective_roles[0]]['foo']

Is there an easy way to access these optional keys provided by Fabric?

+4
source share
2 answers

The fabric should make it causes on the env, which you can refer to: env.foo; on any roledef that has this dict in it.

0
source

, 1.11.1 ( , № 1276 , ).

, env , . , :

from functools import wraps

def apply_role(f):
    "Decorator to apply keys in effective roledef to current env."
    @wraps(f)
    def wrapper(*args, **kwargs):
        if env.effective_roles:
            for k, v in env.roledefs[env.effective_roles[0]].items():
                if k in env and isinstance(env[k], list) and isinstance(v, list):
                    env[k].extend(v)
                elif k in env and isinstance(env[k], list):
                    env[k].append(v)
                else:
                    env[k] = v
        return f(*args, **kwargs)
    return wrapper

, roledef:

@apply_role
def mytask():
    ...
0

All Articles