簡體   English   中英

如果該源文件存在,如何編寫Chef配方來復制文件? only_if后衛不工作

[英]How to write a Chef recipe to copy a file if that source file exists? only_if guard not working

我目前正在開發一個簡單的腳本來處理Chef部署期間的屬性文件。 我正在使用chef-apply script.rb執行以下代碼

class_path = '/var/lib/tomcat/webapps/ROOT/WEB-INF/classes'
file '/home/corp/working/corp.properties' do
  owner 'root'
  group 'root'
  mode 0644
  p File.exist?("#{class_path}")
  content File.open("#{class_path}/corp.properties").read
  action :create_if_missing
  only_if { File.exist?("#{class_path}/corp.properties") }
end

但它運行錯誤

false
[2017-12-20T11:37:40-05:00] FATAL: Stacktrace dumped to /home/corp/.chef/cache/chef-stacktrace.out
[2017-12-20T11:37:40-05:00] FATAL: Stacktrace dumped to /home/corp/.chef/cache/chef-stacktrace.out
[2017-12-20T11:37:40-05:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2017-12-20T11:37:40-05:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2017-12-20T11:37:40-05:00] FATAL: Errno::ENOENT: No such file or directory @ rb_sysopen - /var/lib/tomcat/webapps/ROOT/WEB-INF/classes/corp.properties
[2017-12-20T11:37:40-05:00] FATAL: Errno::ENOENT: No such file or directory @ rb_sysopen - /var/lib/tomcat/webapps/ROOT/WEB-INF/classes/corp.properties

如您所見,即使File.exist? call返回false,File.open正在執行,無論后衛如何。

資源塊中的任何代碼都默認在編譯時運行。 守衛在收斂時間運行。 您可以通過使其content lazy { File.open("#{class_path}/corp.properties").read }來修復此問題content lazy { File.open("#{class_path}/corp.properties").read }以強制文件讀取在收斂時發生。 有關詳細信息,請參閱https://coderanger.net/two-pass/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM