首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将chef-handler中的日志复制到工作站?

如何将chef-handler中的日志复制到工作站?
EN

Stack Overflow用户
提问于 2013-03-20 17:14:56
回答 1查看 1K关注 0票数 4

我尝试将日志从节点复制到工作站。

我是个厨师和红宝石新手。

在食谱中:

代码语言:javascript
复制
directory "/var/chef/handlers" do
recursive true
action :nothing
end.run_action(:create)

cookbook_file "/var/chef/handlers/chef_handler_hibu.rb" do
action :nothing
end.run_action(:create)

chef_handler "Chef::Handler::Copy" do
source "/var/chef/handlers/chef_handler_hibu" 
end.run_action(:enable)

在文件中

代码语言:javascript
复制
class Copy < Chef::Handler
def initialize

end
def report
execute "report" do
command "sshpass -p ******* scp -o StrictHostKeyChecking=no /var/log 
/#{node["ipaddress"]}.log gestchef@192.168.107.214:/var/log/chef_clients_logs
/#{node["ipaddress"]}.log"
end
end
end

但是在chef处理程序中没有定义execute方法。

如何在chef Handler中执行linux命令?

或者如何在ruby中做到这一点?

我试一下这个

代码语言:javascript
复制
require 'rubygems'
require 'net/ssh'
require 'net/scp'


class Copy < Chef::Handler
def initialize

end
Net::SSH.start("192.168.107.214", "*****",:password => "******") do |session|
session.scp.download! "/var/log/#{node["ipaddress"]}.log", "/var/log
/chef_clients_logs   /#{node["ipaddress"]}.log"
end

end

但是错误

代码语言:javascript
复制
cannot load such file -- net/scp

gem list --local

net-scp (1.0.2)
net-sftp (2.0.2)
net-ssh (2.0.11)
s3sync (1.2.5)
xml-simple (1.0.12)

代码语言:javascript
复制
gem which net-scp
Can't find ruby library file or shared library net-scp

发生了什么?

EN

回答 1

Stack Overflow用户

发布于 2013-03-22 18:19:33

经过多次搜索和测试,这就是对我有效的方法。

在食谱中:

代码语言:javascript
复制
    directory "/var/chef/handlers" do
      recursive true
      action :nothing
    end.run_action(:create)

    cookbook_file "/var/chef/handlers/chef_handler_hibu.rb" do
      action :nothing
    end.run_action(:create)

    chef_handler "Chef::Handler::Copy" do
      source "/var/chef/handlers/chef_handler_hibu" 
    end.run_action(:enable)

files/default/chef_handler_hibu.rb

代码语言:javascript
复制
    class Copy < Chef::Handler

      def report

        mpadd = Chef::Resource::Execute.new(mpadd,run_context)
        mpadd.command("sshpass -p ***** scp -o StrictHostKeyChecking=no /var/log
    /#{node["ipaddress"]}.log gestchef@192.168.107.214:/var/log/chef_clients_logs
    /#{node["ipaddress"]}.log")
        mpadd.run_action(:run)
      end
    end

这样,您就可以在chef_handler阶段将节点的日志复制到任何位置。

我希望它能对你有所帮助。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15519413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档