Peter
Peter

Reputation: 427

Passing in a parameter to Fabric

I'm having some issues passing in a parameter to Fabric. I'd like to provide a hostname that is then set on a specified host. Here is the basic structure of what I have tried, yet Fabric seems to interpret this as if to write "hostname %s" to the host instead of the actual value passed in.

@task
def configure_host(hostname):
  sudo("hostname %s") % hostname 

I'll note that this content is within a file called set_host in a fabfile directory. As such, I can run "fab -H [email protected] set_host.configure_host" to run this code (it works when I hardcode the hostname). However, I have tried to run it as "fab -H [email protected] set_host.configure_host:host=FunHostname" (noting my suffix), it fails.

The first issue is that it seems to be writing "hostname %s" instead of the actual hostname, but also not liking some of my sudo commands very much and some return a "Fatal error: sudo() received nonzero return code 1 while executing!". I can try and find a workaround for the latter but I'm wondering if my basic strategy for a user to pass in a parameter is logical. I thought this would be simple and I was following the outline under "Task arguments" here: http://docs.fabfile.org/en/latest/tutorial.html

Upvotes: 0

Views: 355

Answers (1)

mguillermin
mguillermin

Reputation: 4181

You should include the string formatting inside the parentheses :

sudo("hostname %s" % hostname)

This way, Fabric will correctly replace %s with the value of the hostname variable.

Upvotes: 1

Related Questions