以编程方式覆盖chef中的属性

时间:2013-05-23 15:40:39

标签: python ruby python-2.7 chef knife

我正在使用厨师来测试软件。因此,所述软件动态/的文件名和下载位置将作为属性传递。

请注意,我必须使用我们的运营团队正在使用的厨师脚本和食谱,作为测试程序的一部分。它们具有环境级别和default.rb cookbook级别的值。他们使用ruby脚本通过刀具openstack&& amp;来设置VM。通过REST api将该服务器添加到chef:

    Chef::Config.from_file("/root/.chef/knife.rb")  
    rest = Chef::REST.new(CHEF_API)
    newserver=
        {
          :name => server.hostname,
          :chef_type => "node",
          :chef_environment => server.environment,
          :json_class => "Chef::Node",
          :attributes => {
            :cobbler_profile => server.profile
          },
          :overrides => {
          },
          :defaults => {
          },
          :run_list => server.roles 
        }

    begin
        result = rest.post_rest("/nodes",newserver)
        ....

理想情况下,文件名和位置将作为命令行参数传递到python应用程序,然后使用knife或pychef(或ruby,如果必须...)来设置/覆盖现有的节点级属性。

他们用来添加服务器的方法省略了我在其他类似问题中看到的-j选项。

我尝试了knife node edit - 但这需要使用编辑器。

我试过了

node = chef.Node('myNode')  
node.override['testSoftware']['downloads']['testSoftwareInstaller'] = 'http://location/of/download'
node.save()

但是node.override['testSoftware']['downloads']['testSoftwareInstaller']随后会返回原始值(并且可以在UI中看作原始值)。您似乎只能以这种方式设置 new 属性 - 但不能编辑/覆盖现有属性。

我正在考虑简单地动态生成environment.json文件......但更愿意不偏离正在使用的操作。

1 个答案:

答案 0 :(得分:1)

我对厨师很陌生,你可能在3年后甚至不需要这个,但是......我认为你应该使用node['override']['attribute']代替node.override['attribute']。前者用于设定值,后者用于获取值。

我不是说这会起作用,因为我没有使用python的厨师,但我认为这是它的工作方式。

相关问题