如何在Chef中使用特殊字符?

时间:2015-08-21 18:18:45

标签: ruby linux chef chef-recipe chef-solo

我正在使用chef-solo编写Chef食谱,我需要运行包含+的命令,但如果我使用+字符,Chef会返回错误。

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)

2 个答案:

答案 0 :(得分:3)

这与命令中的+无关!

没有错,厨师按预期工作:

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

此命令返回状态代码1,因为找不到任何内容(man grep强调的引用是我的):

  

退出状态

     

如果找到选定的行,退出状态为0,,如果未找到,则退出。如果发生错误,则退出状态为2.(注意:POSIX   错误处理          代码应检查'2'或更高。)

厨师告诉你的确如此:

  

预期进程以[0]退出,但收到'1'

阅读bash resource documentation,我们看到此描述中有returns属性:

  

returns Ruby类型:整数,数组

     

命令的返回值。这可能是一系列被接受的   值。当返回值不匹配时引发异常。   默认值:0。

因此,如果您想接受此命令,请返回0或1:

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

所以这可能无法解决您的问题,因为您没有描述您要实现的目标,我们在XY problem这里

答案 1 :(得分:0)

厨师食谱是纯粹的红宝石,所以你可以写出这样的东西,没有任何问题:

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

如果你需要在命令中使用双引号,你可以用反斜杠“\”来转义它。