Thursday, June 18, 2015

Variables May Vary

Now that we have talked about puppet some, let's add some variables to the mix.
The simple idea is that they are passed from the puppet/manifest/site.pp to the module/apache/templates/velcrohurts.conf.erb via magic!

I agree, it is magic, but the magic tells you how to do it.
Lets start with breaking down our previous simple config.



# Managed by Puppet 
# All changes will be overwriten 

<VirtualHost *:80>
  ServerName internal.velcrohurts.net 
  DocumentRoot  /var/www/html/
  LogLevel info 
  LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" combined
  ErrorLog ${APACHE_LOG_DIR}/velcrohurts-error.log
  CustomLog ${APACHE_LOG_DIR}/velcrohurts-access.log combined
  RedirectMatch ^/$ http://velcrohurts.net/
  <Directory /var/www/html/> 
     AllowOverride all
     Order allow,deny 
     allow from all
  </Directory>
</virtualHost> 

The virtual host config has the ServerName, what if we want to use this config for other servers.
We can edit it, or we can set it as a variable. Let's do the second one for this example.

First we need to copy the config to the modules/apache/templates/velcrohurts.conf.erb

Then we want to edit it to add the variable names.



# Managed by Puppet 
# All changes will be overwriten 

<VirtualHost *:80>
  ServerName <%= server_name %>  
  DocumentRoot  /var/www/html/
  LogLevel info 
  LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" combined
  ErrorLog ${APACHE_LOG_DIR}/<%= server_name %>-error.log
CustomLog ${APACHE_LOG_DIR}/<%= server_name %>-access.log combined
  RedirectMatch ^/$ http://velcrohurts.net/
  <Directory /var/www/html/> 
     AllowOverride all
     Order allow,deny 
     allow from all
  </Directory>
</virtualHost> 

So now that we have the .erb we need to edit our modules/apache/manifests/init.pp and add the variables there, and make a few changes.


class apache ($server_name){ 
# Install package 
    package {'apache2':
        ensure  => installed,
    }

# set the configure for debian 
  file { '/etc/apache2/sites-available/velcrohurts.conf':
    ensure => file,
    mode   => '0644',
    owner  => 'root',
    group  => 'root',
    content => template["apache/velcrohurts.conf.erb"],
    require => Package['apache2'],
    notify => Exec ["reload-apache2"],
  }

# Reload apache with the config 
# And remove the default-000 config 
    exec {'/usr/sbin/a2dissite 000-default':
    require => Package['apache2'],
    notify => Exec ["reload-apache2"],
    }

# Now add your config 
    exec {'/usr/sbin/a2ensite ajplus':
      notify => Exec ["reload-apache2"],
      require => Package['apache2'],
    }

# the apache reloader 
    exec { 'reload-apache2':
      command => '/etc/init.d/apache2 reload',
      refreshonly => true,
  }
}


The final step here is to add the magic, set the manifests/site.pp settings


node default { 
   include accounts 
} 

# web servers 
node web1 { 
   class apache {
       server_name = internal.velcrohurts.com
} 
node web2 { 
   class apache {
       server_name = public.velcrohurts.com
} 
node web3 { 
   class apache {
       server_name = public.velcrohurts.com
} 

Tuesday, June 9, 2015

Do it again

How about a super simple apache install?
This is just a really simple installation of apache, and it can be done with any application. I might even have a short cut in my text expander.

this is the modules/apache/manifests/init.pp

class apache { 
# Install package 
    package {'apache2':
        ensure  => installed,
    }

# set the configure for debian 
  file { '/etc/apache2/sites-available/velcrohurts.conf':
    ensure => file,
    mode   => '0644',
    owner  => 'root',
    group  => 'root',
    source => 'puppet:///modules/apache/velcrohurts.conf',
    require => Package['apache2'],
    notify => Exec ["reload-apache2"],
  }

# Reload apache with the config 
# And remove the default-000 config 
    exec {'/usr/sbin/a2dissite 000-default':
    require => Package['apache2'],
    notify => Exec ["reload-apache2"],
    }

# Now add your config 
    exec {'/usr/sbin/a2ensite ajplus':
      notify => Exec ["reload-apache2"],
      require => Package['apache2'],
    }

# the apache reloader 
    exec { 'reload-apache2':
      command => '/etc/init.d/apache2 reload',
      refreshonly => true,
  }
}



That just keeps it nice and simple. It installs the apache app, sets the config, and restarts apache.
My config is simple because I only have 1 site per server, so my modules/apache/files/velcrohurts.conf looks a lot like this.

# Managed by Puppet 
# All changes will be overwriten 

<VirtualHost *:80>
  ServerName internal.velcrohurts.net 
  DocumentRoot  /var/www/html/
  LogLevel info 
  LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" combined
  ErrorLog ${APACHE_LOG_DIR}/velcrohurts-error.log
  CustomLog ${APACHE_LOG_DIR}/velcrohurts-access.log combined
  RedirectMatch ^/$ http://velcrohurts.net/
  <Directory /var/www/html/> 
     AllowOverride all
     Order allow,deny 
     allow from all
  </Directory>
</virtualHost> 


Then finally, your module should look like this under your manifests/site.pp


node default { 
   include accounts 
} 

# web servers 
node web1 { 
    include apache 
} 
node web2 { 
    include apache 
} 

node web3 { 
    include apache 
}