linux - Chef Recipe - 用 sed 命令替换 Linux 文件中的文本(接收错误)

标签 linux chef-infra chef-recipe cookbook test-kitchen

我在 Chef Recipe 中使用了以下代码:

bash 'replace_text' do
    cwd ::File.dirname('/data01/nodemanager/bin')
    code <<-EOH
      sed 's/NODEMGR_HOME=.*/NODEMGR_HOME="\/data01\/nodemanager"/g' startNodeManager.sh
      EOH
  end

我要替换的文本是包含 NODEMGR_HOME= startNodeManager.sh 的行中的所有内容,位于 /data01/nodemanager/bin

并且我在运行 Recipe 时收到以下输出:

       Running handlers:
       [2017-11-21T23:58:22+00:00] ERROR: Running exception handlers
       [2017-11-21T23:58:22+00:00] ERROR: Running exception handlers
       Running handlers complete
       [2017-11-21T23:58:22+00:00] ERROR: Exception handlers complete
       [2017-11-21T23:58:22+00:00] ERROR: Exception handlers complete
       Chef Client failed. 0 resources updated in 01 seconds
       [2017-11-21T23:58:22+00:00] FATAL: Stacktrace dumped to /tmp/kitchen/cache/chef-stacktrace.out
       [2017-11-21T23:58:22+00:00] FATAL: Stacktrace dumped to /tmp/kitchen/cache/chef-stacktrace.out
       [2017-11-21T23:58:22+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
       [2017-11-21T23:58:22+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
       [2017-11-21T23:58:22+00:00] ERROR: bash[replace_text] (domain-test::sed2 line 1) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'
       ---- Begin output of "bash"  "/tmp/chef-script20171121-11258-vwwwm6" ----
       STDOUT:
       STDERR: sed: -e expression #1, char 34: unknown option to `s'
       ---- End output of "bash"  "/tmp/chef-script20171121-11258-vwwwm6" ----
       Ran "bash"  "/tmp/chef-script20171121-11258-vwwwm6" returned 1
       [2017-11-21T23:58:22+00:00] ERROR: bash[replace_text] (domain-test::sed2 line 1) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'
       ---- Begin output of "bash"  "/tmp/chef-script20171121-11258-vwwwm6" ----
       STDOUT:
       STDERR: sed: -e expression #1, char 34: unknown option to `s'
       ---- End output of "bash"  "/tmp/chef-script20171121-11258-vwwwm6" ----
       Ran "bash"  "/tmp/chef-script20171121-11258-vwwwm6" returned 1
       [2017-11-21T23:58:22+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
       [2017-11-21T23:58:22+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
>>>>>> ------Exception-------
>>>>>> Class: Kitchen::ActionFailed
>>>>>> Message: 1 actions failed.
>>>>>>     Converge failed on instance <default-ubuntu-1404>.  Please see .kitchen/logs/default-ubuntu-1404.log for more details
>>>>>> ----------------------
>>>>>> Please see .kitchen/logs/kitchen.log for more details
>>>>>> Also try running `kitchen diagnose --all` for configuration

最佳答案

您的反斜杠转义级别不够。请记住,它首先是一个 Ruby 字符串,然后是一个 bash 字符串,然后是一个 sed 参数。您也可以使用 linepoise-file 说明书来回避整个问题。

关于linux - Chef Recipe - 用 sed 命令替换 Linux 文件中的文本(接收错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47424806/

相关文章:

linux - 如何使用正则表达式进行 grep 以消除此堆栈跟踪?

java - JAVA(tomcat)中的环境变量

ruby - 使用切片!在变量上修改填充变量的节点属性

chef-infra - 进行 SVN 结帐的 Chef Recipe

ubuntu - 某环境下的 Knife 列表内核版本

linux - 如何使用 native 网络系统从 Pd Vanilla > 0.46 控制 VLC?

linux - 在 Linux 中只输入一条命令和在执行 '/bin/sh' 后输入相同的命令有什么区别?

yaml - 在 Test Kitchen 中使用类似的 "suites"进行代码重用?

chef-infra - 仅当特定属性更改时才重新启动服务

ruby - Chef ServerSpec SpecInfra 未初始化常量 NameError