【问题标题】:How copy logs in chef-handler to workstation?如何将厨师处理程序中的日志复制到工作站?
【发布时间】:2013-03-09 07:16:54
【问题描述】:

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

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

在配方中:

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)

在文件中

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 Handler 中执行 linux 命令?

或者如何用 ruby​​ 做到这一点?

我试试这个

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

但是错误

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)

gem which net-scp
Can't find ruby library file or shared library net-scp

发生了什么?

【问题讨论】:

    标签: ruby chef-infra chef-recipe


    【解决方案1】:

    经过多次搜索和多次测试,这对我有用。

    在配方中:

        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:

        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阶段。

    希望对你有帮助。

    【讨论】:

    猜你喜欢
    • 2015-04-15
    • 1970-01-01
    • 2014-03-09
    • 2015-02-25
    • 2013-05-23
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多