【问题标题】:How to use special characters in Chef?如何在 Chef 中使用特殊字符?
【发布时间】:2015-08-25 17:05:14
【问题描述】:

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

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

方法一:将代码放入任意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 linux chef-infra chef-recipe chef-solo


    【解决方案1】:

    这与命令中的+无关!

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

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

    此命令返回状态码 1,因为没有找到(man grep 的重点是我的引用):

    退出状态

    如果找到选定的行,则退出状态为 0,如果未找到,则为 1。如果发生错误,退出状态为 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 此处

    【讨论】:

      【解决方案2】:

      Chef recipes 是纯红宝石,所以你可以毫无问题地写出这样的东西:

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

      如果您需要在命令中使用双引号,可以使用反斜杠“\”对其进行转义。

      【讨论】:

      • 您创建命令没有问题,但执行的命令被重新调整退出状态1。您确定您输入的命令可以成功执行(有效)?
      • 是的,我可以手动运行它,它现在没有返回任何输出。但如果这些类型的条目将保留在/etc/shadow 中,那么它也会显示该条目。我在其他社区发现了同样的问题,但没有回复,或者可能是厨师不允许或可能是其他使用方法。
      • 如果您能够手动运行此程序,则可能是 Chef 不在好目录中。尝试将选项 cwd 添加到 bash 资源,而不是在命令中使用它。你可以在这里找到文档:docs.chef.io/resource_bash.html。简单:将目录放在cwd 选项中,例如:cwd '/etc/shadow',然后尝试使用grep,无需指定目录。
      【解决方案3】:

      我用

      解决了我的问题
      `/bin/grep '^+:' /etc/passwd >> /var/output`
      

      这是使用反引号执行系统命令的ruby方法,其中全局变量$?通过反引号自动设置

      【讨论】:

      • 警告,它将在编译时执行而不是在收敛时执行,并且不会作为审计报告提供,我真的很奇怪你为什么不依赖ohai属性哈希node['etc']['passwd']并在这种情况下使用 ruby​​ 方法对其进行过滤。
      • 我正在使用另一个配方生成审计报告
      • 所以利用节点属性而不是重做ohai已经完成的工作。
      • 是的,我在我的食谱中使用了很多地方,但我只是想以不同的方式使用它。然后我收到了这个错误,通过分享我了解了我不知道的exit status。但无论如何,非常感谢 Tensibai。
      猜你喜欢
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多