2013-05-30 48 views
1

我可以运行:蟒蛇厨师在控制台的工作不是在脚本

import chef 
chef.autoconfigure() 
for node in chef.Node.list(): 
    if "auto" in node.lower(): 
     print "deleting node " + node 
     nodeObj = chef.Node(node) 
     nodeObj.delete() 
在控制台

直接,但是当我尝试运行它作为一个脚本:python2.7 test.py我收到以下错误:

Traceback (most recent call last): 
    File "test.py", line 38, in <module> 
    for node in chef.Node.list(): 
    File "/usr/local/lib/python2.7/site-packages/chef/base.py", line 86, in list 
    names = [name for name, url in api[cls.url].iteritems()] 
TypeError: 'NoneType' object has no attribute '__getitem__' 

我用控制台验证

>>> chef.__path__ 
['/usr/local/lib/python2.7/site-packages/chef'] 

因此,机器是一样的,蟒蛇的版本是一样的, nd模块是一样的。为什么这可能发生?

回答

2

我发现,当作为脚本运行时,pyChef没有正确识别自动配置步骤的knife.rb文件。

这是得到它,而不是工作:

with chef.ChefAPI('http://example.com:4000', '/root/.chef/client.pem', 'client'): 
    for node in chef.Node.list(): 
     if "auto" in node.lower(): 
      print "deleting node " + node 
      nodeObj = chef.Node(node) 
      nodeObj.delete() 

请注意,我不知道为什么它无法在一种情况下正确使用knife.rb文件,而不是其他的(我核实,同在这两种情况下都使用了cwd ... - 甚至尝试指向自动配置('/ folder/of/knife.rb'),但没有运气。

1

虽然我不知道为什么ChefAPI对象不会持久存在一个脚本,我发现我必须通过我的搜索对象我的ChefAPI对象,as seen as a keyword argument in the signature here。就像你的情况,当在控制台中测试我的代码时,这是没有必要的。

就我而言,我生成ChefAPI对象from_config_file(),并将它传递给我的搜索对象是这样的:

import chef 
chefapiobject = chef.chefAPI.from_config_file('knife.rb') 
nodes = chef.Search('node', 'roles:worker', api=chefapiobject) 

在控制台中,该工程没有经过api=chefapiobject

1

您可以导入本地配置使用chef.autoconfigure。例如:

from chef import autoconfigure, Client, Node 
api = autoconfigure() 

http://pychef.readthedocs.org/en/latest/api.html#chef.autoconfigure

Try to find a knife or chef-client config file to load parameters from, starting from either the given base path or the current working directory.

相关问题