Manifests and Resources in Puppet.

Priyanka Jadhav
4 min readMay 9, 2021

--

What Is Puppet?

Puppet is a Configuration Management tool that is used for deploying, configuring and managing servers. Puppet uses a Master Slave architecture. In which the Master and Slave communicate through a secure channel with the help of SSL.

It performs the following functions:

  • Defining configurations for every host, and constantly checking and confirming whether the required configuration is in place and is not changed on the host.
  • Dynamic scaling-up and scaling-down of machines.
  • Providing control over all your configured machines.

Resources

A Resource describes something about the state of the system. They are one of the fundamental units of puppet. Resources are used to design or build manifests or machines.

Puppet has multiple resources. The part of puppet code in manifest file is called a resource declaration. It is written in DML.

Format of the resource:

resource_type { 'name_of_the_resource':
argument => value,
other_arg => value,
}

For Example:-

file { 'test':
path => '/etc/xyz',
content => 'Hello',
}

Below is the command to list resource types :

puppet resource --typesaugeas
computer
cron
exec
file
filebucket
group
host
interface
k5login
macauthorization
mailalias
maillist
mcx
mount
nagios_command
nagios_contact
nagios_contactgroup
nagios_host
nagios_hostdependency
nagios_hostescalation
nagios_hostextinfo
nagios_hostgroup
nagios_service
nagios_servicedependency
nagios_serviceescalation
nagios_serviceextinfo
nagios_servicegroup
nagios_timeperiod
notify
package
resources
router
schedule
scheduled_task
selboolean
selmodule
service
ssh_authorized_key
sshkey
stage
tidy
user
vlan
whit
yumrepo
zfs
zone
zpool

There are three kinds of resource types:

  1. Puppet core or built-in resource types.
  2. Puppet defined resource types.
  3. Puppet custom resource types.

Puppet core or built-in resource types:

Built-in resource types are the puppet resource that are pre-built shipped with puppet software. All of the built-in resources are written and maintained by Puppet Inc.

Puppet defined resource types:

Defined resource types are lightweight resource types written in Puppet declarative language using a combination of existing resource types.

Puppet custom resource types:

Custom resource types are completely customized resource types written in Ruby.

Each resource is unique to a manifest, and can be referenced by the combination of its type and name, such as Service[“puppet”]. Finally, a resource also comprises a list of zero or more attributes. An attribute is a key-value pair, such as “enable => false”.

Attribute names cannot be chosen arbitrarily. Each resource type supports a specific set of attributes.

Puppet also allows you to read your existing system state by using the puppet resource command:

root@puppetmaster:~# puppet resource user root

Manifests in Puppet

Manifest is a directory containing puppet DSL files. These are written in Ruby Programming Language. and files have a .pp extension. The .pp extension is for puppet program.

All Puppet programs which are built for creating or managing any target host machine is called a manifest. Puppet is operate by manifests, the equivalent of scripts or programs. In any manifests, a user can have number of resources.

Puppet Manifests Components

  • Files: Files are the text files that can be deployed on your puppet clients.
  • Resources: Resources are the elements that we need to assess or change.
  • Templates: are used to create configuration files which we can reuse later.
  • Nodes: Block of code where all the information related to the client is defined.
  • Classes: Classes are used to combine various types of resources.

Example 1: Creating a new file (in site.pp)

Step 1: Login to Puppet Server as a root user.

Step 2: Go to /etc/puppet/manifests

Step 3: Edit site.pp — main manifest file. If you don’t find one, create it.

# nano site.pp

Step 4: Let us write Puppet code to create a file in /etc/ at Puppet agent node. The code goes as below,

file {'/etc/motd':
ensure => "present",
owner => "root",
content => "This is my first manifest to create a file in etc directory",
}

In the above code, we have used file resource type, with arguments such as ensure, owner and content. The file will be created in /etc.

Step 5: The manifest is ready. now from puppet agent run,
puppet agent -t

Working with variables in manifest

The user can define a new variable or use an existing variable at any point in a manifest.

Puppet supports different kind of variables but few of them are frequently used such as strings and array of string.

 $package = 'http'
package{ $package:
ensure => 'ínstalled'
}

Using Loops in Manifest

Loops are used when you want to use the same piece of code multiple times on a till the condition is not satisfied. They are also used to do repetitive tasks with different set of values.

$packages = [ 'httpd', 'mysql', 'puppet' ]package { $packages:
ensure => installed,
}

The loops are mostly used with arrays to do the repeat task with different values.

Working with conditional statements in manifest

Conditional statements let your code behave differently in various situations. Puppet supports if and else statements, case statements, and selectors.

Here is an example of conditional statement,

if $facts[‘is_virtual’] { 
warning(‘Tried to include class ntp on virtual machine; this node might be misclassified.’)
} elsif $facts[‘os’][‘family’] == ‘Darwin’ {
warning(‘This NTP module does not yet work on our Mac laptops.’)
} else {
include ntp
}

Conclusion

In this article we saw what is puppet, and its resources . We also saw resource types used in puppet We also learnt how to write a manifest in puppet and how to check that on the agent machine. We saw how to use variables, loops and conditionals statements in a manifest.

--

--

No responses yet