Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fabric env.roledefs not acting as expected

Tags:

python

fabric

On the fabric website, this example is given:

from fabric.api import env

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

As far as I can tell from the documentation, this setup should give the env dict key 'foo' the value 'bar' when executing on hosts 'www1', 'www2', 'www3'. I cannot get this behavior, though fabric does correctly determine hosts. An example fabfile:

env.foo = 'WRONG'
@task()
def set_role():
    env.roles.append('web')

@task()
def print_foo():
    print env.foo

The example command:

fab set_role print_foo

Unexpected output:

[www1] Executing task 'print_foo'
WRONG
[www2] Executing task 'print_foo'
WRONG
[www3] Executing task 'print_foo'
WRONG

Done.

Am I misunderstanding the purpose of this? How can I make it so that one server sees a different value for a key then another without too much trouble?

I am using fabric 1.10.0

like image 309
Aaron Schif Avatar asked Sep 12 '14 17:09

Aaron Schif


1 Answers

Just an answer to this:

env.roledefs = {
    'prod': {
        'hosts':['server1','server2'],
        'path':'/opt/prod'
        },
    'stag': {
        'hosts':['server3','server4'],
        'path':'/opt/stag'
        }
}

@roles('prod')
def runa():
    role = env.effective_roles[0]
    print env.roledefs[role]['path']
like image 88
Andreas Avatar answered Oct 14 '22 09:10

Andreas