Fabric accepts a hosts iterable as parameters in tasks. Per the documentation:
An iterable of host-connection specifiers appropriate for eventually instantiating a Connection. The existence of this argument will trigger automatic parameterization of the task when invoked from the CLI, similar to the behavior of --hosts.
One of the members of which could be:
A string appropriate for being the first positional argument to Connection - see its docs for details, but these are typically shorthand-only convenience strings like hostname.example.com or user@host:port.
As for your example, please try this for fabfile.py:
host_list = ["my_host"]
@task(hosts=host_list)
def tt(c):
c.run('uname -a')
Alternatively, you can omit the host declaration from the fabfile altogether. If you don't specify the host in fabfile.py
, you can simply specify it as a host when invoking the fab
cli utility. If your fabfile.py is this:
@task
def tt(c):
c.run('uname -a')
You would now run fab -H my_host tt
to run it on the alias tt
from your SSH client config.
Hope this helps.