问题描述:

I have the following fabric task:

@task

def deploy_west_ec2_ami(name, puppetClass, size='m1.small', region='us-west-1', basedn='joe', ldap='arch-ldap-01', secret='secret', subnet='subnet-d43b8abd', sgroup='sg-926578fe'):

execute(deploy_ec2_ami, name='%s',puppetClass='%s',size='%s',region='%s',basedn='%s',ldap='%s',secret='%s',subnet='%s',sgroup='%s' %(name, puppetClass, size, region, basedn, ldap, secret, subnet, sgroup))

However when I run the command:

fab deploy_west_ec2_ami:test,java

I get the following Traceback:

 Traceback (most recent call last):

File "/usr/local/lib/python2.6/dist-packages/fabric/main.py", line 710, in main

*args, **kwargs

File "/usr/local/lib/python2.6/dist-packages/fabric/tasks.py", line 321, in execute

results['<local-only>'] = task.run(*args, **new_kwargs)

File "/usr/local/lib/python2.6/dist-packages/fabric/tasks.py", line 113, in run

return self.wrapped(*args, **kwargs)

File "/home/bcarpio/Projects/githubenterprise/awsdeploy/fabfile.py", line 35, in deploy_west_ec2_ami

execute(deploy_ec2_ami, name='%s',puppetClass='%s',size='%s',region='%s',basedn='%s',ldap='%s',secret='%s',subnet='%s',sgroup='%s' %(name, puppetClass, size, region, basedn, ldap, secret, subnet, sgroup))

TypeError: not all arguments converted during string formatting

I am not sure I understand why. I am pretty sure I have all the values defined here just fine.

Also when I run the execute task deploy_ec2_ami as so:

deploy_ec2_ami:test,java,m1.small,us-west-1,'dc\=test\,dc\=net',ldap-01,secret,subnet-d43b8abd,sg-926578fe

It works just fine

网友答案:

Ok the issue was when using execute you must define a host=. My other fab task doesn't need host= so I just put in regular python:

deploy_ec2_ami (name, puppetClass, size, region, basedn, ldap, secret, subnet, sgroup)

And it all worked fine.

网友答案:

You are passing in a set of keyword arguments to the execute method, and only the last value is considered the target for the string interpolation:

sgroup='%s' %(name, puppetClass
        , size, region, basedn, ldap, secret, subnet, sgroup))

There is no need to use string interpolation here; just pass all the arguments on to the execute method:

execute(deploy_ec2_ami, name=name, puppetClass=puppetClass, size=size, region=region, basedn=basedn, ldap=ldap, secret=secret, subnet=subnet, sgroup=sgroup)
网友答案:

From the line

sgroup='%s' %(name, puppetClass, size, region, basedn, ldap, secret, subnet, sgroup)

String interpolation works for one string, not for all arguments. I mean

'%s %s' % (arg1, arg2)

but if you try

'%s' % (arg1, arg2) 

there is no room for arg2

If you want to keep doing string interpolation I suggest doing this:

execute(deploy_ec2_ami, name='%s' % name, puppetClass='%s' % puppetClass, size='%s' % size, region='%s' % region, and so on

As you are not changing anything in the parameters, I would definitely go for Brian solution

相关阅读:
Top