如何在Chef中的ruby_block中调用Chef资源?

时间:2016-07-05 04:48:43

标签: ruby chef

我正在尝试在ruby_block中调用Chef资源。任何人都可以告诉我这段代码有什么问题?

file '/tmp/arockia/storage.txt' do
end

lines = `cat /tmp/arockia/storage.txt | wc -l`
ruby_block 'Check for content' do
    block do
            lines = `cat /tmp/arockia/storage.txt | wc -l`
            if Integer(lines) == 0
                    r = Chef::Resource::Execute.new('Get-Disk-Storage',run_context)
                    r.command 'df -kh >> /tmp/arockia/storage.txt'
                    r.run_action :run
            end
    end
end

1 个答案:

答案 0 :(得分:2)

为什么不

require 'mixlib/shellout'

file '/tmp/arockia/storage.txt' do
  action :create_if_missing
  contents lazy { shell_out!('df -kh').stdout }
  not_if do
    ::File.file?('/tmp/arockia/storage.txt') && \
      ::File.size('/tmp/arockia/storage.txt') > 0
  end
end

厨师食谱中的反叛通常是一个坏主意。 https://docs.chef.io/ruby.html#shelling-out

更新以在评论中包含coderanger的建议。