I am writing this post as part of course Linuxin keskitetty hallinta held by Tero Karvinen. In this article I will install Varnish reverse proxy and change its port by Puppet parametrized class. Double weeks ago I did article for how you install Apache with Puppet: http://nikokiuru.com/2013/11/puppet-package-file-service-example/.

Create new module

Remember first start by hello world! http://nikokiuru.com/2013/11/hello-puppet/

$ mkdir -p modules/varnishd/manifests/
$ nano modules/varnishd/manifests/init.pp

Code in init.pp:

class varnishd ($varnish_port = 80, $backend_port = 8080) {

  package {"varnish":
    ensure => "installed",
  }

  service {"varnish":
    ensure => "running",
    enable => "true",
    require => Package["varnish"],
  }

}

Run module:

$ puppet apply --modulepath modules/ -e 'class {"varnishd":}'

Create templates

First copy and modify varnish default config-file:

$ mkdir modules/varnishd/templates/
$ cp /etc/default/varnish modules/varnishd/templates/varnish.erb
$ nano modules/varnishd/templates/varnish.erb

Change port 6081 to variable @varnish_port:

DAEMON_OPTS="-a :<%= @varnish_port %> \

And next copy and modify varnish backend service config-file:

$ cp /etc/varnish/default.vcl modules/varnishd/templates/default.vcl.erb
$ nano modules/varnishd/templates/default.vcl.erb

Change backend port to variable @backend_port:

.port = "<%= @backend_port %>";

And last add two file method in init.pp

$ nano modules/varnishd/manifests/init.pp

And code in init.pp:

class varnishd ($varnish_port = 80, $backend_port = 8080) {
  package {"varnish":
    ensure => "installed",
  }

  service {"varnish":
    ensure => "running",
    enable => "true",
    require => Package["varnish"],
  }

  file { "/etc/default/varnish":
    content => template("varnishd/varnish.erb"),
  }

  file { "/etc/varnish/default.vcl":
    content => template("varnishd/default.vcl.erb"),
  }

}

Finally run command:

$ puppet apply --modulepath modules/ -e 'class {"varnishd":}'

Test

$ curl -I localhost

Result:

HTTP/1.1 200 OK
Server: Apache/2.2.22 (Ubuntu)
Last-Modified: Thu, 21 Nov 2013 19:08:06 GMT
ETag: "c40836-d-4ebb49e1a8e3c"
Vary: Accept-Encoding
Content-Type: text/html
Transfer-Encoding: chunked
Date: Thu, 21 Nov 2013 19:29:59 GMT
X-Varnish: 1215889787
Age: 0
Via: 1.1 varnish
Connection: keep-alive