Differences between revisions 8 and 9
Revision 8 as of 2018-07-25 09:06:52
Size: 1977
Editor: risca
Comment:
Revision 9 as of 2018-07-25 09:09:39
Size: 2794
Editor: risca
Comment:
Deletions are marked like this. Additions are marked like this.
Line 70: Line 70:
== Groups of hosts ==
{{{#!highlight pycon
>>> from fabric import Connection
>>> for host in ('web1', 'web2', 'mac1'):
>>> result = Connection(host).run('uname -s')
... print("{}: {}".format(host, result.stdout.strip()))
...
...
web1: Linux
web2: Linux
mac1: Darwin
}}}
=== serialized groups ===
{{{#!highlight pycon
>>> from fabric import SerialGroup as Group
>>> results = Group('web1', 'web2', 'mac1').run('uname -s')
>>> print(results)
<GroupResult: {
    <Connection 'web1'>: <CommandResult 'uname -s'>,
    <Connection 'web2'>: <CommandResult 'uname -s'>,
    <Connection 'mac1'>: <CommandResult 'uname -s'>,
}>
>>> for connection, result in results.items():
... print("{0.host}: {1.stdout}".format(connection, result))
...
...
web1: Linux
web2: Linux
mac1: Darwin
}}}

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

   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))

Groups of hosts

   1 >>> from fabric import Connection
   2 >>> for host in ('web1', 'web2', 'mac1'):
   3 >>>     result = Connection(host).run('uname -s')
   4 ...     print("{}: {}".format(host, result.stdout.strip()))
   5 ...
   6 ...
   7 web1: Linux
   8 web2: Linux
   9 mac1: Darwin
  10 

serialized groups

   1 >>> from fabric import SerialGroup as Group
   2 >>> results = Group('web1', 'web2', 'mac1').run('uname -s')
   3 >>> print(results)
   4 <GroupResult: {
   5     <Connection 'web1'>: <CommandResult 'uname -s'>,
   6     <Connection 'web2'>: <CommandResult 'uname -s'>,
   7     <Connection 'mac1'>: <CommandResult 'uname -s'>,
   8 }>
   9 >>> for connection, result in results.items():
  10 ...     print("{0.host}: {1.stdout}".format(connection, result))
  11 ...
  12 ...
  13 web1: Linux
  14 web2: Linux
  15 mac1: Darwin
  16 

Documentation

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