Bootstrapping Puppet with Rake
To make a newly provisioned machine part of our Puppet infrastructure, we just need to run a few commands on it, so let's make this process even easier by adding a new bootstrap task to the Rakefile
.
Getting ready...
To get ready for the recipe, do the following:
Add the following line to the top of your
Rakefile
:REPO = '[email protected]:bitfield/cookbook.git'
Add the following task anywhere in the
Rakefile
:desc "Bootstrap Puppet on ENV['CLIENT'] with hostname ENV['HOSTNAME']" task :bootstrap do client = ENV['CLIENT'] hostname = ENV['HOSTNAME'] || client commands = <<BOOTSTRAP sudo hostname #{hostname} && \ sudo su - c 'echo #{hostname} >/etc/hostname' && \ wget http://apt.puppetlabs.com/puppetlabs-release-precise.deb && \ sudo dpkg -i puppetlabs-release-precise.deb && \ sudo apt-get update && sudo apt-get -y install git puppet && \ git clone #{REPO} puppet && \ sudo puppet apply --modulepath=/home/ubuntu/puppet /modules /home/ubuntu/puppet/manifests/site.pp BOOTSTRAP sh "#{SSH} #{client} '#{commands}'" end
How to do it...
You'll need a freshly provisioned server (one that you can log in to, but that doesn't have Puppet installed or any other config changes made on it). If you're using EC2, create a new EC2 instance. Get the public instance address from the AWS control panel; it'll be something like:
ec2-107-22-22-159.compute-1.amazonaws.com
Here are the steps to bootstrap the new server using Rake:
Add a node declaration to your
nodes.pp
file for the hostname you'll be using on the new server. For example, if you wanted to call itcookbook-test
, you could usenode 'cookbook-test' { include puppet }
Run the following command in the Puppet repo on your own machine (substitute the address of the new server as the value of
CLIENT
, and the hostname you want to use as the value ofHOSTNAME
). The command should all be on one line:$ rake CLIENT=ec2-107-22-22-159.compute-1.amazonaws.com HOSTNAME=cookbook-test bootstrap
You'll see output something like the following:
(in /Users/john/git/cookbook) ssh -A -i ~/git/bitfield/bitfield.pem -l ubuntu ec2-107-22-22-159.compute-1.amazonaws.com 'sudo hostname cookbook-test && sudo su -c 'echo cookbook-test >/etc/hostname' && wget http://apt.puppetlabs.com/puppetlabs-release-precise.deb && sudo dpkg -i puppetlabs-release-precise.deb && sudo apt-get update && sudo apt-get -y install git puppet && git clone [email protected]:bitfield/cookbook.git puppet && sudo puppet apply --modulepath=/home/ubuntu/puppet/modules /home/ubuntu/puppet/manifests/site.pp' The authenticity of host 'ec2-107-22-22-159.compute-1.amazonaws.com (107.22.22.159)' can't be established. RSA key fingerprint is 23:c5:06:ad:58:f3:8d:e5:75:bd:94:6e:1e:a0:a3:a4. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'ec2-107-22-22-159.compute-1.amazonaws.com,107.22.22.159' (RSA) to the list of known hosts. sudo: unable to resolve host cookbook-test --2013-03-15 15:53:44-- http://apt.puppetlabs.com/puppetlabs-release-precise.deb Resolving apt.puppetlabs.com (apt.puppetlabs.com)... 96.126.116.126, 2600:3c00::f03c:91ff:fe93:711a Connecting to apt.puppetlabs.com (apt.puppetlabs.com)|96.126.116.126|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 3392 (3.3K) [application/x-debian-package] Saving to: `puppetlabs-release-precise.deb' 0K 100% 302M=0s 2013-03-15 15:53:44 (302 MB/s) - `puppetlabs-release-precise.deb' saved [3392/3392] Selecting previously unselected package puppetlabs-release. (Reading database ... 25370 files and directories currently installed.) Unpacking puppetlabs-release (from puppetlabs-release-precise.deb) ... Setting up puppetlabs-release (1.0-5) ... Processing triggers for initramfs-tools ... update-initramfs: Generating /boot/initrd.img-3.2.0-29-virtual Ign http://us-east-1.ec2.archive.ubuntu.com precise InRelease [ ... apt output redacted ... ] Setting up hiera (1.1.2-1puppetlabs1) ... Setting up puppet-common (3.2.2-1puppetlabs1) ... Setting up puppet (3.2.2-1puppetlabs1) ... * Starting puppet agent puppet not configured to start, please edit /etc/default/puppet to enable ...done. Processing triggers for libc-bin ... ldconfig deferred processing now taking place Cloning into 'puppet'... Warning: Permanently added 'github.com,207.97.227.239' (RSA) to the list of known hosts. Notice: /Stage[main]/Puppet/Cron[run-puppet]/ensure: created Notice: /Stage[main]/Puppet/File[/usr/local/bin/pull-updates]/ensure: defined content as '{md5}20cfc6cf2a40155d4055d475a109137d' Notice: /Stage[main]/Puppet/File[/usr/local/bin/papply]/ensure: defined content as '{md5}171896840d39664c00909eb8cf47a53c' Notice: /Stage[main]/Puppet/File[/home/ubuntu/.ssh/id_rsa]/ensure: defined content as '{md5}db19f750104d3bf4e2603136553c6f3e' Notice: Finished catalog run in 0.11 seconds
How it works...
Here's a line by line breakdown of what the Rake task does. In order to make the machine ready to run Puppet, we need to set its hostname to the name you've chosen:
sudo hostname #{hostname} sudo echo #{hostname} >/etc/hostname
Next, we download and install the Puppet Labs repo package, and install Puppet and Git:
wget http://apt.puppetlabs.com/puppetlabs-release-precise.deb sudo dpkg -i puppetlabs-release-precise.deb sudo apt-get update && sudo apt-get -y install git puppet
We need to disable the SSH StrictHostKeyChecking
option to avoid being prompted when the script clones the Git repo:
echo -e \"Host github.com\n\tStrictHostKeyChecking no\n\" >> ~/.ssh/config
We check out the repo:
git clone #{REPO} puppet
And finally, run Puppet:
sudo puppet apply --modulepath=/home/ubuntu/puppet/modules /home/ubuntu/puppet/manifests/site.pp
The new machine will now pull and apply Puppet changes automatically, without you ever having to log into it interactively. You can use this Rake task to bring lots of new servers under Puppet control quickly.