Skip to content

ratbox/puppet-dotenv

Repository files navigation

Manage .env configuration files

Table of Contents

  1. Description
  2. Setup requirements
  3. Usage - Configuration options and additional functionality
  4. Limitations - OS compatibility, etc.
  5. Credits
  6. Licensing information

Description

This Puppet module provides the dotenv defined type for managing .env files commonly used for storing configuration separately from application code; see the The Twelve-Factor App methodology for details of this concept.

The module also supplies the dotenv function that produces a string representation of .env file contents from a Puppet hash.

Setup Requirements

This module depends on the puppetlabs/stdlib module.

Usage

dotenv defined type

The dotenv defined type has the following attributes:

  • path (namevar) – absolute path to the .env file to manage;
  • ensure (default: present) – manages the file as present or absent;
  • mode (default: undef) – file mode to set (DAC permissions);
  • owner (default: undef) – file owner to set;
  • group (default: undef) – file group to set;
  • force (default: false) – see the force attribute of the underlying file resource type;
  • entries – hash of key-value entries to write into the file.

dotenv function

The dotenv function takes a Puppet hash of key-value entries as its sole parameter and returns a string that represents the corresponding .env file contents.

Please see the last example below for a use case.

Examples

Manage a web application configuration file:

dotenv { 'Web app config':
  path    => '/var/www/app/.env',
  mode    => '0400',
  owner   => 'www-data',
  group   => 'www-data',
  entries => {
    'MYSQL_HOSTNAME' => 'localhost',
    'MYSQL_USERNAME' => 'user',
    'MYSQL_PASSWORD' => 'secret',
    'MYSQL_DATABASE' => 'dbname',
  },
}

The result in the /var/www/app/.env file will be:

# Managed by Puppet

MYSQL_HOSTNAME="localhost"
MYSQL_USERNAME="user"
MYSQL_PASSWORD="secret"
MYSQL_DATABASE="dbname"

Manage a web application configuration file with entries looked up in Hiera:

$entries = lookup('app_env', Hash, deep)

dotenv { 'Web app config':
  path    => '/var/www/app/.env',
  mode    => '0400',
  owner   => 'www-data',
  group   => 'www-data',
  entries => $entries,
}

Previous example using the base file resource type and the dotenv function (in case you might want more control over the resource attributes–note the backup attribute):

$entries = lookup('app_env', Hash, deep)

file { 'Web app config':
  path    => '/var/www/app/.env',
  backup  => false,
  mode    => '0400',
  owner   => 'www-data',
  group   => 'www-data',
  content => dotenv($entries),
}

Limitations

The dotenv defined type is a wrapper around the file resource type and implements a narrow subset of its attributes.

Credits

The dotenv function is a stripped down version of the hash2kv function from the mmckinst/hash2stuff module by Mark McKinstry.

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

Puppet module for managing .env files

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published