Book Image

Puppet 3 Cookbook - Second Edition

By : John Arundel
Book Image

Puppet 3 Cookbook - Second Edition

By: John Arundel

Overview of this book

A revolution is happening in web operations. Configuration management tools can build servers in seconds, and automate your entire network. Tools like Puppet are essential to taking full advantage of the power of cloud computing, and building reliable, scalable, secure, high-performance systems. More and more systems administration and IT jobs require some knowledge of configuration management, and specifically Puppet."Puppet 3 Cookbook" takes you beyond the basics to explore the full power of Puppet, showing you in detail how to tackle a variety of real-world problems and applications. At every step it shows you exactly what commands you need to type, and includes full code samples for every recipe.The book takes the reader from a basic knowledge of Puppet to a complete and expert understanding of Puppet's latest and most advanced features, community best practices, writing great manifests, scaling and performance, and extending Puppet by adding your own providers and resources. It starts with guidance on how to set up and expand your Puppet infrastructure, then progresses through detailed information on the language and features, external tools, reporting, monitoring, and troubleshooting, and concludes with many specific recipes for managing popular applications.The book includes real examples from production systems and techniques that are in use in some of the world's largest Puppet installations, including a distributed Puppet architecture based on the Git version control system. You'll be introduced to powerful tools that work with Puppet such as Hiera. The book also explains managing Ruby applications and MySQL databases, building web servers, load balancers, high-availability systems with Heartbeat, and many other state-of-the-art techniques
Table of Contents (16 chapters)
Puppet 3 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. Add the following line to the top of your Rakefile:

    REPO = '[email protected]:bitfield/cookbook.git'
  2. 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:

  1. 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 it cookbook-test, you could use

    node 'cookbook-test' {
      include puppet
    }
  2. 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 of HOSTNAME). The command should all be on one line:

    $ rake CLIENT=ec2-107-22-22-159.compute-1.amazonaws.com HOSTNAME=cookbook-test bootstrap
    
  3. 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.