Differences between revisions 9 and 10
Revision 9 as of 2018-07-25 09:09:39
Size: 2794
Editor: risca
Comment:
Revision 10 as of 2018-07-25 09:12:28
Size: 3116
Editor: risca
Comment:
Deletions are marked like this. Additions are marked like this.
Line 33: Line 33:
== Autoresponder ==
== Session stuff ==

=
== Autoresponder ===
Line 47: Line 50:
== Sudo ==
=
== Sudo ===
Line 64: Line 68:
== File transfer ==
=
== File transfer ===
Line 70: Line 75:
== Groups of hosts ==
=
== Groups of hosts ===
Line 82: Line 88:
=== serialized groups === ==== serialized groups ====
Line 100: Line 106:

== tasks ==
{{{#!highlight python
from fabric import task

@task
def upload_and_unpack(c):
    if c.run('test -f /opt/mydata/myfile', warn=True).failed:
        c.put('myfiles.tgz', '/opt/mydata')
        c.run('tar -C /opt/mydata -xzvf /opt/mydata/myfiles.tgz')
}}}

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

Session stuff

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 

tasks

   1 from fabric import task
   2 
   3 @task
   4 def upload_and_unpack(c):
   5     if c.run('test -f /opt/mydata/myfile', warn=True).failed:
   6         c.put('myfiles.tgz', '/opt/mydata')
   7         c.run('tar -C /opt/mydata -xzvf /opt/mydata/myfiles.tgz')

Documentation

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