如何在 Chef 中使用特殊字符?
How to use special characters in Chef?
我正在使用 chef-solo
编写 Chef 食谱,我需要 运行 包含 +
的命令,但是如果我使用 [=15] Chef returns 会出错=] 字符。
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)
大厨的菜谱很纯粹ruby,这样写也不会出问题:
bash "testing" do
code "/bin/grep '^+:' /etc/shadow >>/var/info"
end
如果您需要在命令中使用双引号,可以使用反斜杠“\”进行转义。
这与命令中的+无关!
没问题,厨师工作正常:
# grep '^+' /etc/shadow; echo $?
1
此命令 return 状态代码为 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.
所以如果你想接受这个命令return 0 or 1:
bash "testing" do
code "/bin/grep '^+:' /etc/shadow >>/var/info"
return [0,1]
end
所以这可能无法解决您的问题,因为您没有描述您想要实现的目标,我们在 XY problem 此处
我使用
解决了我的问题
`/bin/grep '^+:' /etc/passwd >> /var/output`
这是ruby使用反引号执行系统命令的方法,其中全局变量$?
通过反引号自动设置
我正在使用 chef-solo
编写 Chef 食谱,我需要 运行 包含 +
的命令,但是如果我使用 [=15] Chef returns 会出错=] 字符。
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)
大厨的菜谱很纯粹ruby,这样写也不会出问题:
bash "testing" do
code "/bin/grep '^+:' /etc/shadow >>/var/info"
end
如果您需要在命令中使用双引号,可以使用反斜杠“\”进行转义。
这与命令中的+无关!
没问题,厨师工作正常:
# grep '^+' /etc/shadow; echo $?
1
此命令 return 状态代码为 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, ArrayThe 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.
所以如果你想接受这个命令return 0 or 1:
bash "testing" do
code "/bin/grep '^+:' /etc/shadow >>/var/info"
return [0,1]
end
所以这可能无法解决您的问题,因为您没有描述您想要实现的目标,我们在 XY problem 此处
我使用
解决了我的问题`/bin/grep '^+:' /etc/passwd >> /var/output`
这是ruby使用反引号执行系统命令的方法,其中全局变量$?
通过反引号自动设置