【问题标题】:How can I know a file's content with chef我如何通过厨师知道文件的内容
【发布时间】:2014-02-20 19:12:22
【问题描述】:

我正在尝试使用执行资源更改 gem 源,但我只需要这样做一次。

remove_rubygems = 'gem sources -r http://rubygems.org/'

execute 'change sources to our gem server' do
  command "#{remove_rubygems} && gem sources -a http://mygemserver"
  creates "~/.gemrc"
end

这一切都是因为当我多次执行此资源时,.gemrc 文件看起来像它。

vagrant@leibniz-app:~$ gem source
*** CURRENT SOURCES ***

http://mygemserver/
http://mygemserver/
http://mygemserver/
http://mygemserver/
...

然后,我希望 mygemserver 在 .gemrc 文件中有一次。

我该怎么做?

在执行我的资源之前有什么方法可以知道.gemrc的内容吗?

【问题讨论】:

    标签: bash chef-infra chef-recipe


    【解决方案1】:

    不要在你的路径中使用~/;这就是为什么 creates 行无法阻止 execute 资源在文件已存在时运行的原因。

    execute 'change sources to our gem server' do
      command "#{remove_rubygems} && gem sources -a http://mygemserver"
      creates "/root/.gemrc"
    end
    

    或者,如果要检查文件是否包含特定行:

    execute 'change sources to our gem server' do
      command "#{remove_rubygems} && gem sources -a http://mygemserver"
      not_if "grep -F -q -e http://mygemserver/ ~/.gemrc" # this uses a shell, so the
                                                          # tilde should work here.
    end
    

    【讨论】:

    • 对于像我这样的新手,您可能会提到 creates 本身将确保文件不会被覆盖。 (虽然手册已经提到它并且听起来很合乎逻辑,......现在:)
    • @hek2mgl,谢谢——我已经更新了这一点,使其更加清晰,并且让not_if 的答案适用于creates 无法处理的情况。
    猜你喜欢
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    相关资源
    最近更新 更多