In this tutorial, we'll explain how to install and use Puppet Automating (IaC).
Puppet is a powerful tool for managing infrastructure as code (IaC). It allows you to automate the configuration and management of your systems, ensuring consistency and reducing manual intervention. In this tutorial, you'll learn how to use Puppet to manage your infrastructure by writing and applying Puppet manifests.
How to install and use Puppet Automating (IaC)
Prerequisites
Before you begin, ensure you have the following:
- A Ubuntu 24.04 installed dedicated server or KVM VPS.
- Basic Linux Command Line Knowledge: Familiarity with the terminal and command-line tools.
- User with sudo Privileges: The commands require root or sudo access.
Step 1: Installing Puppet
Update Your System
First, update your package index to ensure you have the latest package lists.
sudo apt-get update
Install Puppet
Puppet Labs provides a repository that makes it easy to install Puppet.
wget https://apt.puppetlabs.com/puppet6-release-focal.deb
sudo dpkg -i puppet6-release-focal.deb
sudo apt-get update
sudo apt-get install puppet-agent
This will install the Puppet agent, which is necessary for running Puppet commands.
Verify the Installation
After installation, verify that Puppet is installed by checking its version.
puppet --version
You should see the version of Puppet installed.
8.4.0
Step 2: Writing Your First Puppet Manifest
A Puppet manifest is a file that describes the desired state of your infrastructure. Manifests are written in Puppet’s declarative language.
Create a Directory for Manifests
It's a good practice to organize your Puppet manifests in a specific directory.
mkdir -p ~/puppet/manifests
cd ~/puppet/manifests
Write a Simple Manifest
Create a new file called init.pp with the following content:
node default {
file { '/tmp/hello.txt':
ensure => 'present',
content => 'Hello, Puppet!',
}
}
This manifest ensures that a file /tmp/hello.txt
is present and contains the text "Hello, Puppet!
".
Apply the Manifest
Use the Puppet agent to apply your manifest:
puppet apply init.pp
Puppet will read the manifest and enforce the desired state, creating the file if it doesn't exist.
Verify the Result
Check if the file has been created with the desired content:
cat /tmp/hello.txt
You should see the text "Hello, Puppet!
".
Step 3: Managing Packages with Puppet
Puppet can also manage packages, ensuring that the necessary software is installed on your servers.
Extend Your Manifest
Edit init.pp
to include a package resource:
nano init.pp
Add following code:
node default {
file { '/tmp/hello.txt':
ensure => 'present',
content => 'Hello, Puppet!',
}
package { 'cowsay':
ensure => 'installed',
}
}
Save and exit the file.
This manifest ensures that the cowsay
package is installed on your system.
Apply the Manifest
puppet apply init.pp
Puppet will install the cowsay package if it's not already installed.
Verify that cowsay is installed and works:
cowsay "Puppet is awesome!"
You should see a cow saying, "Puppet is awesome!"
< Puppet is awesome! >
--------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Step 4: Managing Services with Puppet
Puppet can also manage services, ensuring that they are running and enabled.
Add a Service Resource
Update init.pp
to manage a service:
nano init.pp
Update following code:
node default {
file { '/tmp/hello.txt':
ensure => 'present',
content => 'Hello, Puppet!',
}
package { 'cowsay':
ensure => 'installed',
}
service { 'ssh':
ensure => 'running',
enable => true,
}
}
This manifest ensures that the SSH service is running and enabled on boot.
Apply the manifest:
puppet apply init.pp
Puppet will start the SSH service if it's not already running and enable it to start on boot.
Verify the status of the SSH service:
sudo systemctl status ssh
The service should be active and running.
Step 5: Using Puppet Modules
Puppet modules are collections of manifests and other resources that can be reused across different projects.
Install a Module
You can install modules from the Puppet Forge. For example, to install the ntp
module:
sudo puppet module install puppetlabs-ntp
Use the Module in Your Manifest
Edit init.pp
to use the ntp module:
nano init.pp
Update the code:
node default {
class { 'ntp':
servers => ['0.pool.ntp.org', '1.pool.ntp.org'],
}
}
This manifest configures the NTP service to sync time with the specified servers.
Apply the manifest:
puppet apply init.pp
Puppet will configure the NTP service according to the module's settings.
Conclusion
In this tutorial, you learned how to install and use Puppet to manage your infrastructure as code. You installed Puppet, wrote and applied basic manifests, managed files, packages, and services, and used a Puppet module. Puppet's declarative approach makes it easy to automate the management of your systems, ensuring consistency and reducing the risk of configuration drift. As you become more familiar with Puppet, you can explore more advanced features, such as defining custom types and using Hiera for data separation.