Our myapp defined type will make use of the db resource we created in the previous section, passing four values to PuppetDB, directly from myapp::db. We'll use this manifest to build a MySQL server, and provide information to our WordPress instance on another node. Notice the following in the example:
- A regular defined type, with standard Puppet DSL. We build a server and a database to support the app.
- $host is not used in the manifest, but is passed along to the produced Db resource.
- Myapp::Db produces Db is placed directly after the define, in the same manifest:
define myapp::db (
$dbuser,
$dbpass,
$host = $::fqdn,
){
class {'::mysql::server':
root_password => 'Sup3rp@ssword!',
override_options => {
'mysqld' => {
'bind-address' => '0.0.0.0'
}
}
}
mysql::db { $name:
user => $dbuser,
password => $dbpass,
host => '%',
grant => ['ALL PRIVILEGES'],
}
}
Myapp::Db produces Db {
dbuser => $dbuser,
dbpass => $dbpass,
dbhost => $host,
dbname => $name,
}
Myapp::Web is a defined type meant to consume the Db produced by Myapp::Db. It installs the required packages, installs Apache, builds a vhost, and deploys WordPress to the docroot of the vhost. Notice the following:
- $vhost and $webpath were provided by application myapp.
- $dbuser, $dbpass, $dbhost, and $dbname are provided by the consumes Db {}.
- Because our manifest uses the values dbpass, dbhost, dbuser and dbname, our mappings don't need to be declared. The following example will directly declare variables:
define myapp::web (
$webpath,
$vhost,
$dbuser,
$dbpass,
$dbhost,
$dbname,
) {
package {['php',
'mysql',
'php-mysql',
'php-gd'
]:
ensure => installed,
}
class {'apache':
default_vhost => false
}
include ::apache::mod::php
apache::vhost { $vhost:
port => '80',
docroot => $webpath,
require => File[$webpath],
}
file { $webpath:
ensure => directory,
owner => 'apache',
group => 'apache',
require => Package['httpd'],
}
class { '::wordpress':
db_user => $dbuser,
db_password => $dbpass,
db_host => $dbhost,
db_name => $dbname,
create_db => false,
create_db_user => false,
install_dir => $webpath,
wp_owner => 'apache',
wp_group => 'apache',
}
}
Myapp::Web consumes Db { }
We can use the preceding collection of code to order and deploy our multitier application. Our current module should resemble the following:
myapp
├── lib
│ └── puppet
│ └── type
│ ├── sql.rb
├── manifests
├── db.pp
├── init.pp
└── web.pp
We can then use the puppet app and puppet job commands to deploy our application.