Created
August 29, 2012 16:15
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
why don't just run, before the puppet agent run:
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