我该如何编辑EC2节点的厨师属性,使用刀 [英] How can I edit a chef attribute of an ec2 node using knife

查看:248
本文介绍了我该如何编辑EC2节点的厨师属性,使用刀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个刀节点命令编辑EC2节点的node_data。
我可以用下面的命令做手工。
刀节点编辑NODE_NAME
它会生成一个JSON,我需要编辑。

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
It will generate a json which I need to edit.

"name": "NODE-1",
  3   "chef_environment": "test",
  4   "normal": {
  5     "node_data": {
  6       "version": "23690ecc9c572e47db242bfad1296388f91da1e9",
  7       "depot_path": "https://s3.amazonaws.com/builds/",
  8       "source_repo": "softwares/"
  9     },
 10     "tags": [
 11 
 12     ]
 13   },
 14   "run_list": [
 15     "role[my-role]"
 16   ]
 17 }

我想编辑 node_data 在JSON。
如果让我来编辑run_list的存在对于
命令 run_list添加节点刀节点的作用[ROLE_NAME]
我需要类似于此命令的东西。

I want to edit node_data in that json.
If I had to edit run_list the there is a command for that
knife node run_list add node 'role[ROLE_NAME]'
I need something similar to this command.

推荐答案

我添加了一个刀插件添加到node_data。

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

这篇关于我该如何编辑EC2节点的厨师属性,使用刀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆