How to Setting Up a Puppet Environment

The Cloud  is one of the biggest paradigm shifts in the IT world. Instead of provisioning physical hardware in a physical data center and then managing applications running on the physical hardware, virtualization has allowed IT organizations to decouple logical infrastructure from physical infrastructure, and thereby deliver new-found flexibility to provide and manage value-add services. the need for good configuration management practices does not end when services (or parts of services) are moved to the cloud. One from the most important tools of configuration management is Puppet.

In computing, Puppet is an open source configuration management utility. It runs on many Unix-like systems, and includes its own declarative language to describe system configuration. It is produced by Puppet Labs, founded by Luke Kanies in 2005. It is written in Ruby and released as free software under the GNU General Public License (GPL) until version 2.7.0 and the Apache License 2.0 later.

Masterless mode is a puppet cluster which has no shared master, in a sense, each node is its own master. It is responsible for fetching, storing and applying manifests itself. In this article we will show you how you can set up a masterless puppet environment on Ubuntu 14.04.

We assume that you are a user of Puppet and Git. As prerequisites, you need to have an Ubuntu 14.04 droplet with a sudo non root user and SSH keys already added and also another Ubuntu 14.04 droplet where you have already added SSH keys and Git Labs also installed.

install puppet in ubuntu

We will start by creating a repository where we will store all our Puppet modules and manifests. So Open the Git Labs UI within this address: http://GIT_server_IP where GIT_server_IP is your Git server address IP. Then create a new account under the New user button and then press sign up. So after activating your account within the link of activation received on your email account you are able to sign in and start using your new account. Later press the “+New project”, concerning the Project path add Puppet then click create project and add again Puppet to the project path. For the visibility level choose Public and create again Create project button. 

Your SSH URL will look like the following one:  [email protected]_server_IP:username/puppet.git.

Next step will be the creation of a SSH key on the Puppet server and add it to Git Labs. So, log in the Puppet server as root, and create a SSH key using the following command:

# ssh-keygen -t rsa

Then we will display our public key using the following command.

# cat ~/.ssh/id_rsa.pub

Copy this public key and on your Git Labs, click on the profile settings, then click on the SSH keys => Add a SSH key => Add a description of the key in the Title field and finally paste the copied public key into the key field and click Add key to save changes.

Now, we will install Puppet and Git. We will start by downloading the Puppet package by using the following command:

# wget http://apt.puppetlabs.com/puppetlabs-release-trusty.deb
dpkg -i /tmp/puppetlabs-release-trusty.deb

And to update the system’s package list, use the following command:

# apt-get update

And to install Puppet and Git type the following command:

# apt-get install puppet git-core

Now after finishing the installation, we will push our Puppet repository so we will move to the /etc/puppet directory using the following command:

# cd /etc/puppet

And type the following command to initialize the Git repository and to add everything to this directory:

# git init
# git add .

And to commit the modifications we made we will use the following command:

# git commit -m "Commit the Puppet files"

The following command is used to add the Git project:

# git remote add origin [email protected]_Address_IP:username/puppet.git

And to push those modifications use the following command:

# git push -u origin master
Now we will log out as root and log in as sudo non root user. We will start by cleaning up the /etc/puppet/puppet.conf file. We will open this file using the following command:
# sudo nano /etc/puppet/puppet.conf

You will get something like this:


[main]
logdir=/var/log/puppet
vardir=/var/lib/puppet
ssldir=/var/lib/puppet/ssl
rundir=/var/run/puppet
factpath=$vardir/lib/facter
templatedir=$confdir/templates

[master]
ssl_client_header = SSL_CLIENT_S_DN 
ssl_client_verify_header = SSL_CLIENT_VERIFY

Start by removing everything from the Master line and the last line too and we will make some changes to get at the end something like the following file:


[main]
logdir=/var/log/puppet
vardir=/var/lib/puppet
ssldir=/var/lib/puppet/ssl
rundir=/var/run/puppet
factpath=$confdir/facter
 Now we will do the last step which is the creation of a useful module for Puppet where it can run. So we will start by moving into the Puppet modules directory using the following command:
# cd /etc/puppet/modules

Then we will create a cron-puppet directory containing manifests and files directories using the following command:

# sudo mkdir -p cron-puppet/manifests cron-puppet/files

Then we will create and open a new file called Pup_init in the manifests directory using the following command:

# sudo nano cron-puppet/manifests/Pup_init

Copy the following code into Pup_init:


class cron-puppet {
    file { 'post-hook':
        ensure  => file,
        path    => '/etc/puppet/.git/hooks/Merge_p',
        source  => 'puppet:///modules/cron-puppet/Merge_p',
        mode    => 0755,
        owner   => root,
        group   => root,
    }
    cron { 'puppet-apply':
        ensure  => present,
        command => "cd /etc/puppet ; /usr/bin/git pull",
        user    => root,
        minute  => '*/30',
        require => File['post-hook'],
    }
}

Then use the following command to open another file called Merge_p in the files directory after saving and closing the Pup_init.

# sudo nano cron-puppet/files/Merge_p

Copy the following code to this file:


#!/bin/bash -e
/usr/bin/puppet apply /etc/puppet/manifests/Site.p

if [ $? -eq 0 ]
then
    /usr/bin/logger -i "Puppet is running successfully" -t "puppet-running"
    exit 0
else
    /usr/bin/logger -i "Puppet is running with an error, try to run it manually" -t "puppet-running"
    exit 1
fi

Then save and close the file.

Then we will use the following command to run this module on Puppet and create a global manifest which is found at /etc/puppet/manifests/Site.p.

# sudo nano /etc/puppet/manifests/Site.p

Copy and paste the following code in the created manifest Site.p which will create a node classification called “default”


node default {
    include cron-puppet
}

Save and close the file and use the following command to run our module:

# sudo puppet apply /etc/puppet/manifests/Site.p

You will receive something like this if everything is working good:

...

Notice: Finished catalog run in 0.18 seconds

And to finish our setting up we will commit our changes to the Git repository, so we will log in as root user and move to the /etc/puppet directory.

# cd /etc/puppet

Use the following command to add everything to this directory:

# git add .

And type the following command to commit the changes:

# git commit -m "Adding cron-puppet module"

And to push those changes use the following command:

# git push -u origin master

And to automate this installation by using a user data you need to have a SSH key when you create the droplet and to add this key to the your Git Labs server. Then on the Enable user data add the following commands and don’t forget to replace the details about (GIT_server_IP and the username) by yours.

#!/bin/bash -e

wget -O /tmp/puppetlabs.deb http://apt.puppetlabs.com/puppetlabs-release-`lsb_release -cs`.deb
dpkg -i /tmp/puppetlabs.deb
apt-get update
apt-get -y install git-core puppet
cd /etc
mv puppet/ puppet-bak
git clone http://GIT_server_IP/username/puppet.git /etc/puppet

puppet apply /etc/puppet/manifests/Site.p

Conclusion

After this article and its description you will have a masterless Puppet system, and you can easily now accelerate various servers easily and remotely.