Warning: from fabric 2 to version 1 everything changes!

Since 2.0 all commands are execute through invoke while fab manage only the Connection.

Quick reference

Runner

(from invoke http://docs.pyinvoke.org/en/latest/api/runners.html)

Main parameters for run:

local: same as run but execute on the local host

Exit codes attributes:

Usage by examples

Autoresponder

   1 >>> from invoke import Responder
   2 >>> from fabric import Connection
   3 >>> c = Connection('host')
   4 >>> sudopass = Responder(
   5 ...     pattern=r'\[sudo\] password:',
   6 ...     response='mypassword\n',
   7 ... )
   8 >>> c.run('sudo whoami', pty=True, watchers=[sudopass])
   9 [sudo] password:
  10 root
  11 <Result cmd='sudo whoami' exited=0>
  12 

Sudo

   1 >>> import getpass
   2 >>> from fabric import Connection, Config
   3 >>> sudo_pass = getpass.getpass("What's your sudo password?")
   4 What's your sudo password?
   5 >>> config = Config(overrides={'sudo': {'password': sudo_pass}})
   6 >>> c = Connection('db1', config=config)
   7 >>> c.sudo('whoami', hide='stderr')
   8 root
   9 <Result cmd="...whoami" exited=0>
  10 >>> c.sudo('useradd mydbuser')
  11 <Result cmd="...useradd mydbuser" exited=0>
  12 >>> c.run('id -u mydbuser')
  13 1001
  14 <Result cmd='id -u mydbuser' exited=0>
  15 

File transfer

Via put e pull:

   1 result = Connection('web1').put('myfiles.tgz', remote='/opt/mydata/')
   2 print("Uploaded {0.local} to {0.remote}".format(result))

Documentation