Thursday, 23 February 2017

Puppet Resources and Types

Resources in Puppet Configuration - In the last article on Puppet - "How to Install and Configure Puppet Master and Puppet Agent in Linux", we learned to create and configure Puppet master and Puppet agent node. In this article, we will be learning about the very basic component of Puppet configuration, known as Resources.

puppet-resources-and-types

What are Resources?

As mentioned, Resources are the fundamental components of a system, which have certain Attributes - state it can be in (present or absent, enabled or disabled, etc.) or an action associated with it (create, delete or modify). With these resources, we can have control on system parameters (like files, users, packages, services, etc.).

Depending on what system parameter a resource can manipulate and configuration it controls, each resource belongs to a particular Resource Type. The complete list of the resource types is provided here. In this article, we will see some of the basic resource types used in writing a Puppet code (also known as Manifest).

1. file

This resource type is used to manage files, directories and symbolic links (Read more about symbolic links). As I mentioned, every resource has a set of attributes associated with it. So lets check out come important attributes associated with a file resource.

  • path - It states the absolute path of the file/directory.
  • Example - '/usr/local/nagios/etc/nagios.cfg'
  • ensure - It states the existence of a file on the system i.e. it is present or absent. If it is present, whether it is a file, directory or a link. If it is a link, one should also mention about the target attribute, representing the source file it must point to.
  • content - This attribute represent what text the file will contain. In this case, you can mention the content within single quotes, like 'Hello world!' or you can use the source attribute, which expects a path to a file. So that, both these files will have same contents.
  • mode - This attributes represents the permissions on the file in octal notations, like 0755, 0644, etc.
  • owner and group - These attributes are associated with ownership of the file.
  • replace - Valid values for this attributes are true, false, yes and no. It states whether the file should be replaced if already present or otherwise.
  • source - As mentioned above, this attribute expects a path to a file, on the same system or the Puppet master. Thus, this file will have the same contents as that of the source file mentioned.

2. package

This resource type is used to manage packages on the Puppet agent nodes, including installation, updation and removal. Lets see some of the attributes associated with this resource type.

  • name - Name of the package.
  • Example - 'httpd'
  • ensure - It states the existence of a package on the system i.e. it should be present or absent.
  • provider - This attribute represent what backend/package manager, like yum or apt or zypper, it should use to install/remove the package.
  • source - This attribute is used to represent where to find a package, in case a package is not provided by the provider and available in a remote central repository.

3. service

This resource type is used to manage services on the Puppet agent nodes. It includes starting, stopping and restarting the service. Not only this, to add or remove the service from startup. Lets see some of the attributes associated with this resource type.

  • name - Name of the service.
  • Example - 'sshd'
  • ensure - It denotes the state of a service on the system i.e. it should be running or stopped.
  • enable - This attribute represents whether a service should start at system boot or not. So, the valid values are true and false.
  • path - With this attribute, we can specify the path where Puppet can find the init scripts.

4. user

This resource type is used to manage system users on the Puppet agent nodes. It includes addition, deletion and modification of the users. Lets see some of the attributes associated with this resource type.

  • name - Name of the user.
  • Example - 'nagios'
  • ensure - It denotes the state of a user on the system i.e. it should be present or absent.
  • comment - This attribute represents description of the user, like 'Nagios Admin User'.
  • expiry - With this attribute, we can specify the date, in YYYY-MM-DD format, when the user account expires.
  • uid/gid - UID and GID of the user.
  • shell - Shell of the user, like '/bin/bash', '/bin/ksh', '/sbin/nologin', etc.

5. exec

This resource type is used to execute system commands on the Puppet agent nodes, provided that, the command should be Idempotent (meaning that, it should always produce same result when executed multiple times). Lets see some of the attributes associated with this resource type.

  • command - Command name.
  • Example - '/bin/mkdir'
  • cwd - Name of the directory from which command should be executed. If directory does not exist, command execution will fail.
  • creates - File that should be looked for before executing the command. If file doesn't exist, command will be executed and file will be created.
  • onlyif - With this attribute, we can set dependency of the executing based on exit status of another command. So, if the other command executes successfully, this command will be executed, otherwise not.
  • path - The path that should be searched to find the command.
  • returns - Exit status of the command which should be returned, must be an integer between 0-255.
  • timeout - The maximum time the command should take for completing the execution.
  • unless - Opposite of onlyif. The command will be executed if exit status of other command is non-zero.

That's it for this article, which was supposed to introduce you with Puppet Resources. In the next article, we will learn about another new concept - Puppet Manifests. Till then, stay tuned!

0 comments:

Post a Comment