Differences between revisions 5 and 6
Revision 5 as of 2018-07-25 08:57:54
Size: 868
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 33: Line 33:
== 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>
}}}

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)