Keshav Kishor
Keshav Kishor

Reputation: 71

While running sed command through chef script getting error

While runnig this chef block:-

bash 'Do some sed' do
    user node['aem_dispatcher_cookbook']['owner']['user']
    group node['aem_dispatcher_cookbook']['owner']['group']
    cwd '/ebiz'
    code <<-EOH
    source /etc/profile.d/disp_profile.sh
      sed  -i -e 's/umask 007/umask 022/g' #{node['aem_dispatcher_cookbook']['user']['home']}/.profile
      sed  -i -e 's/umask 007/umask 022/g' #{node['aem_dispatcher_cookbook']['user']['home']}/.wsenv
      sed  -i -e 's/AUTHOR_HOST/'$AUTHOR_HOST'/g' #{node['aem_dispatcher_cookbook']['owner']['home']}/conf.d/author_farm.any
      sed  -i -e 's/APORT/'$APORT'/g' #{node['aem_dispatcher_cookbook']['owner']['home']}/conf.d/author_farm.any
      sed  -i -e 's/URL1/'$URL'/g' #{node['aem_dispatcher_cookbook']['owner']['home']}/conf.d/author_farm.any
      sed  -i -e 's/PUBLISH_HOST/'$PUBLISH_HOST'/g' #{node['aem_dispatcher_cookbook']['owner']['home']}/conf.d/publish_farm.any
      sed  -i -e 's/PPORT/'$PPORT'/g' #{node['aem_dispatcher_cookbook']['owner']['home']}/conf.d/publish_farm.any  
      sed  -i -e 's/P_HOST_ADDY/'$P_HOST_ADDY'/g' #{node['aem_dispatcher_cookbook']['owner']['home']}/conf.d/publish_farm.any
    EOH
  end

Getting error:-

STDERR: /tmp/chef-script20200518-26241-1f7ynpy: line 2: sed  -i -e s/umask 007/umask 022/g /home/aemadmin/.profile: No such file or directory
/tmp/chef-script20200518-26241-1f7ynpy: line 3: sed  -i -e s/umask 007/umask 022/g /home/aemadmin/.wsenv: No such file or directory
/tmp/chef-script20200518-26241-1f7ynpy: line 4: sed  -i -e s/AUTHOR_HOST//g /ebiz/apache/conf.d/author_farm.any: No such file or directory
/tmp/chef-script20200518-26241-1f7ynpy: line 5: sed  -i -e s/APORT/8080/g /ebiz/apache/conf.d/author_farm.any: No such file or directory
/tmp/chef-script20200518-26241-1f7ynpy: line 6: sed  -i -e s/URL1/dev.uhg/g /ebiz/apache/conf.d/author_farm.any: No such file or directory
/tmp/chef-script20200518-26241-1f7ynpy: line 7: sed  -i -e s/PUBLISH_HOST//g /ebiz/apache/conf.d/publish_farm.any: No such file or directory
/tmp/chef-script20200518-26241-1f7ynpy: line 8: sed  -i -e s/PPORT/8080/g /ebiz/apache/conf.d/publish_farm.any  : No such file or directory
/tmp/chef-script20200518-26241-1f7ynpy: line 9: sed  -i -e s/P_HOST_ADDY//g /ebiz/apache/conf.d/publish_farm.any: No such file or directory
---- End output of "bash"  "/tmp/chef-script20200518-26241-1f7ynpy" ----
Ran "bash"  "/tmp/chef-script20200518-26241-1f7ynpy" returned 127

But these files are available in same location. Please help solve this issue

Upvotes: 1

Views: 217

Answers (1)

Draco Ater
Draco Ater

Reputation: 21226

I don't know exactly, why your sed command fails, but Charles Duffy has a point.

I would like to introduce a more Chef way of managing configuration files.

There is a template resource, that creates a file from an .erb template adding some variables:

# template (templates/default/file.conf.erb)
key1 = value1
key2 = <%= @value2 %>

# recipe
template '/etc/conf/file.conf' do
  source 'file.conf.erb'
  variables(value2: node['value2'])  # this will set @value2 in template to node['value2']
end

If you can use template, then you should. But if you can't, for example this file can be also changed by other software, then you can use Chef::Util::FileEdit library:

How can I change a file with Chef?

In your case you will probably need:

ruby_block "Do some regex replacement" do
  block do
    fe = Chef::Util::FileEdit.new("#{node['aem_dispatcher_cookbook']['user']['home']}/.profile")
    fe.search_file_replace('umask 007', 'umask 022')
    fe.write_file

    fe = Chef::Util::FileEdit.new("#{node['aem_dispatcher_cookbook']['owner']['home']}/conf.d/author_farm.any")
    fe.search_file_replace('AUTHOR_HOST', ENV['AUTHOR_HOST'])
    fe.write_file
    [.. and similarly with other files ..]
  end
end

Upvotes: 1

Related Questions