厨师食谱(Ruby)
我正在制作厨师食谱,该食谱将根据Linux服务器中的内存总数拉下程序安装程序。如果内存总数为8GB或更多。...如果内存小于8GB,则安装...。有人碰巧知道如何脚本吗? 厨师是基于红宝石的。 在我的厨师食谱中,我尝试了以下内容,但没有成功。
puts "***** Linux server node['platform_family']=#{node['platform_family']}"
puts "***** Linux server node['memory.total']=#{node['memory.total']}"
# -------------------------------------------------------------------------------
puts "*****#{node['platform_family']}"
puts "*****#{node['memory.total']}"
if node['platform_family'] == 'debian' && node['memory.total'] >= 7000000
remote_file '/tmp'
source 'local file'
action :create
end
elsif
remote_file '/tmp'
source 'external file'
action :create
end
I'm working on a chef recipe that will pull down a program installer depending on the memory total in a linux server. If the memory total is 8GB or more install .... if the memory is less than 8GB then install ... . Does anybody happen to know how to script this?
Chef is ruby based.
Within my chef recipes I have attempted the following, but had no success.
puts "***** Linux server node['platform_family']=#{node['platform_family']}"
puts "***** Linux server node['memory.total']=#{node['memory.total']}"
# -------------------------------------------------------------------------------
puts "*****#{node['platform_family']}"
puts "*****#{node['memory.total']}"
if node['platform_family'] == 'debian' && node['memory.total'] >= 7000000
remote_file '/tmp'
source 'local file'
action :create
end
elsif
remote_file '/tmp'
source 'external file'
action :create
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我们可以事先计算
source
属性的值,则remote_file
资源可以以更好的方式(一次)编写。另外,由于您已经显示了node ['platform_family']
的比较,因此您似乎还需要匹配debian
以外的OS家族。以下代码将匹配
platform_family
和['MOMERY'] ['total']
,并设置一个可以与source
property一起使用的值。请注意,我正在使用 ternary oterator 代码>如果/
else
在case> case
语句的情况时。The
remote_file
resource can written in a better way (once) if we can compute the value ofsource
property beforehand. Also since you have shown comparison ofnode['platform_family']
, it appears you would need to match for OS families other thandebian
as well.Below code will match for
platform_family
and['memory']['total']
and set a value that we can use with thesource
property.Note that I'm using ternary operator as an alternative to
if
/else
withinwhen
condition ofcase
statement.如果您只想在Linux中使用此食谱,那么此方法将起作用:
不同的OSS以不同的方式报告此值,但并非总是以相同的格式报告。因此,如果您想支持任何系统平台(以防万一使用不使用Linux的人使用),这将是一个更好的选择:
If you're only ever going to use this recipe in Linux, then this method will work:
Different OSes report this value differently and not always in the same format though. So if you want to support any system platform (just in case this has a use for someone not using Linux) this would be a far better option: