4

ノードからワークステーションにログをコピーしようとしています。

私はシェフとルビーが初めてです。

レシピ:

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ハンドラーでは定義されていません。

chefハンドラーでLinuxコマンドを実行するにはどうすればよいですか?

または、ルビーでこれをどのように行うことができますか?

これを試してみます

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

どうしたの?

4

1 に答える 1

3

After much searching, and many tests, this is what has worked for me.

In 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)

In 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

With this, you can copy logs of nodes to anywhere, in the chef_handler stage.

I hope it helps you.

于 2013-03-22T10:19:33.453 に答える