fab-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Fab-user] Different roledefs per task


From: Todd DeLuca
Subject: Re: [Fab-user] Different roledefs per task
Date: Tue, 18 Dec 2012 14:05:23 -0500

This *might* answer your question.  I recommend providing more example code or output when posting to reduce ambiguity.


The following fabfile.py allows you to define some tasks to run on "master" hosts and some on "slave" hosts.  You can then vary which hosts apply to these roles by using "prod" or "stage" to define env.roledefs when fabfile.py is imported.  Below I demonstrate using different command line syntaxes to accomplish the same thing, either `-R <role>` or `--set de=<role>` or using a task `<role>`.  I've used all 3 of these syntaxes in various fabfiles, depending on my mood the day I wrote them.  Personally I would avoid using `-R` to define `env.roledefs`, since they already have a different semantic association.

The demo fabfile.py:

from fabric.api import *

# Using -R to set roledefs
if 'prod' in env.roles:
    env.roledefs = {'master': ['host1'], 'slave': ['host2', 'host3']}
elif 'stage' in env.roles:
    env.roledefs = {'master': ['host4'], 'slave': ['host5', 'host6']}


# Using --set to set roledefs
if env.get('de') == 'prod':
    env.roledefs = {'master': ['host1'], 'slave': ['host2', 'host3']}
elif env.get('de') == 'stage':
    env.roledefs = {'master': ['host4'], 'slave': ['host5', 'host6']}


# Using tasks to set roledefs
@task
def prod():
    env.roledefs = {'master': ['host1'], 'slave': ['host2', 'host3']}


@task
def stage():
    env.roledefs = {'master': ['host4'], 'slave': ['host5', 'host6']}


# Roles to be executed on different sets of hosts
@roles('master')
@task
def do_master():
    print env.host_string

@roles('slave')
@task
def do_slave():
    print env.host_string



Here you can see that the effect is the same no matter which command line syntax you use (though your edge cases might vary):

$ fab --set de=prod do_master do_slave
[host1] Executing task 'do_master'
host1
[host2] Executing task 'do_slave'
host2
[host3] Executing task 'do_slave'
host3

Done.
$ fab --set de=stage do_master do_slave
[host4] Executing task 'do_master'
host4
[host5] Executing task 'do_slave'
host5
[host6] Executing task 'do_slave'
host6

Done.
$ fab -R prod do_master do_slave
[host1] Executing task 'do_master'
host1
[host2] Executing task 'do_slave'
host2
[host3] Executing task 'do_slave'
host3

Done.
$ fab -R stage do_master do_slave
[host4] Executing task 'do_master'
host4
[host5] Executing task 'do_slave'
host5
[host6] Executing task 'do_slave'
host6

Done.
$ fab prod do_master do_slave
[host1] Executing task 'do_master'
host1
[host2] Executing task 'do_slave'
host2
[host3] Executing task 'do_slave'
host3

Done.
$ fab stage do_master do_slave
[host4] Executing task 'do_master'
host4
[host5] Executing task 'do_slave'
host5
[host6] Executing task 'do_slave'
host6

Done.

Hope that helps,
Todd


On Tue, Dec 18, 2012 at 11:50 AM, Francisco José Marques Vieira <address@hidden> wrote:
Hi everyone! This is my first post on this mailing list, so please excuse me if I don't follow some rule or convention of yours.
My name is Francisco and I started using Fabric a couple weeks ago, and so far I'm happy with what I've seen.
Nevertheless, there always comes a time when even the best tool fails to accomplish some task, and that's why I'm here.

The thing is, I have two different ways to group my hosts: one is by role (master or slave), the other is by staging or production, i.e. there is a master and many slaves in staging, and another master and many slaves in production.
I have tasks that I need to run only on the master and others to run in the slaves. Also, I want to be able to run tasks only on staging or only on production.

The way I've been handling this is by having two different roledefs, one for master hosts and another for slaves. Each roledef has two roles, staging and production, meaning I can do something like "fab deploy -R staging" and this makes sure I won't be installing things into production by mistake.
These roledefs are defined in a settings file and each fabfile of mine imports one of them and sets env.roledefs to it.

This works as long as all the tasks in each file are to be executed in hosts with the same role, either master or slaves. And so far that's what happened naturally.
Now I have a fabfile which has some tasks that should run in the master host and another task that should run in the slaves, but I have no way of saying to fabric "please use a different roledef for this task", since by the time my code inside the task starts to execute, the roledefs have already been processed.
Is there any way around this which does not require me to separate the tasks in two different files?

Francisco Vieira


P.S: Sorry for the such a big post for such a trivial matter...

_______________________________________________
Fab-user mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/fab-user



--
Todd DeLuca
http://todddeluca.com
http://wall.hms.harvard.edu/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]