Oh, nice one, thank you. Using a file seems somewhat awkward, so I'm actually using an attribute instead. Works fine, although I haven't figured out the correct lazy syntax yet, so my version currently takes two iterations until the serial number is updated. I have three variables passing into the template, and only the serial number should be lazy.
The point about using include in BIND is well taken, but moot. If I was using BIND, BIND actually has a nicer way of handling this: don't generate the zone file at all, but use nsupdate dynamic updates instead; that automatically handles serial numbers. I am doing that in a different context already.
In this case, the zone file actually serves as input to opendnssec signing.
BTW, the ruby block also needs to have action :nothing set. I know you know this, but I mention it in case somebody else finds this post later through Google.
Kevin Keane
The NetTech
Our values: Privacy, Liberty, Justice
See https://www.4nettech.com/corp/the-nettech-values.html
-----Original message-----
From: Noah Kantrowitz noah@coderanger.net
Sent: Monday 25th May 2015 16:27
To: chef@lists.opscode.com
Subject: [chef] Re: Using time stamps in a template?
No, I would have two template resources, one acting as an idempotence check for the sans-serial data and the other is the full content with serial. A better approach is to try to keep the serial in its own file and use an include. A rough example of the first (not actually going to test this):
template '/etc/bind/myzone.partial' do
source 'whatever.erb'
variables serial: 0
notifies :run, 'ruby_block[dns trick]', :immediately
end
ruby_block 'dns trick' do
block do
path = '/etc/bind/myzone.serial'
IO.write(path, IO.read(path).to_i + 1)
end
end
template '/etc/bind/myzone' do
source 'whatever.erb'
variables lazy { {serial: IO.read('/etc/bind/myzone.serial') } }
end
This shows two templates, when the serial=0 one updates it bumps the serial file, which the second will use to generate the real zone config. The second approach might be possible with $INCLUDE in BIND but I'm not familiar enough with its usage to show an example.
--Noah
On May 25, 2015, at 4:10 PM, Kevin Keane Subscription subscription@kkeane.com wrote:
Interesting thought. Basically, you are suggesting that the template generate an intermediate file, and then in a separate step, I'd use something like sed or awk to insert the actual time stamp.
Thank you! I knew somebody had a solution for my problem!
Kevin Keane
The NetTech
Our values: Privacy, Liberty, Justice
See https://www.4nettech.com/corp/the-nettech-values.html
-----Original message-----
From: Noah Kantrowitz noah@coderanger.net
Sent: Monday 25th May 2015 12:20
To: chef@lists.opscode.com
Subject: [chef] Re: Using time stamps in a template?You would have to implement the idempotence check yourself separate from the file contents. You could do something like make a template with the serial always set to 0, and use a notification that when that template changes trigger a ruby_block to bump the serial number on disk and write out the real template.
--Noah
On May 25, 2015, at 8:26 AM, Kevin Keane Subscription subscription@kkeane.com wrote:
I am looking for a way to put a time stamp into a template, without causing the file to be regenerated on each chef run. I want the time stamp to be updated only if something else has changed in the template, as well.
The use case is for generating a DNS zone file and updating the serial number only when there was a change.
Is there a common solution for this problem in Chef?
Kevin Keane
The NetTech
760-721-8339
Our values: Privacy, Liberty, Justice