Skip to content

Instantly share code, notes, and snippets.

@sarva
Created May 10, 2011 08:23
Show Gist options
  • Save sarva/964092 to your computer and use it in GitHub Desktop.
Save sarva/964092 to your computer and use it in GitHub Desktop.
Puppet defines to build a complex file across all nodes. Useful for creating config files containing IP addresses of nodes in distributed systems.
# Export a single line
#
# == Parameters
#
# [*namevar*]
# Line of text to include in the file to build
#
# [*file*]
# The file the supplied line will be included in
#
# == Examples
# exportline { "$name $ipaddress":
# file => "/etc/app/clients"
# }
#
define exportline($file) {
$tag = regsubst($file, "/", "-", "G")
@@exec { "check $name":
command => "/bin/true",
unless => "grep '$name' $file",
tag => "export$tag",
notify => Exec["clear $file"]
}
@@exec { "set $name":
command => "echo '$name' >> $file",
refreshonly => true,
subscribe => Exec["clear $file"],
tag => "export$tag"
}
}
# Collect all exported lines of data and build the
# desired file.
#
# The resulting file can be referenced with the
# File["/path/to/file"} resource to meet order
# requirement needs
#
# == Parameters
#
# [*namevar*]
# File to build and put all lines into
#
# == Examples
# collectfile { "/etc/app/clients": }
#
# exec { "test":
# require => File{"/etc/app/clients"]
# }
#
define collectfile($trigger=false) {
$tag = regsubst($name, "/", "-", "G")
file {$name:
ensure => file
}
exec { "clear $name":
command => "echo '' |> $name",
refreshonly => true
}
Exec<<| tag=="export$tag" |>> {
before => File["$name"]
}
if $trigger {
File["$name"] {
notify => $trigger,
before => $trigger
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment