【问题标题】:Overriding attributes in chef programmatically以编程方式覆盖厨师中的属性
【发布时间】:2013-05-19 01:39:40
【问题描述】:

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

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

    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 文件...但不希望偏离正在使用的操作。

【问题讨论】:

    标签: python ruby python-2.7 chef-infra knife


    【解决方案1】:

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

    我并不是说这会起作用,因为我没有在 python 中使用过 chef,但我认为这就是它的工作方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      • 2014-01-20
      • 2023-03-22
      相关资源
      最近更新 更多