Skip to content

Instantly share code, notes, and snippets.

@kkeane
Created May 26, 2015 01:20
Show Gist options
  • Save kkeane/38020a052a73067c3812 to your computer and use it in GitHub Desktop.
Save kkeane/38020a052a73067c3812 to your computer and use it in GitHub Desktop.
Using chef to creating a DNS zone file idempotently with auto-incrementing serial numbers
# Using chef to create files with a time stamp or similar - in this case,
# the serial number in a DNS zone file - presents a problem, because the
# time stamp interferes with with idempotency. Here is a trick for solving
# this (thanks to Noah Kantrowitz for his input and suggestion!)
# The trick is to use the same template twice, once with a constant serial
# number to check for idempotency, and a second time with the "real" serial
# number. The actual serial number can be stored in an attribute.
# Initial serial number for the zone
node.default['myzone']['serial'] = 1
# Use node.set so the default is only used the first time round.
# Don't use node.override because that would not be saved to the
# node!
ruby_block "dns trick for myzone" do
block do
node.set['myzone']['serial'] = node['myzone']['serial'] + 1
end
action :nothing
end
# the template for checking for idempotency.
# If you want to manually force incrementing the serial number,
# you simply delete /tmp/myzone.withoutserial, and the next
# chef run will regenerate it and trigger incrementing the
# serial number, as well
template "/tmp/myzone.withoutserial" do
source "zonetemplate.erb"
variables(
:serialnumber => 0,
:data => zonedata
)
notifies :run, "ruby_block[dns trick for myzone]", :immediate
end
# the real template is almost identical to the one without serial number
# lazy evaluation so we pick up the new serial number.
template "/tmp/myzone" do
source "zonetemplate.erb"
variables lazy {
{
:serialnumber => node['myzone']['serial'],
:data => zonedata
}
}
# notify BIND or whatever else you may need to do here
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment