简体   繁体   English

Chef上的Ruby代码作为“ruby_block”无法正常工作

[英]Ruby code on Chef as a “ruby_block” not working

I have a question for the Ruby and Chef hackers. 我有一个关于Ruby和Chef黑客的问题。

I have very limited knowledge of Chef and even less on Ruby programming language, however, I need to implement on Chef (chef-solo) something similar to "augeas" (which works with Puppet, but here I need a solution for Chef). 我对Chef的知识非常有限,对Ruby编程语言的了解甚少,但是,我需要在Chef(chef-solo)上实现类似于“augeas”的东西(与Puppet一起使用,但在这里我需要一个Chef的解决方案)。

I got the example code below but it's not working and I am now for a few days trying to figure out what is wrong. 我得到了下面的示例代码,但它不起作用,我现在试图弄清楚出了什么问题。

Basically I need to be able to select specific strings in a text file and modify these values. 基本上我需要能够在文本文件中选择特定的字符串并修改这些值。 I could use sed but perhaps I can do it in a more elegant way using the ruby_block from Chef. 我可以使用sed但也许我可以使用Chef的ruby_block以更优雅的方式完成它。

Please let me know what can be possibly wrong with the code below. 请让我知道下面的代码可能有什么问题。 Why is my /etc/hosts not being updated with new values? 为什么我的/ etc / hosts没有使用新值更新?

Always when I re-run chef-solo, I get the following error: 总是当我重新运行chef-solo时,我收到以下错误:

NoMethodError
-------------
undefined method `chef' for Chef::Resource::RubyBlock

Thanks for your help. 谢谢你的帮助。

Follows my default.rb file: 关注我的default.rb文件:

ruby_block "edit etc hosts" do
  block do
    rc = Chef::Util::FileEdit.new("/etc/hosts")
    rc.search_file_replace_line(
      /^127\.0\.0\.1 localhost$/,
      "127.0.0.1 #{new_fqdn} #{new_hostname} localhost"
    )
    rc.write_file
  end
end

Add this line as the first line of your ruby block: 将此行添加为ruby块的第一行:

require 'chef/util/file_edit'

According to your case, you should use the cookbook hostsfile : 根据你的情况,你应该使用cookbook hosts文件

hostsfile_entry '127.0.0.1' do
  hostname  new_hostname
  aliases   [new_fqdn]
  comment   'Append by Recipe X'
  action    :append
end

It shouldn't be too hard to get Augeas to work with Chef. 让Augeas与Chef合作应该不会太难。 Augeas is a C library, and Puppet simply uses its Ruby bindings. Augeas是一个C库,Puppet只使用它的Ruby绑定。 You just need to make use of these bindings in Chef. 您只需要在Chef中使用这些绑定。

There is a PoC Augeas resource provider for Chef here: https://github.com/craigtracey/augeas . 这里有一个PoC Augeas资源提供者: https//github.com/craigtracey/augeas

Note: http://lists.opscode.com/sympa/arc/chef/2013-02/msg00337.html mentions Augeas integration into Chef, but apparently the participants misunderstand Augeas as they mention idempotency issues and deltas. 注意: http ://lists.opscode.com/sympa/arc/chef/2013-02/msg00337.html提到Augeas整合到Chef中,但显然参与者误解了Augeas,因为他们提到了幂等问题和增量。 Most uses of Augeas don't lead to managing deltas, but desired states. Augeas的大多数用途都不会导致管理增量,而是希望的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM