在使用 chef 11.x 实现模板化配置文件时,我想在文件更新时将当前日期/时间插入到文件中。
例如:
# Created By : core::time-settings
# On : <%= Time.now %>
显然,这会在每个配方运行时进行评估,并不断更新目标文件,即使其他属性都正常——这是不希望的。
因此有人知道解决方案吗?我不知道 Chef 中有任何内置逻辑可以实现这一点,也不知道我可以在 ruby block 中评估的内置 chef 变量只有在其他属性不合规时才会为真(因为这会提供潜在的解决方法)。
我知道我可以运行一个执行类型的操作,它只会在模板资源被触发后运行,并且它会扩展文件中的一个变量来实现这一点,但我不喜欢这样做的概念或想法.
谢谢,
最佳答案
虽然我同意 Tensibai您期望的不是 Chef 的用途。
我想添加的(因为前段时间我搜索了很长时间)是如何在文件中包含当前时间戳,一旦它被 Chef 修改(你必须以某种方式避免它总是更新时间戳)。
可以查到结果here ,简化的,未经测试的版本:
time = Time.new.strftime("%Y%m%d%H%M%S")
template "/tmp/example.txt" do
source "/tmp/example.txt.erb" # the source is not in a cookbook, but on the disk of the node!
local true
variables(
:time => time
)
action :nothing
end
template "/tmp/example.txt.erb" do
variables(
variable1 => "test"
)
notifies :create, resources(:template => "/tmp/example.txt"), :immediately
end
每当 /tmp/example.txt.erb
的内容发生变化时,它都会触发写入 /tmp/example.txt
- 以 /tmp/example.txt.erb
作为模板来自本地磁盘而不是 Recipe (因为 local true
)并用当前时间替换 time
变量.
因此,在编写 /tmp/example.txt
时唯一需要替换的变量是时间,因此 example.txt.erb
模板如下所示:
# my template
time: <%%= @time %>
other stuff here.. <%= @variable1 %>
关于ruby - Chef 模板 - 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28068099/