Vinod
Vinod

Reputation: 83

sed command using chef

Hi I am using sed command in chef recipe but its throwing error. But If I run just sed command on CLI it works. Any help appreciated.

My sed command is -

sed -i -e "/<BigSQL>/,/<\/BigSQL>/ s|<username>[0-9a-z.]\{1,\}</username>|<username>bigsql</username>|" /home/biadmin/install.xml

And my chef recipe is -

bash "change_bigsqluser" do
user "root"
code <<-EOH
sed -i -e "/<BigSQL>/,/<\/BigSQL>/ s|<username>[0-9a-z.]\{1,\}</username>|<username>bigsql</username>|" /home/biadmin/install.xml
EOH
end

Here is the error

Error executing action `run` on resource 'bash[change_bigsqluser]'
================================================================================


Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '1'
---- Begin output of "bash"  "/tmp/chef-script20140807-4025602-160wvt2" ----
STDOUT:
STDERR: sed: -e expression #1, char 15: unknown command: `B'
---- End output of "bash"  "/tmp/chef-script20140807-4025602-160wvt2" ----
Ran "bash"  "/tmp/chef-script20140807-4025602-160wvt2" returned 1


Resource Declaration:
---------------------
# In /var/chef/cache/cookbooks/biginsights/recipes/temp.rb

 22: bash "change_bigsqluser" do
 23: user "root"
 24: code <<-EOH
 25: sed -i -e "/<BigSQL>/,/<\/BigSQL>/ s|<username>[0-9a-z.]\{1,\}</username>|<username>bigsql</username>|" /home/biadmin/install.xml
 26: EOH
 27: end



Compiled Resource:
------------------
# Declared in /var/chef/cache/cookbooks/biginsights/recipes/temp.rb:22:in `from_file'

bash("change_bigsqluser") do
  action "run"
  retries 0
  retry_delay 2
  guard_interpreter :default
  command "\"bash\"  \"/tmp/chef-script20140807-4025602-160wvt2\""
  backup 5
  returns 0
  user "root"
  code "sed -i -e \"/<BigSQL>/,/</BigSQL>/ s|<username>[0-9a-z.]{1,}</username>|<username>bigsql</username>|\" /home/biadmin/install.xml\n"
  interpreter "bash"
  cookbook_name "biginsights"
  recipe_name "temp"
end



[2014-08-07T04:25:49-07:00] INFO: Running queued delayed notifications before re-raising exception

Running handlers:
[2014-08-07T04:25:49-07:00] ERROR: Running exception handlers
Running handlers complete

[2014-08-07T04:25:49-07:00] ERROR: Exception handlers complete
[2014-08-07T04:25:49-07:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
Chef Client failed. 0 resources updated in 7.374318882 seconds
[2014-08-07T04:25:49-07:00] ERROR: bash[change_bigsqluser] (biginsights::temp line 22) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'
---- Begin output of "bash"  "/tmp/chef-script20140807-4025602-160wvt2" ----
STDOUT:
STDERR: sed: -e expression #1, char 15: unknown command: `B'
---- End output of "bash"  "/tmp/chef-script20140807-4025602-160wvt2" ----
Ran "bash"  "/tmp/chef-script20140807-4025602-160wvt2" returned 1
[2014-08-07T04:25:49-07:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

Upvotes: 1

Views: 3421

Answers (2)

brad
brad

Reputation: 156

Since this came up as the first result in Google, I thought I've provide an answer for completeness.

If you look in the compiled resource, the code starts off like this:

sed -i -e \"/<BigSQL>/,/</BigSQL>/

The issue is with escaping. Just change: code <<-EOH to code <<-'EOH':

bash "change_bigsqluser" do
user "root"
code <<-'EOH'
sed -i -e "/<BigSQL>/,/<\/BigSQL>/ s|<username>[0-9a-z.]\{1,\}</username>|<username>bigsql</username>|" /home/biadmin/install.xml
EOH
end

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 158120

Since you are executing just a single command and you are not using any bash features, I would use an execute resource:

execute "Do some sed" do
    command "sed -i '/<BigSQL>/,/</BigSQL>/ s|<username>[0-9a-z.]{1,}</username>|<username>bigsql</username>|' /home/biadmin/install.xml"
    action :run
end

Upvotes: 4

Related Questions