面料“类型错误:不是字符串格式化过程中转换所有参数”

问题描述:

我有以下面料任务:面料“类型错误:不是字符串格式化过程中转换所有参数”

@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)) 

然而,当我运行命令:

fab deploy_west_ec2_ami:test,java 

我得到以下回溯:

  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 

我不知道我明白为什么。我很确定我有这里定义的所有值都很好。

而且当我运行执行任务deploy_ec2_ami像这样:

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

它工作得很好

问题使用执行必须定义一个主机时就OK =。我的其他工作室任务不需要主机=所以我只是把普通的python:

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

而且一切正常。

你传递一个组关键字参数到execute方法,只有最后一个值是考虑到字符串插值的目标:

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

这里没有必要使用字符串插值;只是传递到execute方法的所有参数:

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

从线

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

路线插值适用于一个字符串,而不是对所有参数。我的意思是

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

但如果您尝试

'%s' % (arg1, arg2) 

没有余地ARG2

如果你想继续做串插,我建议这样做:

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

由于你没有改变任何参数,我肯定会去布赖恩解决方案