Skip to main content

Puppet: From Installation to Catalog Converge

Hands-On Lab

 

Photo of Elle Krout

Elle Krout

Content Team Lead in Content

Length

02:00:00

Difficulty

Intermediate

Jump in to the configuration management platform Puppet in this Learning Activity! Starting with installing Puppet, we aim to take you from newbie Puppet user to a competent module creator by installing and configuring Puppet Open Source, then by creating an Apache module where we'll learn how to use resource types, add static files, create templates, use OS-specific parameters, and store data in Hiera. By the end of this learning activity, you'll be ready to craft your own Puppet modules efficiently and with best practices.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Puppet: From Installation to Catalog Converge

Introduction

Starting with installing Puppet, we'll then install and configure Puppet Open Source. Next, we'll create an Apache module where we'll learn how to use resource types, add static files, create templates, use OS-specific parameters, and store data in Hiera.

Solution

Open two terminal sessions. Using the credentials provided on the lab page, sign in via SSH to the Ubuntu 18.04 Puppet Server in one and the CentOS 7 Puppet Agent Node in the other.

Install Puppet Server

Work from the Ubuntu 18.04 Puppet Server.

  1. Switch to root:

    sudo -i

  2. Pull in the Puppet repository:

    wget https://apt.puppetlabs.com/puppet6-release-bionic.deb

  3. Install the package:

    dpkg -i puppet6-release-bionic.deb

  4. Make sure we have access to the repository:

    apt update

  5. Install puppetserver:

    apt-get install puppetserver

    It will take a few minutes to complete installation.

  6. Configure Puppet Server to use puppet hostname in /etc/puppetlabs/puppet/puppet.conf. First, open the file:

    vim /etc/puppetlabs/puppet/puppet.conf

  7. Edit the file to include:

    [main] certname = puppet

    [master] certname = puppet

    Save and exit the file.

  8. Update /etc/default/puppetserver to use 512 MB of memory. First, open the file:

    vim /etc/default/puppetserver

  9. Edit the file to update it:

    JAVA_ARGS="-Xms512m -Xmx512m -Djruby.logger.class=com.puppetlabs.jruby_utils.jruby.Slf4jLogger"

    Save and exit the file.

  10. Set up the certificate authority:

    /opt/puppetlabs/bin/puppetserver ca setup

  11. Start the Puppet Server (it may take a few minutes):

    systemctl start puppetserver

  12. Enable the Puppet Server:

    systemctl enable puppetserver

  13. Log out and log back in to be able to use the puppet command on the CLI:

    exit sudo -i puppetserver ca list --all

Install the Puppet Agent

Work from the CentOS 7 Puppet Agent.

  1. Add the Puppet repository:

    sudo rpm -Uvh https://yum.puppetlabs.com/puppet6/puppet6-release-el-7.noarch.rpm

  2. Run a YUM update to make sure we can access the repository:

    sudo yum update

  3. Install the puppet-agent:

    sudo yum install puppet-agent

  4. Start the service:

    sudo systemctl start puppet

  5. Enable the service:

    sudo systemctl enable puppet

Accept Key Exchange

On the master:

  1. Accept the agent:

    puppetserver ca sign --certname web1.ec2.internal

Create the Apache Module

Work from the Ubuntu 18.04 Puppet Server.

  1. Move into the production environment:

    cd /etc/puppetlabs/code/environments/production

  2. Install the Puppet Dev Kit:

    apt-get install pdk

  3. Generate a new module:

    cd modules pdk new module apache

  4. This will take us through a series of questions. Give the following answers:

    1. If you have a Puppet Forge username, add it here.
      • clouduser
    2. Who wrote this module?
      • Enter your name.
    3. What license does this module code fall under?
      • Apache-2.0
    4. What operating systems does this module support?
      • RedHat based Linux
  5. Move into the newly created apache directory:

    cd apache

install.pp

  1. Create the install class for the new module:

    pdk new class install

  2. Write a class to install httpd on CentOS 7 using the package resource type:

    vim manifests/install.pp

    class apache::install ( $install_package = $apache::params::install_package, ) inherits apache::params { package { 'install_apache': name => $install_package, ensure => $apache::install_ensure, } }

    Save and exit.

  3. Create and update a params class for OS-specific data:

    pdk new class params vim manifests/params.pp

    class apache::params { case $::osfamily { 'RedHat': { $install_package = 'httpd' } } }

    Save and exit.

  4. In the module's data directory, open common.yaml and add the $apache::install_ensure Hiera data:

    vim data/common.yaml


    apache::install_ensure: 'present'

    Save and exit.

  5. Run the Puppet parser against the install manifest:

    puppet parser validate manifests/install.pp

service.pp

  1. Create a service class using the PDK:

    pdk new class service

  2. Create a service class that ensures the httpd service is started, enabled, and able to be restarted by other classes:

    vim manifests/service.pp

    class apache::service ( $service_name = $apache::params::service_name ) inherits apache::params { service { 'apache_service': name => $service_name, ensure => $apache::service_ensure, enable => $apache::service_enable, hasrestart => $apache::service_hasrestart, } }

    Save and exit.

  3. Update the params.pp file:

    vim manifests/params.pp

    class apache::params { case $::osfamily { 'RedHat': { $install_package = 'httpd' $service_name = 'httpd' } } }

    Save and exit.

  4. Update the common.yaml file:

    vim data/common.yaml


    apache::install_ensure: 'present' apache::service_ensure: 'running' apache::service_enable: true apache::service_hasrestart: true

    Save and exit.

  5. Validate the manifest:

    puppet parser validate manifests/service.pp

config.pp

  1. Download the Apache configuration file to the files directory:

    curl https://raw.githubusercontent.com/linuxacademy/content-puppetqs-nginx/learning-activity/httpd.conf -o files/httpd.conf

  2. Create a config class; ensure the Apache daemon is restarted whenever there is a change in this class:

    pdk new class config

    class apache::config ( $config_path = $apache::params::config_path, $config_source = $apache::params::config_source, ) inherits apache::params { file { 'apache_config': path => $config_path, source => $config_source, ensure => $apache::config_ensure, notify => Service['apache_service'], } }

  3. Update the params class:

    class apache::params { case $::osfamily { 'RedHat': { $install_package = 'httpd' $service_name = 'httpd' $config_path = '/etc/httpd/conf/httpd.conf' $config_source = 'puppet:///modules/apache/httpd.conf' } } }

  4. Update the common.yaml Hiera data:


    apache::install_ensure: 'present' apache::service_ensure: 'running' apache::service_enable: true apache::service_hasrestart: true apache::config_ensure: 'present'

  5. Validate the class:

    puppet parser validate manifests/config.pp

vhosts.pp

  1. Create the basic vhosts.conf.epp template in the templates directory:

    vim templates/vhosts.conf.epp
    
    Listen <%= $apache::vhosts_port %>
    <VirtualHost *:<%= $apache::vhosts_port %>>
       DocumentRoot "<%= $apache::vhosts_root %>"
       ServerName www.<%= $nginx::apache_servername %>
    </VirtualHost>
  2. This Hiera data will be node-specific, so create the nodes directory under the production environment Hiera data:

    mkdir ../../data/nodes

  3. Create the web1.ec2.internal.yaml file in this new directory and add the Hiera data for the virtual hosts file:


    apache::vhosts_port: '80' apache::vhosts_root: '/var/www' apache::vhosts_servername: 'the-puppet-project.com'

  4. Create a vhosts class to add this template to the agent:

    pdk new class vhosts

    class apache::vhosts ( $vhosts_dir = $apache::params::vhosts_dir ) inherits apache::params { file { "${vhosts_servername}.conf": content => epp('apache/vhosts.conf.epp'), ensure => $apache::vhosts_ensure, path => "${vhosts_dir}/${apache::vhosts_servername}.conf", }

     file { "$apache::vhosts_root":
       ensure => $apache::vhosts_root_ensure,
     }

    }

  5. Update the params file:

    class apache::params { case $::osfamily { 'RedHat': { $install_package = 'httpd' $service_name = 'httpd' $config_path = '/etc/httpd/conf/httpd.conf' $config_source = 'puppet:///modules/apache/httpd.conf' $vhosts_dir = '/etc/httpd/conf.d/' } } }

  6. Update the common.yaml Hiera data:


    apache::install_ensure: 'present' apache::service_ensure: 'running' apache::service_enable: true apache::service_hasrestart: true apache::config_ensure: 'present' apache::vhosts_ensure: 'present' apache::vhosts_root_ensure: 'directory'

  7. Run the Puppet parser:

    puppet parser validate manifests/vhosts.pp

init.pp

  1. Create the main apache class and ensure all classes in the module are included:

    pdk new class apache

    class apache ( $install_package = apache::params::install_package, $service_name = $apache::params::service_name, $config_path = $apache::params::config_path, $config_source = $apache::params::config_source, $vhosts_dir = $apache::params::vhosts_dir, String $install_ensure, String $service_ensure, Boolean $service_enable, Boolean $service_hasrestart, String $config_ensure, String $vhosts_port, String $vhosts_root, String $vhosts_servername, String $vhosts_ensure, String $vhosts_root_ensure, ) inherits apache::params { contain apache::install contain apache::config contain apache::service contain apache::vhosts

     Class['apache::install']
     -> Class['apache::config']
     ~> Class['apache::service']
     -> Class['apache::vhosts']

    }

  2. Add the apache class to our web1 server in the production environment's site.pp manifest:

    vim ../../manifests/site.pp

    node web1.ec2.internal { class { 'apache': } }

  3. Switch to the web1 server and force a catalog converge:

    puppet agent -t

  4. Ensure Apache is installed and running, and the virtual hosts file is present:

    systemctl status httpd cat /etc/httpd/conf.d/the-puppet-project.com.conf

Conclusion

Congratulations on completing this hands-on lab!