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

后端 未结 3 1001
深忆病人
深忆病人 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:04

    It sounds like you want a scriptable/non-interactive way to set an attribute of a given node. You can use knife exec for this.

    For your given example, suppose you want to get and set the value of source_repo in node_data for "NODE-1". You could achieve this by running:

    knife exec -E "nodes.find(:name => 'NODE-1') { |node| node['node_data']['source_repo'] = '/new/path/softwares/'; node.save; }"

    Note the node.save at the end: this will make the chef server save your changes. If this is missing in the command, then it's a temporary change that is not saved on the chef server.

    To confirm that the attribute has indeed changed on the chef server, you can get the current value like this:

    knife exec -E "nodes.find(:name => 'NODE-1') { |node| puts node['node_data']['source_repo'] }"

    You should see: /new/path/softwares/ as the output of the above command.

    By the way, note that node['node_data']['source_repo'] is equivalent to (and can be replaced with) node.node_data.source_repo

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-03 00:27

    if you want to

    add new attribute

    knife exec -E "nodes.find(:name => 'NODE-1') { |node|   node.normal_attrs[:attribute_name]='Value' ; node.save; }"
    

    Need to update attribute

    its depends on node attribute type

    if node attribute is json then run this command

    knife exec -E "nodes.find(:name => 'NODE-1') { |node|   node.normal_attrs['node_data'][:attribute_name]= 'Value' ; node.save; }"
    

    if node attribute in array type

    knife exec -E "nodes.find(:name => 'NODE-1') { |node|   node.normal.tags << {'attribute_name'=>'Value',''=>}; node.save; }"
    
    0 讨论(0)
提交回复
热议问题