# How to avoid hard coding node names

I'm currently running an environment where we have multiple clusters (i.e. application nodes, database nodes, etc.) and have separate manifests for each. Currently within our manifests we are having to hardcode our nodenames (xxxdev,xxxstage,xxxprod). The problem with this is that we have a puppetmaster per environment which houses this code for each of our testing environments. So when we migrate our scripts, we have to manually touch each of these files to remove the xxxdev and replace it with xxxstage or xxxprod.

What I'm looking for is a more dynamic way to set our node names. Could we do something like set a variable called $pupenv='dev' or$pupenv='stage' in the site.pp manifest and then have nodenames set as follows in the rest of the manifests? :

node 'xxx$env' { and it would evaluate to xxxdev  or is there a way within hiera to set an array of servers (xxxprd1,xxxprd2) and then say : node hiera('app') {  and it would populate for each app within that hiera variable Any input would be appreciated. edit retag close merge delete ## 1 Answer Sort by » oldest newest most voted You can use regular expressions to define your node names https://docs.puppetlabs.com/puppet/3....nodedefinitions.html node /^(foo|bar)\.example\.com$/ {
include common
}


More likely, what you are looking for is directory environments

https://docs.puppetlabs.com/puppet/3....

For this you would create separate directories like

/etc/puppet/environments/prod
/etc/puppet/environments/dev
/etc/puppet/environments/stage


Then on the nodes, you can choose what environment they are in either with your enc, or in the puppet.conf

If you want to keep your 3 separate puppet masters, you could try leveraging facter's ability to read in environment variables.

For example if you environment contains a string that starts with FACTER_ it is accessible from manifests.

$export FACTER_FOO='production' node "web-${::foo}"


This will then expand to 'web-production'

more