Manual host name control
In this tool I didn't need to run many parallel SSH connection, so I decided to control remote host name from inside the loop in the function my setting env.host_string each time (this is very useful functionality). Like in following example:#!/usr/bin/env python
"""Example code to use Fabric as a library.
It shows how to set up host manually.
Author: Wawrzek Niewodniczanski < main at wawrzek dot name >
"""
# import sys to deal with scripts arguments and of course fabric
import sys
import fabric from fabric.api import run, hide, env env.hosts = ['host1', 'host2']
# Main function to run remote task
def run_task(task='uname'): """run_task([task]) - runs a command on a remote server. If task is not specify it will run 'uname'."""
# hide some information (this is not necessary).
with hide('running', 'status'): run(task)
# Main loop
# take all arguments and run them on all hosts specify in env.hosts variable
# if not arguments run 'uname'
if len(sys.argv) > 1:
tasks = sys.argv[1:]
for task in tasks:
for host in env.hosts: env.host_string = host run_task(task)
else:
for host in env.hosts:
run_task()
Fabric in full control
The problem bugged me since then. Yesterday I found some of my old code. Analysed it and quickly found small, but profound difference with mu recent fabric usage. Rhe code above called the run_task function wrongly. Rather than dealt it in the normal way I supposed to use execute.#!/usr/bin/env python
"""Example code to use Fabric as a library.
It shows how to set up host manually.
Author: Wawrzek Niewodniczanski < main at wawrzek dot name >
"""
# import sys to deal with scripts arguments and of course fabric
import sys
import fabric from fabric.api import run, hide, env, execute env.hosts = ['host1', 'host2']
# Main function to run remote task
def run_task(task='uname'): """run_task([task]) - runs a command on a remote server. If task is not specify it will run 'uname'."""
# hide some information (this is not necessary).
with hide('running', 'status'): run(task)
# Main loop
# take all arguments and run them on all hosts specify in env.hosts variable
# if not arguments run 'uname'
if len(sys.argv) > 1:
tasks = sys.argv[1:]
for task in tasks:
execute(run_task, task)
else:
execute(run_task)
Links:
http://www.fabfile.org/http://larryn.blogspot.co.uk/2012/11/fabric-as-python-module.html
http://lists.nongnu.org/archive/html/fab-user/2014-10/msg00002.html