如何通过参数在厨师中执行?
我是厨师的新手。最近,我正在研究资源,我遇到了一个问题,即如何传递参数以命令在厨师中执行。这是细节 - 我搜索了此页面 - https://discourse.chef.io/t/pass-parameter-to-bash-script-script-script-and-basl-bash-scripts-in-chef-cookbook/10431 使用Ruby变量格式#{xxx}的外观可以将食谱中的变量传递给配方中的``execute'ecare'' 我进行了测试,首先是我的食谱 -
execf="haha"
file '/tmp/e.sh' do
content '
#!/bin/bash
cat /tmp/e.log
DT=`date +%F" "%T`
echo $1" "${DT} >> /tmp/e.log
'
mode '755'
execf="e1"
notifies :run, 'execute[e.sh]', :delayed
end
file '/tmp/e2.sh' do
content '
#!/bin/bash
cat /tmp/e.log
DT=`date +%F" "%T`
echo $1" "${DT} >> /tmp/e.log
'
mode '777'
execf="e2"
notifies :run, 'execute[e.sh]', :delayed
end
execute 'e.sh' do
command '/tmp/e.sh #{execf}'
action :nothing
end
我认为 - 这可以记录reasouce“ e.sh”或“ e2.sh”的运行时间到e.log。运行“厨师 - 客户”时,没有语法错误。但是令我惊讶的是#{execf} - 始终是“”
2022-05-13 22:04:48
2022-05-13 22:17:44
2022-05-13 22:19:07
字符串/tmp/e.sh as $ 1 ...一开始,我认为这可能是由于execf是Ruby中的局部变量,因此我尝试了@Execf和$ execf ...但所有这些都没有起作用。也许在食谱中,我不应该使用变量,而应该使用节点属性?但是我找不到如何在资源中修改节点属性... 请帮助。事先感谢您的任何想法。
I'm new to chef. Recently, I'm studying the resources, I got a problem on how to pass parameter to command in execute in chef. Here's the details --
I searched this page -- https://discourse.chef.io/t/pass-parameter-to-bash-script-and-call-bash-scripts-in-chef-cookbook/10431 -- it looks using ruby variable format #{xxx} can pass a variable in recipe to the script in "execute" in recipe
I made a test, first my recipe --
execf="haha"
file '/tmp/e.sh' do
content '
#!/bin/bash
cat /tmp/e.log
DT=`date +%F" "%T`
echo $1" "${DT} >> /tmp/e.log
'
mode '755'
execf="e1"
notifies :run, 'execute[e.sh]', :delayed
end
file '/tmp/e2.sh' do
content '
#!/bin/bash
cat /tmp/e.log
DT=`date +%F" "%T`
echo $1" "${DT} >> /tmp/e.log
'
mode '777'
execf="e2"
notifies :run, 'execute[e.sh]', :delayed
end
execute 'e.sh' do
command '/tmp/e.sh #{execf}'
action :nothing
end
I think -- this can record resouce "e.sh" or "e2.sh"'s runtime to e.log. And there's no syntax error when run "chef-client". But to my suprise -- the #{execf} -- is always a "" string while calling /tmp/e.sh... So in e.log, I can only see --
2022-05-13 22:04:48
2022-05-13 22:17:44
2022-05-13 22:19:07
Only a "" was passing to the /tmp/e.sh as $1... At first I think maybe it's due to the execf is a local variable in ruby so I tried @execf and $execf ... but all didn't work. Maybe in recipe, I shouldn't use variable, but should use node attribute? But I can't find how to modify node attribute in resource...
Please kind help. Thanks in advance for any idea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
主要问题是在
命令
中使用单人引号(''
)。 Ruby中的单引号不允许插值#{execf}
。您应该将其重写为:,但是
file> file
资源中的变量分配(例如execf =“ e1”
)将不会产生任何效果。您将在日志中获得类似HAHA 2022-05-13 22:04:48
。编辑
事实证明,还编制了来自资源内部的变量分配,并使用了最后一个分配的值。
The main issue is the use of single quotes (
''
) in thecommand
. Single quotes in Ruby don't allow for interpolation of the variable#{execf}
. You should rewrite it as:However variable assignment (such as
execf="e1"
) in thefile
resources will not have any effect. You will get something likehaha 2022-05-13 22:04:48
in the logs.Edit
Turns out that the variable assignment from within resource is also compiled, and last assigned value is used.