geting password value from trocla within puppet erb template - puppet

I have a template file (erb) in puppet which is actually the config file of a wordpress installation. The file looks like:
<?php
// DB config
define('DB_NAME', 'wpdb');
define('DB_USER', 'myuser');
define('DB_PASSWORD', 'mypass');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
$table_prefix = 'wp_';
..
..
What i wish to achieve is to be able to get the DB_PASSWORD value from trocla. I know that this can be achieved on a manifest file with the following method:
$myvar=trocla('testuser:plain')
However i wish to use trocla on the template erb file. Is this possible?

Something like:
$myvar = '<%= scope.function_trocla(['testuser:plain']).gsub(%r{[\\']}, '\\\\\\0') %>';
See Referencing Custom Functions In Templates in the manual.

Related

env file format weirdly

I'm working on node project, I'm using .env file to hide some of data. The .env file worked normally, and after I added more informations it got some color letters for variable names and it won't detect my variables when i'm using:
mongodb+srv://MONGODB_USERNAME:MONGODB_PASSWORD#cluster0.ekxmb.mongodb.net/MONGODB_DATABASE_NAME?retryWrites=true&w=majority
I'm having following code in my .env file:
MONGODB_PASSWORD = testpassword;
MONGODB_USERNAME = testuser;
MONGODB_DATABASE_NAME = testdbname;
It works when I manually type in app.js file code like this:
"mongodb+srv://testuser:testpassword#cluster0.ekxmb.mongodb.net/testdbname?retryWrites=true&w=majority"
After I added some additional variables code got formed weirdly with colors on variable names.
Note: It worked with code where I'm importing variables from .env file before, but after weird .env format it won't work.
Putting a variable name in a string will just make it say that variable name. In addition, you don't have the environment variables loaded anywhere.
Install the dotenv npm package for processing the .env file. Then, add this to your code to load the config file:
require('dotenv').config();
Create variables for each of your environment variables from process.env.YOUR_VARIABLE_NAME. An easy way to do this is with destructuring:
let {MONGODB_USERNAME, MONGODB_PASSWORD, MONGODB_DATABASE_NAME} = process.env;
Properly insert these variables into the string. You can use template literals for this:
`mongodb+srv://${MONGODB_USERNAME}:${MONGODB_PASSWORD}#cluster0.ekxmb.mongodb.net/${MONGODB_DATABASE_NAME}?retryWrites=true&w=majority`

How to indicate custom configuration files for terragrunt modules?

I am trying to build Terragrunt script for deploying the infrastructure to Microsoft Azure cloud. Things are working fairly well but I am not able to figure out one thing.
The structure of setup looks something like this:
rootdir
terragrunt.hcl
someconfig.hcl
module1dir
terragrunt.hcl
config.auto.tfvars.json
module2dir
terragrunt.hcl
config.auto.tfvars.json
module3dir
terragrunt.hcl
config.auto.tfvars.json
Each module is configured using Terraform autoload tfvars feature with config.auto.tfvars.json. What I would like is to have these files outside of the directory structure and somehow instruct Terragrunt to apply correct external configuration file to correct submodule.
Any ideas?
I solved this in the following manner:
Define environment variable you plan on using which should contain location to the configuration files. Make sure it is not clashing with anything existing. In this example we will use TGR_CFGDIR. In the external configuration module place the module configuration files and make sure they are properly named. Each file should be named as the module and end with .auto.tfvars.json. So if your module is named foo you should have config file foo.auto.tfvars.json. Change your terragrunt modules (terragrunt.hcl) to have these statements:
locals {
moduleconfig = get_env("TGR_CFGDIR")
modulename = basename(get_terragrunt_dir())
}
generate "configuration" {
path = "config.auto.tfvars.json"
if_exists = "overwrite"
disable_signature = true
contents = file("${local.moduleconfig}/${local.modulename}.auto.tfvars.json")
}
And finally call terragrunt cli like this:
TGR_CFGDIR="<configdir>" terragrunt "<somecommand>"

Terraform: using the file interpolation to read local file from a module

Is there a way to tell terraform that when I do something like
locals {
file_content = "${file("somefile.txt")}"
}
Inside a module, it sould read the file that's in the module?
It appears that if the file structure is this:
/module
/main.tf <- the code above is here
/somefile.txt
/project
/main.tf <- this uses the module from ../module
Terraform will look for the file under /project and not under /module
Thanks
You can use path.module to refer to the path of the module.
So in your module you would refer to it with:
locals {
file_content = "${file("${path.module}/somefile.txt")}"
}
This is also shown in the documentation for the file function:
> file("${path.module}/hello.txt")
Hello World

Different urls for development and production

I am trying to set up an CodeIgniter project with NGINX. However, it is an already website which is online, say for example test.com. This means that the project also has $config['base_url'] = 'test.com'. But when I want to setup an NGINX server block I have to define the server name that is the same as the domain in base_url, according to this post. However, I want to have url test.dev for the development and test.com should just link to the online website. How do I achieve this?
Set your ENVIRONMENT variable. https://www.codeigniter.com/user_guide/general/environments.html
For your production
define('ENVIRONMENT','production');
and for development
define('ENVIRONMENT','production');
and in your config file you can check like this or any variables
if(ENVIRONMENT == 'development'){
$config['base_url'] = 'test.dev';
}else{
$config['base_url'] = 'test.com';
}
In CI, you can define a config.php file for each environment you are using.
In your config folder, create a development folder in which you place a config.php file with the base_url you need. Then, in that environment, CI will use that new file instead of the main config.php file.
Create Environments. Handling Multiple Environments
In your index.php Find a line which says
//define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
Under that line place a check on dirname and paste this code
switch(dirname(__FILE__))
{
case 'Path\of\your\live\server\folder':
define('ENVIRONMENT','production');
break;
case 'path\of\your\local\folder':
define('ENVIRONMENT','development');
break;
}
Now you have a global variable ENVIRONMENT which has values production or development. You can place a switch on this variable and give values to base_url in config.php or choose active group in database.php for DB credentials .

Is this the correct way to change a config file using puppet?

I have a rails app and I'd like to change the ./config/environment/production.rb file to have a different config based on what I want that server to do.
So, I'm going into the .rb file from the .pp file and changing some strings then restarting the service. This just seems really poor form to me. Is there a better way to do this? I've been asked to deliver 1 RPM and change the config via puppet, so...
class Cloud-widget($MServer, $GoogleEarthServer, $CSever) {
package { "Cloud-widget":
ensure => installed
}
service { "Cloud-widget":
ensure => running,
}
<%
file_names = ['./config/environment/production.rb']
file_names.each do |file_name|
puts text.gsub(/.*config.mserver(.*)/, "config.mserver_root = \"#{$Merver}\"")
puts text.gsub(/.*config.google_earth_url(.*)/, "config.google_earth_url( = \"#{$GoogleEarthServer}\"")
puts text.gsub(/.*config.cserver_base_url(.*)/, "config.cserver_base_url = \"#{$CServer}\"")
end
File.open(file_name, "w") {|file| file.puts output_of_gsub}
%>
service { Cloud-widget:
ensure => running,
subscribe => File["./config/environment/production.rb"],
}
}
No, that is not a good way to achieve what you need.
You could look at templates and generate the config files that way. That way, you can use variables in the config file.
If you need create conf from pattern you should use INI-file module from Puppetlabs
ini_setting { "sample setting":
path => '/tmp/foo.ini',
section => 'foo',
setting => 'foosetting',
value => 'FOO!',
ensure => present,
}
install this module from puppet:
puppet module install cprice404-inifile

Resources