gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 08:04:51 31 4
gpt4 key购买 nike

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

我是 Chef 和 ruby​​ 的新手。

在 Recipe 中:

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

但是 execute 方法在 chef handler 中是未定义的。

如何在 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

发生了什么事?

最佳答案

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

在 Recipe 中:

    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 阶段将节点日志复制到任何地方。

希望对你有帮助。

关于ruby - 如何将 chef-handler 中的日志复制到工作站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15519413/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com