Not able to delete node attribute using knife command

3.9k Views Asked by At

I have created cookbook for enabling some of the windows feature and rebooting the machine using WindowsRebootHandler. As shown in below code, I have used ruby_block to not perform the same action in the subsequent runs.

windows_batch 'Enable_MS_Feature' do
  code <<-EOH
    #My script
  EOH
  notifies :create, "ruby_block[WindowsFeature_Install]", :immediately
  notifies :request, 'windows_reboot[60]'
  not_if { node.attribute?("WindowsFeature_Installed") }
end

ruby_block "WindowsFeature_Install" do
  block do
    node.set['WindowsFeature_Installed'] = true
    node.save
  end
  action :nothing
end

For testing purpose, I have to delete WindowsFeature_Installed attribute to perform the action again. So, I have used below command.

knife exec -E "nodes.transform(:all) {|n| n.delete('WindowsFeature_Installed');n.save() }"

Above command is not deleting the node attribute. When I searched for the same attribute using knife command, it listed all the nodes.

knife search node "WindowsFeature_Installed:true"

To debug further, have enabled verbose output of the commands and all the HTTP responses from chef-server were OK.

Also, tried to check chef-server logs for error details. But was not able to identify the issue as many process logs are present related to chef-server package.

So, how can I remove this attribute to perform the recipe action again? Or in which chef-server log, node attribute delete details will be logged? Any pointers will be helpful.

1

There are 1 best solutions below

0
On

I had a similar problem. I could not delete a attribute from the node but modifying via set worked. What help me to solve this was to look on which level the attribute was generated by one of my recipes.

For example to delete attribute "network_interfaces" look at your current notes config.

knife node show -l -F json <node_name>

You should get a similar output from your nodes-config:

{
  "name": "<node_name>",
  "chef_environment": "_default",
  "run_list": [
   ...
  ],
  "normal": {
     "tags": [
     ],
    "network_interfaces": {
       "order": [
         "vpn_vpn"
       ]
    },
  },
  "default": {
     ...
  },
  ...

In my case I wanted to delete the network_interfaces-attribute from all nodes. So I executed:

knife exec -E 'nodes.transform(:all) { |n| n.normal_attrs.delete("network_interfaces"); n.save() }'

instead of:

knife exec -E 'nodes.transform(:all) { |n| n.delete("network_interfaces"); n.save() }'

More about the different attribute precedence levels can be found here: https://coderwall.com/p/rfm4lg

A great article about querying for node attributes with knife: http://www.programmersparadox.com/2013/02/05/viewing-chef-node-attributes-with-knife/