运行 通过 chef 脚本执行 sed 命令时出现错误
While running sed command through chef script getting error
在运行这个厨师块时:-
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
获取错误:-
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
但这些文件在同一位置可用。请帮忙解决这个问题
我不太清楚为什么你的 sed
命令失败了,但 Charles Duffy 说得有道理。
我想介绍一种更 Chef 的方式来管理配置文件。
有一个 template 资源,它从 .erb
模板添加一些变量创建文件:
# 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
如果你可以使用模板,那么你应该。但是如果你不能,比如这个文件也可以被其他软件改变,那么你可以使用Chef::Util::FileEdit
library:
How can I change a file with Chef?
在您的情况下,您可能需要:
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
在运行这个厨师块时:-
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
获取错误:-
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
但这些文件在同一位置可用。请帮忙解决这个问题
我不太清楚为什么你的 sed
命令失败了,但 Charles Duffy 说得有道理。
我想介绍一种更 Chef 的方式来管理配置文件。
有一个 template 资源,它从 .erb
模板添加一些变量创建文件:
# 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
如果你可以使用模板,那么你应该。但是如果你不能,比如这个文件也可以被其他软件改变,那么你可以使用Chef::Util::FileEdit
library:
How can I change a file with Chef?
在您的情况下,您可能需要:
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