Ask Your Question

Generate a template dynamically using environment attribute

asked 2016-01-28 06:40:04 -0500

rajeevsharma gravatar image

I have two environments setup Development and Production on PE 4.2.2. The basic thing I want to do is to isolate code and getting it deployed as per the environment chosen.

I am trying to write a template having the same code <environment = $environment>

which is suppose to generate lines dynamically like below as per the environment selected:

<environment = Development> : if code is deployed on dev env

<environment = Production> : if code is deployed on prod env

I see, there is no facter "environment" available and if possible we can make this using environment variable. But I am not very sure how to implement this. Any help would be appreciated.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-01-29 06:14:55 -0500

rajeevsharma gravatar image

Got an exact answer after digging up lot. To generate dynamic templates as per the environment selected we can do following:

set following code in: /etc/puppetlabs/code/hiera.yaml

  - yaml

  - "%{environment}"
  - "nodes/%{::trusted.certname}"
  - common

  :datadir: "/etc/puppetlabs/code/environments/%{environment}/hieradata"

Create environment specific yaml file (which should interpolate your varibale "%{environment}" defined in hierarchy) having following key value attributes:

cat /etc/puppetlabs/code/environments/testing/hieradata/testing.yaml
environment: 'testing'

cat /etc/puppetlabs/code/environments/production/hieradata/production.yaml
environment:  'production'

Lookup command to check everything is working fine:

# hiera environment environment=testing
# hiera environment environment=production

Likewise this can be coded in module to generate a template. Thanks.

edit flag offensive delete link more

answered 2016-01-28 09:50:08 -0500

gschandra gravatar image

You can try using directory environments for all your environments. And hiera to define you environment specific values.

edit flag offensive delete link more


I am already using directory environments and what if I don't want to use hiera? Is there anything we can achieve using environment variable or facters?

rajeevsharma gravatar imagerajeevsharma ( 2016-01-29 00:07:52 -0500 )edit

Thanks for the pointer.

rajeevsharma gravatar imagerajeevsharma ( 2016-01-29 06:15:49 -0500 )edit

That is exactly how I did with hiera. Also, you can create your own facters if needed.

gschandra gravatar imagegschandra ( 2016-01-29 09:39:15 -0500 )edit

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


Asked: 2016-01-28 06:40:04 -0500

Seen: 107 times

Last updated: Jan 29 '16