Interview Prep/Python for DevOps/Kubernetes, Docker & SSH clients

What does Fabric add on top of paramiko?

IntermediateConceptKubernetes, Docker & SSH clients
Answer

Fabric (2.x/3.x) gives a high-level Connection with run, sudo, put, get, cd context managers, and ThreadingGroup for parallel execution across hosts.

from fabric import Connection, ThreadingGroup
c = Connection("web1", user="deploy", connect_kwargs={"key_filename": "~/.ssh/deploy"})
c.put("app.tar.gz", "/tmp/")
c.sudo("systemctl restart app", hide=True)

results = ThreadingGroup("web1", "web2", "web3").run("df -h /", hide=True, warn=True)
for conn, r in results.items():
    print(conn.host, r.ok, r.stdout.strip())

warn=True returns failures instead of raising, so one bad host does not abort the whole group.

All Python for DevOps questions Roadmap: Python for automation

More on Kubernetes, Docker & SSH clients