我正在尝试使用PyChef api创建一个角色。我能够成功检索节点或角色列表(GET方法),但是当我尝试创建一个角色(POST方法)时,我得到一个运行时例外,即不允许使用方法。 这是我的代码:
import json
import requests
import re
import chef
from chef.exceptions import ChefError, ChefServerNotFoundError
from chef.base import ChefQuery, ChefObject
from chef import Role
pem_file = "path-to-validator.pem/chef-validator.pem"
client_name = "validator"
node_name = "root"
role_url = "ChefServer-URL/roles/"
rolelist =[]
chef_api = chef.ChefAPI(role_url,pem_file,client_name,ssl_verify=False)
create_role = chef.Role.create(name='myrole', api=chef_api ,description='A test role', run_list=['recipe[foo]'],default_attributes={'attr': 'foo'}, override_attributes={'attr': 'bar'})
rolelst = chef.Role.list()
for j in rolelst:
rolelist.append(j)
print rolelist
这是我面临的错误:
Traceback (most recent call last):
File "roleapi.py", line 37, in <module>
create_role = chef.Role.create(name='myrole', api=chef_api ,description='A
test role', run_list=['recipe[foo]'],default_attributes={'attr': 'foo'},
override_attributes={'attr': 'bar'})
File "/usr/lib/python2.6/site-packages/chef/base.py", line 99, in create
api.api_request('POST', cls.url, data=obj)
File "/usr/lib/python2.6/site-packages/chef/api.py", line 217, in
api_request
response = self.request(method, path, headers, data)
File "/usr/lib/python2.6/site-packages/chef/api.py", line 207, in request
raise ChefServerError.from_error(response.reason, code=response.status_code)
chef.exceptions.ChefServerError: Method Not Allowed
所以请建议我如何解决此错误。提前谢谢。
答案 0 :(得分:2)
URL应该是Chef服务器的基础,可能类似于https://myserver.com/organizations/foo
。