Basic "Hello World" Tutorial for Puppet using Vagrant

setup Puppet in your local machine with this easy steps

Posted by Darwin Biler on March 29, 2015

Like any new technology, the best way to quickly show and explain it is by giving a very basic tutorial like the classical "Hello World" example. And today, you are going to see one for Puppet. In case you haven't heard it yet, its a tool to automate the management of your entire IT infrastructure. There are tons of articles about it in the Internet, but beware -- most of those are targeted towards System Administrators and alike.

If you are a web developer like me, chances are you'll end up confused reading many of those. That is why I written this article to provide a very basic tutorial in web developers perspective. So lets get started!

Things you'll need:

  • VirtualBox
  • Vagrant

First thing you need to do is create a folder where you want to place this tutorial. Lets say the folder name is "hellopuppet"

mkdir hellopuppet

go inside the folder and initialize a vagrant file by specifiying an Ubuntu basebox.

cd hellopuppet
vagrant init hashicorp/precise32

What basically happens here is, vagrant init creates an "Vagrantfile" inside the hellopuppet folder. This file tells vagrant what kind of box we want to build. For this case, we specified we want it to be an Ubuntu box by Hashicorp. The good news is, this box already comes with puppet installed, so you wont need to install Puppet yourself and we can just focus in trying it out.

Next is, booting up the box

vagrant up

If the precise32 box was not present yet in your computer, this command will download it (that could take a long time so grab a coffee first ). Once the box was fully loaded, you are ready to write your first Puppet codes!

At this point, it should be like this:

Screen Shot 2015-03-29 at 9.33.24 PM

There might be more output messages than the one displayed above. But there should not be red messages. IF there are, check the vagrant documentation as it is outside the scope of this tutorial.

Now login to the Virtual Machine you just had created by executing

vagrant ssh

You should be now inside the VM. Now there is where we will try out Puppet.

Inside the VM, create a file called "hellopuppet.pp"

nano hellopuppet.pp

and put the following code:

file {'/vagrant/helloworld.txt':
  ensure  => present,
  content => "Hello World!",
}

Save the file and exit by pressing Ctrl+O then Ctrl+X

What you just had did is you created a Puppet manifest, which for us PHP Developers are called PHP source codes.
The filename extension it is using is .pp like how we set .php for our PHP files.

The fragment of code you just had pasted is called a "Resource declaration" . There, we are declaring a file with the filename of "helloworld.txt" should be present (via ensure=> present parameter) in the /vagrant directory, and its content should be the text "Hello World!"

Unlike ordinary scripts that we are used to, Puppet language is declarative, it describes how things should be, rather than what are the things to be done.

Lets try running the script!

You can run the script by

puppet apply hellopuppet.pp

It should output something like this:
Screen Shot 2015-03-29 at 10.05.34 PM

What the script did is it checked the /vagrant folder if there is a helloworld.txt file present. It figured out there is none so it had created one and put the contents "Hello World!".

Uhmm, what is special in here.. I can do that as well in PHP..

Well, though our sample is just for writing a simple hello world file, Puppet is for some bigger purpose, like:

  • It can check your server periodically if it matches the declaration, if its not, it will automatically "fix things"
  • Puppet is not limited in creating simple files, it can install entire software stack like Nginx, PHP, MySQL etc.
  • The same code can run in multiple platforms. For example, you want to ensure MySQL is installed in both Ubuntu and CentOS server, in normal circumstances, you would have to manually login to those two box and execute the commands meant for each OS, with Puppet, you'll just declare once the .pp file, and it can run in any machine

There is a lot of things to cover about Puppet. From here, I suggest you continue your learning by reading more in here


Did you find this useful?

I'm always happy to help! You can show your support and appreciation by Buying me a coffee (I love coffee!).