2015-08-21 72 views
1

我正在寫廚師食譜使用chef-solo,在那裏我需要運行命令,其中包括+但廚師返回一個錯誤,如果我使用+字符。如何在廚師中使用特殊字符?

bash "testing" do 
code <<-EOH 
/bin/grep '^+:' /etc/shadow >>/var/info 
EOH 
end 

方法1:將代碼中的任何script.sh文件,並使用它作爲:

execute " script running " do 
    command "sh /path/script.sh" 
end 

,但我不希望使用它。有沒有其他的方式來使用特殊字符?

注:我嘗試使用反斜槓(「\」)。

更新:有以下錯誤,當我使用代碼

bash "testing" do 
code "/bin/grep '^+:' /etc/shadow >>/var/info" 
end 

錯誤:

chef-solo -c solo.rb -j web.json 
Starting Chef Client, version 11.8.2 
Compiling Cookbooks... 
Converging 1 resources 
Recipe: test::test 
    * bash[testing] action run 
================================================================================ 
Error executing action `run` on resource 'bash[testing]' 
================================================================================ 


Mixlib::ShellOut::ShellCommandFailed 
------------------------------------ 
Expected process to exit with [0], but received '1' 
---- Begin output of "bash" "/tmp/chef-script20150822-12224-kbivpd" ---- 
STDOUT: 
STDERR: 
---- End output of "bash" "/tmp/chef-script20150822-12224-kbivpd" ---- 
Ran "bash" "/tmp/chef-script20150822-12224-kbivpd" returned 1 


Resource Declaration: 
--------------------- 
# In /home/new/cookbooks/test/recipes/test.rb 

    1: bash "testing" do 
    2: code "/bin/grep '^+:' /etc/shadow >>/var/info" 
    3: end 



Compiled Resource: 
------------------ 
# Declared in /home/new/cookbooks/test/recipes/test.rb:1:in `from_file' 
bash("testing") do 
    action "run" 
    retries 0 
    retry_delay 2 
    command "\"bash\" \"/tmp/chef-script20150822-12224-kbivpd\"" 
    backup 5 
    returns 0 
    code "/bin/grep '^+:' /etc/shadow >>/var/info" 
    interpreter "bash" 
    cookbook_name :test 
    recipe_name "test" 
end 



[2015-08-22T19:49:52+05:30] ERROR: Running exception handlers 
[2015-08-22T19:49:52+05:30] ERROR: Exception handlers complete 
[2015-08-22T19:49:52+05:30] FATAL: Stacktrace dumped to /home/chef-solo/chef-stacktrace.out 
Chef Client failed. 0 resources updated 
[2015-08-22T19:49:52+05:30] ERROR: bash[testing] (test::test line 1) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1' 
---- Begin output of "bash" "/tmp/chef-script20150822-12224-kbivpd" ---- 
STDOUT: 
STDERR: 
---- End output of "bash" "/tmp/chef-script20150822-12224-kbivpd" ---- 
Ran "bash" "/tmp/chef-script20150822-12224-kbivpd" returned 1 
[2015-08-22T19:49:52+05:30] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1) 

回答

0

我解決了使用

`/bin/grep '^+:' /etc/passwd >> /var/output` 
我的問題

這是使用反引號執行系統命令的ruby方法,其中全局變量$?通過反引號自動設置

+1

警告,它將在編譯時執行,而不是在收斂時間,將不會作爲審計報告,我真的很奇怪你爲什麼不依靠ohai屬性hash'node ['etc'] [ 'passwd']'並在這種情況下用ruby方法過濾它。 – Tensibai

+0

我正在使用另一個配方生成審計報告 –

+0

因此,請充分利用節點屬性,而不是重做ohai已完成的工作。 – Tensibai

0

廚師食譜是純Ruby,所以你可以寫這樣的事情,沒有任何問題:

bash "testing" do 
    code "/bin/grep '^+:' /etc/shadow >>/var/info" 
end 

如果您需要使用雙引號insid e你的命令,你可以用斜槓「\」來轉義它。

+0

您能分享更多關於錯誤的信息嗎? – rastasheep

+0

!!!我通過在我的問題中運行代碼更新了錯誤。 –

+0

你沒有創建命令的問題,但執行的命令退出退出狀態1.你確定你輸入的命令可以執行成功(工作)嗎? – rastasheep

3

這與命令中的+無關!

沒有什麼錯,廚師按預期工作:

# grep '^+' /etc/shadow; echo $? 
1 

此命令返回的1狀態代碼沒有被找到(man grep強調的報價是我的):

EXIT STATUS

The exit status is 0 if selected lines are found, and 1 if not found. If an error occurred the exit status is 2. (Note: POSIX error handling code should check for '2' or greater.)

與廚師長告訴你到底是什麼:

Expected process to exit with [0], but received '1'

閱讀bash resource documentation我們看到有一個returns屬性有這樣的描述:

returns Ruby Types: Integer, Array

The return value for a command. This may be an array of accepted values. An exception is raised when the return value(s) do not match. Default value: 0.

所以,如果你想接受這個命令返回0或1:

bash "testing" do 
    code "/bin/grep '^+:' /etc/shadow >>/var/info" 
    return [0,1] 
end 

所以你沒這個可能不是解決你的問題描述你想要達到什麼樣的,我們在這裏XY problem

+0

感謝Tensibai更好的解釋! up投票 –