How can I edit a chef attribute of an ec2 node using knife

后端 未结 3 1000
深忆病人
深忆病人 2021-01-02 23:44

I want to edit ec2 node\'s node_data using a knife node command.
I can manually do it by using below command.
knife node edit NODE_NAME

3条回答
  •  被撕碎了的回忆
    2021-01-03 00:26

    I have added a knife plugin to add to node_data.

    require 'chef/knife'
    require 'chef/knife/core/node_presenter'
    
    class Chef
      class Knife
        class NodeJson_dataUpdate < Knife
    
          deps do
            require 'chef/node'
            require 'json'
          end
    
          banner "knife node json_data update [NODE] [JSON_NODE_DATA]"
    
          def run
            node = Chef::Node.load(@name_args[0])
            node_data = @name_args[1]
            update_node_data(node, node_data)
            node.save
            output(node.normal.node_data)
          end
    
          def update_node_data(node,node_data)
            parsed_node_data = JSON.parse(node_data)
            parsed_node_data.each do |key,val|
    
                if key.empty?
                    print "ERROR: Key is empty for value- "+val+". Not adding this to node_data.\n"
                else
                    node.normal.node_data[key]=val
                end          
            end 
          end
    
        end
      end
    end
    

提交回复
热议问题