Gripp
Gripp

Reputation: 166

Overriding attributes in chef programmatically

I am using chef to test software. Thus the file name and download location of said software dynamic/would be passed in as an attribute.

Note that I have to use the chef scripts and recipes that our operations team is using, as part of the test procedure. They have the values in question at the environmental level and the default.rb cookbook level. They use a ruby script to setup the VM via knife openstack && add that server to chef via the 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)
        ....

Ideally the file name and location would be passed into the python app as command line parameters, and then use knife or pychef (or ruby, if I have to...) to set/override the existing node-level attributes.

The method that they use to add the server leaves out the -j option that I've seen in other similar questions.

I have tried knife node edit - but that requires the use of an editor..

I have tried

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

But node.override['testSoftware']['downloads']['testSoftwareInstaller'] subsequently returns the original value (and can be seen as the original in the UI). It seems that you can only set new attributes this way - but not edit/overwrite existing ones.

I am contemplating simply generating the environmental.json file dynamically... but would prefer to not deviate from what operations is using.

Upvotes: 2

Views: 1309

Answers (1)

ilvidel
ilvidel

Reputation: 341

I am quite new to chef, and you probably don't even need this after 3 years, but... I think you should be using node['override']['attribute'] instead of node.override['attribute']. The former is for setting values, the latter for getting values.

I am not saying this will work, since I haven't used chef with python, but I think that's the way it works.

Upvotes: 1

Related Questions