class fabric.tasks.Task(alias=None, aliases=None, default=False, *args, **kwargs)

Abstract base class for objects wishing to be picked up as Fabric tasks.

Instances of subclasses will be treated as valid tasks when present in fabfiles loaded by the fab tool.

For details on how to implement and use Task subclasses, please see the usage documentation on new-style tasks.

New in version 1.1.

get_hosts(arg_hosts, arg_roles, arg_exclude_hosts, env=None)

Return the host list the given task should be using.

See How host lists are constructed for detailed documentation on how host lists are set.

fabric.tasks.execute(task, *args, **kwargs)

Execute task (callable or name), honoring host/role decorators, etc.

task may be an actual callable object, or it may be a registered task name, which is used to look up a callable just as if the name had been given on the command line (including namespaced tasks, e.g. "deploy.migrate".

The task will then be executed once per host in its host list, which is (again) assembled in the same manner as CLI-specified tasks: drawing from -H, env.hosts, the hosts or roles decorators, and so forth.

host, hosts, role, roles and exclude_hosts kwargs will be stripped out of the final call, and used to set the task’s host list, as if they had been specified on the command line like e.g. fab taskname:host=hostname.

Any other arguments or keyword arguments will be passed verbatim into task when it is called, so execute(mytask, 'arg1', kwarg1='value') will (once per host) invoke mytask('arg1', kwarg1='value').

This function returns a dictionary mapping host strings to the given task’s return value for that host’s execution run. For example, execute(foo, hosts=['a', 'b']) might return {'a': None, 'b': 'bar'} if foo returned nothing on host a but returned 'bar' on host b.

In situations where a task execution fails for a given host but overall progress does not abort (such as when env.skip_bad_hosts is True) the return value for that host will be the error object or message.

See also

The execute usage docs, for an expanded explanation and some examples.

New in version 1.3.

Changed in version 1.4: Added the return value mapping; previously this function had no defined return value.

Previous topic


Next topic


This Page