Thursday, November 13, 2014

Puppet - How to create a file and change its contents using templates

Don't know what puppet is?
Go through my previous blog article: Introduction to Puppet

If you come across difficulties in establishing connections between puppet master and agent, go through this blog article: Puppet master-agent communication errors
It may help to solve your common communication problems.

In this quick tutorial you can create a file in puppet agent using a predefined template and modify its contents.

Before starting this tutorial you need to have two instances of puppet master and puppet agent separately installed and configured (say 1 GB RAM, ubuntu VM instances).
Say puppet agent's hostname is 'appserver-wkr'.

You can follow these 3 simple quick steps.
1. Create a template file
2. Configure node definition in site.pp or class file for the agent node
3. Perform a catalog run in puppet agent

Step 1: Creating the template file.
In puppet master create the template file as follows.
Location: /etc/puppet/modules/appserver/templates/
Filename: welcome-template-file.erb
<% if @say_hello_to -%>
Hello <%= @say_hello_to %>,
<% end -%>

I'm <%= @myname %>, on a <%= @operatingsystem %> system, nice to meet you.

Step 2: Configuring site.pp file
node 'appserver-wkr' {

        $say_hello_to = 'guys and gals'
        $myname = 'welcome file'

        file {  "/tmp/$myname":
                ensure  => file,
                content => template('appserver/welcome-template-file.erb'),

Note that the path is given ignoring 'templates' folder as appserver/<template-name.erb>

Step 3: Perform a catalog run in puppet agent
root@appserver-wkr:/opt# puppet agent --test
dnsdomainname: Name or service not known
dnsdomainname: Name or service not known
info: Caching catalog for appserver-wkr.openstacklocal
info: Applying configuration version '1415862309'
notice: /Stage[main]//Node[appserver-wkr]/File[/tmp/welcome file]/ensure: defined content as '{md5}3210b176441c528b9291ef4a61511f72'
notice: Finished catalog run in 0.16 seconds

Verify content.

root@appserver-wkr:/opt# ls /tmp/
file03  hsperfdata_root  welcome file
root@appserver-wkr:/opt# vi /tmp/welcome\ file

Hello guys and gals,

I'm welcome file, on a Ubuntu system, nice to meet you.

Wondering how value came for @operatingsystem?
Apart from custom variables defined, puppet can use variables predefined by Factor.

What is the significance of this content modifications?

Well, when you are deploying instances in large scale (Physical computer server clusters, VM clusters) these templates could come in handy where you can modify xml configuration files contents specific to each node.

As for an example say you have a WSO2 Application Server cluster with Worker/Manager Clustering pattern.
You don't have to keep separate installation product packs for each manager and worker. Can keep only one pack and modify the configuration xml files according to the node role (manager or worker).

Define templates and save them in your product pack's templates folder in puppet master.
Say, /etc/puppet/modules/appserver/templates/
Sit back and relax, let the puppet do the work...

Slight modification to this code could come in handy, to solve your daily deployment needs... :)

How about modifying an existing line in your /etc/hosts file or add a line to it if the particular line is already not there?

host { '':
    ensure => 'present',
    target => '/etc/hosts',
    ip => '',

No comments:

Post a Comment