Skip to content

Instantly share code, notes, and snippets.

@grahamgilbert
Created August 29, 2012 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grahamgilbert/3515040 to your computer and use it in GitHub Desktop.
Save grahamgilbert/3515040 to your computer and use it in GitHub Desktop.
Script to write the puppet.conf for a Mac client using the serial as the certname
#!/usr/bin/env python
import subprocess
import re
from os import close
##put your puppet server here:
puppetserver = "puppet.example.com"
##get the system serial number
the_command = "ioreg -c \"IOPlatformExpertDevice\" | awk -F '\"' '/IOPlatformSerialNumber/ {print $4}'"
serial = subprocess.Popen(the_command,shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]
serial = re.sub(r'\s', '', serial)
##if it stars with a number, put aaa in front to make it a valid hostname (otherwise puppet will complain)
if serial[0].isdigit():
serial = "aaa"+serial
##write the conf
data = "[main]\nlogdir=/var/log/puppet\nvardir=/var/lib/puppet\nssldir=/var/lib/puppet/ssl\n#rundir=/var/run/puppet\nfactpath=$vardir/lib/facter\ntemplatedir=$confdir/templates\n\n[master]\n# These are needed when the puppetmaster is run by passenger\n# and can safely be removed if webrick is used.\nssl_client_header = SSL_CLIENT_S_DN \nssl_client_verify_header = SSL_CLIENT_VERIFY\n\n[agent]\nserver="+puppetserver+"\ncertname="+serial.lower()+"\nreport=true\npluginsync=true"
the_command = "/usr/bin/touch /etc/puppet/puppet.conf"
p=subprocess.Popen(the_command,shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
file = open("/etc/puppet/puppet.conf", "w")
file.write(data)
file.close()
the_command="/usr/bin/puppet agent --server "+puppetserver+" --test"
puppet = subprocess.Popen(the_command,shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]
@vmule
Copy link

vmule commented Nov 19, 2014

why don't just run, before the puppet agent run:

puppet resource file /tmp/puppet.conf ensure=present content=`facter |grep serial |awk '{print $3}'`

or if you don't want to use facter you can run the same command to grep the serial:
the_command=ioreg -c \"IOPlatformExpertDevice\" | awk -F '\"' '/IOPlatformSerialNumber/ {print $4}'
puppet resource file /tmp/puppet.conf ensure=present content=$the_command

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment