Skip to main content

Getting Started with @PuppetLabs - Part 4 - Supporting multiple operating systems

Most puppet modules can be designed to support multiple operating systems. Puppet has built in features to allow this through some simple syntax in the .pp file. It is a good practice to do that, whether you are publishing these modules on Puppet Forge or building highly customized modules for a specific purposes in a private enterprise.

1. Scalability - In case an organization chooses to switch operating systems or support other operating systems, your modules are already supporting them or they already have some operating systems and it is a matter of adding a few lines of code at the very top to add support for other operating systems.

2. Visibility and usability - The modules that support more than one operating system have higher usage counts on Puppet Forge because they support more than one type of need.

We will walk through a simple example to demonstrate multi-OS support. I checked in this code in GitHub at https://github.com/adityai/puppetApache.

Module: Install Apache web server and start it as a service.
OS Support: CentOs, Ubuntu and RHEL

Step 1: Folder structure
Create the following folder structure and blank files.

Folder: puppetApache
|-> Folder: apache -> Folder: manifests -> File: init.pp
|->  runner.pp

Step 2: Create runner.pp
We can use runner.pp to make it easy for us to apply the module from command line. Open the puppetApache/runner.pp file and enter the following line of code.

include apache

Step 3: Create the apache class
Open puppetApache/manifests/init.pp and enter the following code. I will explain each line of code in comments here.

#Create a class named apache
class apache {
#Case: when operating system is: case $::operatingsystem { #When operating system is ubuntu, set the package name and service name to apache2
"ubuntu": {
$packagename = "apache2"
$servicename = "apache2"
} #When operating system is centos or rhel, set the package name and service name to httpd
"centos", "rhel": {
$packagename = "httpd"
$sercicename = "httpd"
} #Default condition when the operating system is not supported, fail and stop.
default: {
fail("Ubsupported OS: ${::operatingsystem}")
}
}
#Ensure Apache is installed #Refer to puppet type reference https://docs.puppetlabs.com/references/latest/type.html#package
package { 'apache':
name => $packagename,
ensure => installed,
}
#Ensure Apache is running #Refer to puppet type reference https://docs.puppetlabs.com/references/latest/type.html#service
service { 'apache':
name => $servicename,
ensure => running, #require acts as a dependency. In this case, this service section requires the apache package section to be completed before this service section can run. #This ensures that dependencies are met before a particular section can execute.
require => Package['apache'],
}
}

Step #4: Run the module
Execute the following command from the puppetApache folder in the command line.

sudo puppet apply --modulepath . runner.pp

Step #5: Verify that apache is installed and running as a service
Execute the following command on the command line. You may have to run it with sudo depending on the user permissions.

For CentOs/RHEL:
service httpd status

For Ubuntu:
service apache2 status

Step #6: A little fun.
Now that we have confirmed that the apache web server is installed and running, let us stop the service and re-apply the puppet module.

Stop the Apache service
service apache2 stop #will stop the service on Ubuntu

Check the status of the service
service apache2 status #will show that the service is stopped

Apply the puppet module again.
sudo puppet apply --modulepath . runner.pp

Check the status of the service
service apache2 status #will show that the service is running

Here's what happened: When the puppet client applied the module:
1. It observed that apache is already installed (ensure => installed) and it does nothing with apache package.
2. It observed that the apache service is stopped (ensure => running) and it started the service.

Helpful Tip: If you really want your module to support multiple operating systems, I recommend setting up the case $::operatingsystem part as soon as you create the init.pp. As you add more sections, update the case $::operatingsystem part in parallel.

Have fun!



Popular posts from this blog

A @trello board to get kids excited

My 8 year old just started his summer break. He did so well in school and I am proud of him. He skipped second grade, got into the gold honor roll in every quarter and got a medal for doing that. Last night, I promised to install a new app for him on his iPad mini. I installed Trello and created a board for him while he watched. I showed him how to create cards, add labels to them and move them from To Do, to Doing to Done. I had him create some cards and label them. He could not stop creating cards. I could not convince him to go to bed after that. He created cards for everything he wants to do in the summer and he is not done with creating cards. He even created a card to email a screenshot of his Trello board to his teacher.

Later last night, he was still awake in bed when I checked on him. He told me that he wanted to add three more labels - Math, Science and One-on-One. He wanted a label titled 'One-on-one' for tasks that he wants to do with me and he wants one-on-one att…

#Git starter cheat sheet

Initialize a local repo Run the following command in the folder where you would like to initialize a git repo.
git init
Get status It is a good practice to frequently run check on status during development. The following command points out the changes between the previous commit and current state of the folder.
git status
Add content To add an untracked file named ‘text.txt’ to the staging area, execute the following command.
git add text.txt
Syntax: git add <filename>
Commit changes To commit changes made to the folder, execute the following command. The message will be used as a commit message to associate this check-in with the message.
git commit –m “Add text.txt to the code base.”
Syntax: git commit –m “<Commit message>”
Add using wild card To add multiple files using a wild card character, execute the following command.
git add ‘*.txt’
Syntax: git add ‘<wildcard_character+string>’
Check history Review commit history using the following command.
git log
Add Remote re…

A @trello board to get kids excited - continued

This is a continuation of the previous post titled - A trello board to get kids excited. At the time of writing this post, the previous post had 198 page views. I wish people commented. I did get some positive feedback on Twitter. The Trello twitter account re-tweeted my tweet and also sent out a second tweet advertising the page. Thank you very much. I hope a lot of parents and kids benefited and had fun as a result.

Trello people: Idea - How about a 'Trello Kiddo'? Perhaps you could offer that to schools that give iPads for each kid to take home with them. Get them when they are young.

When a kid does something religiously, regularly and feels great about it and can't wait to tell everyone about it, you know you've done well as a parent. We realized that we needed a separate column to keep track of 'Special accomplishments'. Positive reinforcement that you can see with your eyes!

Some parents feel like they haven't done enough for their kids, particularly…