Distributing a puppet git repo to multiple puppet masters

Arjen de Vries

When building a puppet setup with dynamic environments managed with git i needed a way to distribute the central puppet git repo to the multiple puppet masters. There are ofcourse multiple ways to accomplish this, but i wanted my changes to the central repo to be replicated to the masters instantly.To do this i’ve created a post receive hook. This post receive hook, will push all the branches to the puppetmasters defined in the script.
on the puppetmasters itself a post-receive hook is setup which will create or update the environment(s) on the master.

#!/usr/bin/env ruby
# post-receive hook
# This simple script pushes all the branches to the puppetmasters defined below.
# To add a new puppetmaster add it to hosts with the following syntax: @:
hosts = ['puppet@puppetmaster1.example.com:puppet','puppet@puppetmaster2.example.com:puppet']

# The git repo dir should be the full path to the git repo that we will be using to push from.
git_repo_dir = File.expand_path('/git/gitrepos/puppet.git')

# Read STDIN (Format: "from_commit to_commit branch_name")
from, to, branch = ARGF.read.split " "

# We've received a push with a null revision. delete te given branch
if to =~ /^0+$/
     hosts.each do | host |
     puts"Deleting #{branch} on #{host}"
         `GIT_WORK_TREE="#{git_repo_dir}" git push "#{host}" :"#{branch}"`
# Push the all the branches to the puppet masters.
    hosts.each do | host |
    puts"Pushing all branches to #{host}"
        `GIT_WORK_TREE="#{git_repo_dir}" git push --all "#{host}"`


you might also like