Ask Your Question
0

Load files based on AWS region

asked 2016-11-07 03:09:58 -0600

Hi,

So, I have 2 EC2 instances in 2 different AWS regions. Both are installing certain packages. However, the configuration between both servers is slightly different, based on their AWS region.

What I want to achieve is that depending on the AWS region, I load a different configuration file within a module. So in the piece below for example, I either want to load the config for Region 1 or config for Region 2.

file { 'aws-config':
    path    => '/opt/.aws/config',
    ensure  => file,
    backup  => false,
    owner => root,
    group => root,
    mode  => 644,
    require => File['aws-directory'],
    source => "puppet:///modules/aws_our/config",
 }

What is the best way to achieve this?

Also in regards of templates rather then files. What is the best way to have different variables within a template, based on the region?

I read about having multiple sources like this;

    source      => [ 
     "puppet://puppet/modules/aws_our/config$region", # Region file
     "puppet://puppet/modules/aws_our/config" # Default file
],

How on earth can I get the region here?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-11-07 08:05:41 -0600

DarylW gravatar image

updated 2016-11-07 08:09:06 -0600

There are two problems..

1) How can I get the region? 2) what do I do with the region to select the functionality that I want

1 should be pretty easy, either with a custom fact or the built in ec2 facts..
In puppet 3.x, I can get the region information like so..

It isn't directly the region, but there is a fact called ec2_placement_availablity_zone. You could take that and remove the 'az' information and leave the region in place.

you can also pull the information directly from the metadata service... on the command line using jq, I do the following

curl -s -fq http://169.254.169.254/latest/dynamic/instance-identity/document | jq --raw-output .region

if you make that call within a custom factor fact, you can pull back the json, convert it to a hash and extract the region information. (or if you ensure that jq and curl are already present on your box before you run puppet, you could put that as is into either a custom or external fact)

For part two, the files vs templates with variables. The way that I would go is with logic and/or variables built into a template if it is a simple difference. If you are using an erb based template, you can reference any variable that is 'in scope' of your template call with @varname inside the erb template. with epp templates, I believe you can pass the values directly as parameter lists which helps make things explicit.

Hope that helps!

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-11-07 03:09:58 -0600

Seen: 60 times

Last updated: Nov 07 '16