2016-08-01 74 views
-1

我正在使用Amazon opsworks並努力通過單個腳本來實現它,我創建了一個名爲clamav.rb的腳本。腳本的內容是:如何在同一腳本中使用配置Ruby腳本安裝ClamAV?

yum_package 'clamav' do 
    action :install 
end 

yum_package 'clamav-update' do 
    action :install 
end 

file_names = ['/etc/freshclam.conf'] 

file_names.each do |file_name| 

text = File.read(file_name) 

replace = text.gsub("Example", "#Example") 

    # To merely print the contents of the file, use: 

    puts replace 

    # To write changes to the file, use: 

    File.open(file_name, "w") {|file| file.puts replace } 

end 

execute "Run Freshclam" do 

    command "/usr/bin/freshclam" 

end 

當我執行上面的腳本它堅持了一個錯誤:

[2016-08-01T13:02:36+00:00] ERROR: Running exception handlers 
[2016-08-01T13:02:36+00:00] ERROR: Exception handlers complete 
[2016-08-01T13:02:36+00:00] FATAL: Stacktrace dumped to /var/lib/aws/opsworks/cache.stage2/chef-stacktrace.out 
[2016-08-01T13:02:36+00:00] ERROR: No such file or directory - /etc/freshclam.conf 
[2016-08-01T13:02:36+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1) 

,但是當我在兩個部分劃分腳本它運行得很好,如建立獨立的腳本用於yum包並單獨更改配置。

回答

1

你正在被廚師的兩道加載模型咬傷。在代碼中的這一點,該包尚未安裝。請查看https://coderanger.net/two-pass/瞭解更多詳情,但要解決您的實際問題,請使用含有此類搜索資源的line食譜,並將其替換爲文件,以便爲您正確處理排序。

+0

我是新來這個,你能不能請提供簡要 – Navneet

+0

沒有步驟,檢查出的線菜譜自述,並說明了如何使用其資源。 – coderanger

0

我解決了這個問題,以下是解決

代碼:代替我的老代碼與此

File.open( '的/ etc/freshclam.conf', 「R」)做|å文件| 如果aFile text = File.read('/ etc/freshclam.conf') replace = text.gsub(「Example」,「#Example」) #僅僅打印文件的內容,使用: puts替換 #要將更改寫入文件,請使用: File.open('/ etc/freshclam.conf',「w」){| file | file.puts替換}

else puts「Unable to open file!」 結束