Python/Fabric2

From Omnia
Jump to navigation Jump to search

Fabric 2

https://www.fabfile.org/
>>> from fabric import Connection
>>> result = Connection('web1.example.com').run('uname -s', hide=True)
>>> msg = "Ran {0.command!r} on {0.connection.host}, got stdout:\n{0.stdout}"
>>> print(msg.format(result))
Ran 'uname -s' on web1.example.com, got stdout:
Linux

Installation

Basic install:

pip install fabric

To not conflict with v1 (have both installed):

pip3 install fabric2

Basic Connection

import fabric2

cn = fabric2.Connection('user@somesystem')
cn.config.run.warn=True  # don't abort on failure
cn.run('hostname')  # remote execution
cn.local('hostname')  # local execution

Specify Password and Port

    conn = Connection(
    "{username}@{ip}:{port}".format(
        username=username,
        ip=ip,
        port=port,
        ),
    connect_kwargs={"password": password},
)

ref: [1]

CD

con = fabric2.Connection('localhost')
with con.cd('/etc'):
  con.run('pwd')  # should display /etc

ref: [2]

keywords

Python Fabric Fabric2 Paramiko