Differences between revisions 1 and 6 (spanning 5 versions)
Revision 1 as of 2018-06-16 22:12:54
Size: 252
Editor: risca
Comment:
Revision 6 as of 2018-07-25 09:03:41
Size: 1736
Editor: risca
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
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}}}:
 * command
 * shell
 * warn
 * hide: [out|err|both|None]
 * pty
 * echo: show what will be execute
 * env: inject variables
 * replace_env: it will be the only environment
 * watchers: automatic replay to stdout!
 * echo_stdin
 * respond: write in response of watcher

{{{local}}}: same as {{{run}}} but execute on the local host

Exit codes attributes:
 * ok|failed
 * exited
 * return_code
 * stdout
 * connection.host
= Usage by examples =
== Autoresponder ==
Line 4: Line 35:
from fabric.api import abort, cd, env, get, hide, hosts, local, prompt, \
    put, require, roles, run, runs_once, settings, show, sudo, warn
>>> from invoke import Responder
>>> from fabric import Connection
>>> c = Connection('host')
>>> sudopass = Responder(
... pattern=r'\[sudo\] password:',
... response='mypassword\n',
... )
>>> c.run('sudo whoami', pty=True, watchers=[sudopass])
[sudo] password:
root
<Result cmd='sudo whoami' exited=0>
Line 7: Line 47:
== Sudo ==
{{{
>>> import getpass
>>> from fabric import Connection, Config
>>> sudo_pass = getpass.getpass("What's your sudo password?")
What's your sudo password?
>>> config = Config(overrides={'sudo': {'password': sudo_pass}})
>>> c = Connection('db1', config=config)
>>> c.sudo('whoami', hide='stderr')
root
<Result cmd="...whoami" exited=0>
>>> c.sudo('useradd mydbuser')
<Result cmd="...useradd mydbuser" exited=0>
>>> c.run('id -u mydbuser')
1001
<Result cmd='id -u mydbuser' exited=0>
}}}
Line 10: Line 66:
 * [[Full manual|http://docs.fabfile.org/en/2.1/]]  * [[http://docs.fabfile.org/en/2.1/|Full manual]]

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:

  • command
  • shell
  • warn
  • hide: [out|err|both|None]
  • pty
  • echo: show what will be execute
  • env: inject variables
  • replace_env: it will be the only environment
  • watchers: automatic replay to stdout!
  • echo_stdin
  • respond: write in response of watcher

local: same as run but execute on the local host

Exit codes attributes:

  • ok|failed
  • exited
  • return_code
  • stdout
  • connection.host

Usage by examples

Autoresponder

>>> from invoke import Responder
>>> from fabric import Connection
>>> c = Connection('host')
>>> sudopass = Responder(
...     pattern=r'\[sudo\] password:',
...     response='mypassword\n',
... )
>>> c.run('sudo whoami', pty=True, watchers=[sudopass])
[sudo] password:
root
<Result cmd='sudo whoami' exited=0>

Sudo

>>> import getpass
>>> from fabric import Connection, Config
>>> sudo_pass = getpass.getpass("What's your sudo password?")
What's your sudo password?
>>> config = Config(overrides={'sudo': {'password': sudo_pass}})
>>> c = Connection('db1', config=config)
>>> c.sudo('whoami', hide='stderr')
root
<Result cmd="...whoami" exited=0>
>>> c.sudo('useradd mydbuser')
<Result cmd="...useradd mydbuser" exited=0>
>>> c.run('id -u mydbuser')
1001
<Result cmd='id -u mydbuser' exited=0>

Documentation

MyWiki: fabric (last edited 2018-07-25 09:12:28 by risca)