如何通过参数在厨师中执行?

发布于 2025-01-29 05:13:24 字数 1293 浏览 2 评论 0原文

我是厨师的新手。最近,我正在研究资源,我遇到了一个问题,即如何传递参数以命令在厨师中执行。这是细节 - 我搜索了此页面 - 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸短情长 2025-02-05 05:13:24

主要问题是在命令中使用单人引号('')。 Ruby中的单引号不允许插值#{execf}。您应该将其重写为:

execute 'e.sh' do
  command "/tmp/e.sh #{execf}"
  action :nothing
end

,但是file> file资源中的变量分配(例如execf =“ e1”)将不会产生任何效果。您将在日志中获得类似HAHA 2022-05-13 22:04:48

编辑

但是,文件>文件中的变量分配(例如execf =“ e1”资源将没有任何效果。

事实证明,还编制了来自资源内部的变量分配,并使用了最后一个分配的值。

The main issue is the use of single quotes ('') in the command. Single quotes in Ruby don't allow for interpolation of the variable #{execf}. You should rewrite it as:

execute 'e.sh' do
  command "/tmp/e.sh #{execf}"
  action :nothing
end

However variable assignment (such as execf="e1") in the file resources will not have any effect. You will get something like haha 2022-05-13 22:04:48 in the logs.

Edit

However variable assignment (such as execf="e1") in the file resources will not have any effect.

Turns out that the variable assignment from within resource is also compiled, and last assigned value is used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文