Linuxin keskitetty hallinta – Homework 2 – Modules, templates, facter

Course homepage:

http://terokarvinen.com/2012/aikataulu-linuxin-keskitetty-hallinta-ict4tn011-3

This is piece of homework is pretty shoddy as I was taken by surprise by the earlier-than-usual deadline.

Assignment

Study:

http://docs.puppetlabs.com/learning/modules1.html

http://docs.puppetlabs.com/learning/templates.html

http://www.puppetcookbook.com/posts/list-facter-facts.html

and create example modules about each article.

Modules

Basic

Let’s create the necessary directory structure:

master@palvelin:~$ pwd
/home/master
master@palvelin:~$ mkdir -p puppet/modules/filewriter3/manifests

Next up, init.pp:

master@palvelin:~$ nano puppet/modules/filewriter3/manifests/init.pp

It looks like this:

class filewriter3 {
    file { '/home/master/writtenfile3':
        content => "This is just a file created by a module. ",
    }
}

Let’s make a shell script for testing the module:

master@palvelin:~$ nano filewriter3.sh

Which looks like this:

#!/bin/bash
sudo puppet apply --modulepath puppet/modules/ -e 'class {"filewriter3":}'

Time to run it and see if it works:

master@palvelin:~$ ./filewriter3.sh 
warning: Could not retrieve fact fqdn
notice: /Stage[main]/Filewriter3/File[/home/master/writtenfile3]/content: content changed '{md5}0cd8778e05e7ee1012eb94f8d193d0fd' to '{md5}42b89c3ab2551460fdc215029c97b22e'
notice: Finished catalog run in 0.13 seconds
master@palvelin:~$

The catalog run finished. Let’s take a look at the created file:

master@palvelin:~$ cat writtenfile3 
This is just a file created by a module. master@palvelin:~$

Hooray.

Facter

Directory for the module:

master@palvelin:~$ pwd
/home/master
master@palvelin:~$ mkdir -p puppet/modules/filewriter/manifests

Init.pp:

master@palvelin:~$ nano puppet/modules/filewriter/manifests/init.pp
class filewriter {
    file { '/home/master/writtenfile':
        content => "This machine is called ${hostname}. ",
    }
}

Script for running the module:

master@palvelin:~$ nano filewriter.sh

It looks like this:

#!/bin/bash
sudo puppet apply --modulepath puppet/modules/ -e 'class {"filewriter":}'

Let’s test it:

master@palvelin:~$ ./filewriter.sh 
warning: Could not retrieve fact fqdn
notice: /Stage[main]/Filewriter/File[/home/master/writtenfile]/content: content changed '{md5}e50c39d47ada5c42819e6b2b329f4cc5' to '{md5}d3ed13434749c34d0533cbb107f033c2'
notice: Finished catalog run in 0.13 seconds

Does it work?

master@palvelin:~$ cat writtenfile
This machine is called palvelin. master@palvelin:~$

Yes.

Template

Directories for the module. One for the manifest and one for the template:

master@palvelin:~$ pwd
/home/master
master@palvelin:~$ mkdir -p puppet/modules/filewriter2/manifests
master@palvelin:~$ mkdir -p puppet/modules/filewriter2/templates

Templates are .erb files and their syntax is different from that of puppet’s:

master@palvelin:~$ nano puppet/modules/filewriter2/templates/mallisisalto.erb

whose contents are:

This is too much content to write in the manifest, surely.
<%= @muuttuvateksti %>

Now for the manifest:

master@palvelin:~$ nano puppet/modules/filewriter2/manifests/init.pp

whose contents are:

class filewriter2 {
    $muuttuvateksti = "This is from a variable! "
        file { '/home/master/writtenfile2':
           content => template("filewriter2/mallisisalto.erb")
        }
    }

How about a script for trying it out:

master@palvelin:~$ nano filewriter2.sh

It looks like this:

#!/bin/bash
sudo puppet apply --modulepath puppet/modules/ -e 'class {"filewriter2":}'

Testing it out:

master@palvelin:~$ ./filewriter2.sh 
warning: Could not retrieve fact fqdn
notice: Finished catalog run in 0.11 seconds
master@palvelin:~$ cat writtenfile2
This is too much content to write in the manifest, surely.
This is from a variable! 
master@palvelin:~$
Advertisements

About a1100320

IT student, musician, gamer. Beep boop.
This entry was posted in Linuxin keskitetty hallinta ICT4TN011-3. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s