Joni Junni

Installing SSH with Puppet parameterized classes

Categories: [school]
Tags: [linux], [puppet], [debian], [linux-centralized-management-course], [ssh]

First, I created the default module structure:

mkdir -p /modules/sshporttest/manifests
mkdir -p /modules/sshporttest/templates

Then I created the init.pp file to manifests-directory:

nano /modules/sshporttest/manifests/init.pp

And wrote the following settings to it:

class sshporttest($port = 22) {
        package { 'openssh-server':
                ensure => "installed",
        }

         file { '/etc/ssh/sshd_config':
                content => template("sshporttest/sshd_config.erb"),
                require => Package["openssh-server"],
                notify => Service["ssh"],
        }

        service { 'ssh':
                ensure => 'running',
                enable => 'true',
                require => Package["openssh-server"],

                hasstatus => 'false',
                status => "/etc/init.d/ssh status|grep running",
        }
}

I added a parameter for port on the class line. This can be used on the class apply command, so that we can change the port to install SSH into.

I copied the default SSH config file to the templates directory:

cp /etc/ssh/sshd_config /home/joni/puppet/modules/sshporttest/templates/sshd_config.erb

And edited the header to test if my module would work:

ssh-test-edit

Finally, tested to run the module:

sudo puppet apply --modulepath modules/ -e 'class {"sshporttest":}'

Then I checked that the /etc/ssh/sshd_config file was modified:

ssh-test-edit-worked

I edited the /modules/sshporttest/templates/sshd_config.erb file so it would take the port number from the class parameter:

sshd-port-edit

Then I applied the module again with a parameter port => 2222 to set the SSH port to 2222:

sudo puppet apply --modulepath modules/ -e 'class {"sshporttest": port => 2222}'

Finally, I tested that the port has changed:

sshd-port-test-change

Sources