Puppet code for Postgres installation
We have done our best to make it as easy as possible to use Puppet to change your “stem-cell” system into a running Postgres database. Let’s look at some of the Puppet code needed to do this.
In the editor tab, go to the directory hierdata\nodes
and open the file pg14.playground.enterprisemodules.com
. This file contains all the node-specific data.
At the top, you see:
role: role::postgres::simple_db
This hiera definition tells Puppet that for this node, node pg14.enterprisemodules.com
, we want to apply the Puppet class role::postgres::simple_db
.
The role role::postgres::simple_db
Let’s see what this Puppet class is doing for us. In the editor tab, go to the directory modules\role\manifests\postgres
and open the file simple_db.pp
. For your convenience, we have included the contents here as well.
class role::postgres::simple_db()
{
contain ::profile::base
contain ::pg_profile::database
Class['::profile::base']
-> Class['::pg_profile::database']
}
As you can see, this role class contains (is special sort of include) two other classes. The ::profile::base
class is what we call the base profile. A base profile contains all Puppet definitions you would like to apply to all nodes in your organization. It includes all the standard settings you want to apply to your system, no matter what.
The second include is the ::pg_profile::database
class. This Puppet class is where all the magic happens needed to transform this node into an Postgres database.
As you can see, this is pretty easy. Let’s see it in action!
Ensure prerequisites
There are not a lot of prerequisites before you can install the postgres package. But if you wanted to change the Postgres uid of gid, or install some extra packages, the pg_profile
has got you covered. The pg_profile::database
Puppet class would silently take care of all prerequisites for you. For teaching purposes, we have disabled this.
In the editor tab, go to the directory hierdata\nodes
and open the file pg14.playground.enterprisemodules.com
. This file contains all the node-specific data.
In the top of the file you see this:
pg_profile::database::em_license: skip
pg_profile::database::sysctl: skip
pg_profile::database::limits: skip
pg_profile::database::groups_and_users: skip
pg_profile::database::packages: skip
pg_profile::database::db_software: skip
pg_profile::database::db_init: skip
pg_profile::database::db_startup: skip
pg_profile::database::db_roles: skip
pg_profile::database::db_definition: skip
pg_profile::database::db_parameters: skip
pg_profile::database::db_tablespaces: skip
pg_profile::database::db_schemas: skip
pg_profile::database::db_records: skip
This is the hiera settings that tell pg_profile
NOT to execute the software, autostart etc steps.
First Puppet run
With all the skips in place, let’s run Puppet and see what happens.
Well, that didn’t do a lot.
Notice: Compiled catalog for pg14.playground.enterprisemodules.com in environment production in 0.25 seconds
Notice: Applied catalog in 0.13 seconds
Actually, what it did, is executing only the base profile. And in this playground, the base profile is minimal.
Re-enable all prerequisite steps.
For the next step, we are going to remove some of the skip definitions in the hiera. Open the node yaml in the editor tab and remove or comment out all lines in the second block of skips. Your hiera should look something like this now:
# pg_profile::database::em_license: skip
# pg_profile::database::sysctl: skip
# pg_profile::database::limits: skip
# pg_profile::database::groups_and_users: skip
# pg_profile::database::packages: skip
pg_profile::database::db_software: skip
pg_profile::database::db_init: skip
pg_profile::database::db_startup: skip
pg_profile::database::db_roles: skip
pg_profile::database::db_definition: skip
pg_profile::database::db_parameters: skip
pg_profile::database::db_tablespaces: skip
pg_profile::database::db_schemas: skip
pg_profile::database::db_records: skip
Re-run Puppet
So now Puppet with the postgres modules executes much more of its regular steps. Let’s see what a re-run of Puppet provides us.
The result is following:
Notice: Compiled catalog for pg14.playground.enterprisemodules.com in environment production in 1.27 seconds
Notice: /Stage[setup]/Easy_type::License::Available/File[/etc/puppetlabs/puppet/em_license]/ensure: created
Notice: /Stage[setup]/Easy_type::License::Available/File[/etc/puppetlabs/puppet/em_license/.gitkeep]/ensure: defined content as '{md5}d41d8cd98f00b204e9800998ecf8427e'
Notice: Applied catalog in 0.49 seconds
Because we didn’t specify any data for users, groups, packages, only the pg_profile::database::em_license
class actually does something. It ensures that the EM licenses file is in the correct spot.
Wow, that is a lot more. Puppet now:
All prerequisites needed for a successful Postgres database server installation are now fulfilled.
Next up: Install the Postgres software itself.
Install the Postgres software
With all of the prerequisites done, it is time to install some Oracle software.
In the editor tab, go to the directory hierdata\nodes
and open the file pg14.playground.enterprisemodules.com.yaml
. This file contains all the node-specific data.
Let’s comment out the skip for the step pg_profile::database::db_software
as well now . Meaning: next Puppet run, DO execute the software installation.
Your hiera should look something like this now:
#
# pg_profile::database::em_license: skip
# pg_profile::database::sysctl: skip
# pg_profile::database::limits: skip
# pg_profile::database::groups_and_users: skip
# pg_profile::database::packages: skip
# pg_profile::database::db_software: skip
#
Re-run Puppet
Let’s see what Puppet has got in store for us now and re-run Puppet.
The output should look similar to the following:
Notice: Compiled catalog for pg14.playground.enterprisemodules.com in environment production in 0.33 seconds
Notice: Ensure Postgres software version 14
Notice: /Stage[main]/Pg_profile::Database::Db_software/File[/etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG-14]/ensure: defined content as '{md5}b45f4ff074210f1c72422bced5501338'
Notice: /Stage[main]/Pg_profile::Database::Db_software/Yumrepo[yum.postgresql.org]/ensure: created
Notice: /Stage[main]/Pg_profile::Database::Db_software/Package[postgresql14-server]/ensure: created
Notice: Applied catalog in 10.49 seconds
So, now pg_profile
will execute all of the steps required to install the Postgres software. Since it has already taken care of the prerequisites in the previous Puppet run, it will detect everything already in place and report no additional changes.
For the actual Oracle software installation, it executes these steps:
1) Make sure that Postgres repo gpg key is installed.
2) Add Postgres repository.
3) Install postgresql server package.
5) Install additional packages.
Re-run Puppet and check idempotency
What happens now if we run Puppet again?
Puppet detects everything is in its desired state (configured as described in your Puppet manifests and hierarchy settings) and changes nothing. It is idempotent.
Next up: Create a database.
Create the database
Last, but certainly not least, we will have to ensure the database is available to the users.
In the editor tab, go to the directory hierdata\nodes
and open the file pg14.playground.enterprisemodules.com.yaml
. This file contains all the node-specific data.
If you take a look at the third block of settings (lines 8 through 15), they should like this for now:
pg_profile::database::db_init: skip
pg_profile::database::db_startup: skip
pg_profile::database::db_roles: skip
pg_profile::database::db_definition: skip
pg_profile::database::db_parameters: skip
pg_profile::database::db_tablespaces: skip
pg_profile::database::db_schemas: skip
pg_profile::database::db_records: skip
This tell’s Puppet to skip all database steps. Like I explained before, this is here for teaching purposes. Let’s remove or comment out all of these skips, so Puppet will apply a full manifest that ensures the creation of the database and other Database objects.
Make sure yaml file now looks like this:
# pg_profile::database::db_init: skip
# pg_profile::database::db_startup: skip
# pg_profile::database::db_roles: skip
# pg_profile::database::db_definition: skip
# pg_profile::database::db_parameters: skip
# pg_profile::database::db_tablespaces: skip
# pg_profile::database::db_schemas: skip
# pg_profile::database::db_records: skip
#
Re-run Puppet
Let’s see what Puppet has in store for us now, as we re-run Puppet via the terminal window:
puppet apply site.pp
Notice: Compiled catalog for pg14.playground.enterprisemodules.com in environment production in 0.69 seconds
Notice: Ensure Postgres software version 14
Notice: Ensure Postgres initial setup
Notice: Ensure Postgres database start
Notice: Ensure Postgres database(s) testdb@localhost
Notice: /Stage[main]/Pg_profile::Database::Db_init/Exec[Initialize Postgres Database]/returns: executed successfully
Notice: /Stage[main]/Pg_profile::Database::Db_init/Pg_register[localhost]/default: defined 'default' as 'true'
Notice: /Stage[main]/Pg_profile::Database::Db_init/Pg_register[localhost]/user: defined 'user' as 'postgres'
Notice: /Stage[main]/Pg_profile::Database::Db_init/Pg_register[localhost]/os_user: defined 'os_user' as 'postgres'
Notice: /Stage[main]/Pg_profile::Database::Db_init/Pg_register[localhost]/database: defined 'database' as 'postgres'
Notice: /Stage[main]/Pg_profile::Database::Db_init/Pg_register[localhost]/connect_string: defined 'connect_string' as ''
Notice: /Stage[main]/Pg_profile::Database::Db_init/Pg_register[localhost]/daemonized: defined 'daemonized' as 'false'
Notice: /Stage[main]/Profile::Db_startup/Exec[run_postgres]/returns: executed successfully
Notice: /Stage[main]/Pg_profile::Database::Db_definition/Pg_database[testdb@localhost]/ensure: created
Notice: /Stage[main]/Pg_profile::Database::Db_definition/Pg_register[testdb]/default: defined 'default' as 'false'
Notice: /Stage[main]/Pg_profile::Database::Db_definition/Pg_register[testdb]/user: defined 'user' as 'postgres'
Notice: /Stage[main]/Pg_profile::Database::Db_definition/Pg_register[testdb]/os_user: defined 'os_user' as 'postgres'
Notice: /Stage[main]/Pg_profile::Database::Db_definition/Pg_register[testdb]/database: defined 'database' as 'testdb'
Notice: /Stage[main]/Pg_profile::Database::Db_definition/Pg_register[testdb]/connect_string: defined 'connect_string' as ''
Notice: /Stage[main]/Pg_profile::Database::Db_definition/Pg_register[testdb]/daemonized: defined 'daemonized' as 'false'
Notice: Applied catalog in 12.86 seconds
After this Puppet run, you’ll see that database postgres@localhost
has been created and is running on your playground system.
Re-run Puppet and check idempotency
And again, let’s see what happens if we run Puppet one last time:
Puppet detects everything is at its desired state, as described in your Puppet manifests and hierarchy settings. No changes will be done to the system. Another example of the built-in idempotency.
Next up: Introspecting your database
You like it?
Do you like what you see here and want to test this on your own infrastructure? No problem. You can sign up for a free trial.
If you have any questions, don’t hesitate to contact us.