Ask Your Question

Can I use ip address in Hiera

asked 2015-04-30 15:08:52 -0600

bnwcraft gravatar image

HI, My module is to manage the welcome message in the server, we having the DR servers and prd servers with difference ip set. for example all DR server is 10.25.x.x and prd servers is 10.38.x.x. in my module I plan to use hiera to control it, it is possible? if yes how I can difference this 2 groups of ip?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2015-04-30 22:47:25 -0600

JohnsonEarls gravatar image

updated 2015-05-07 11:59:25 -0600

You can use facts in hiera. One of the pre-defined facts you can use is network_device (e.g. network_eth0), which is based on the ip address and netmask of that interface. So in your …/hiera.yaml file, you could set up a hierarchy like...

  - "networks/%{network_eth0}"

then, assuming your netmask is, and your hosts are in the 10.25.{x,y,z}.0 networks in DR and 10.38.{a,b,c}.0 networks in production, create the following Hiera data sources:



You can also create a custom external fact (see the External Facts documentation) that determines whether your server is production or DR, and use that custom fact in your …/hiera.yaml hierarchy. For example, if you created a site_type fact that contains either production or DR, you could set up your hierarchy with:

  - "sites/%{site_type}"

and then create the two Hiera data sources


An example of a custom fact. This would have to be installed either at server deployment into the appropriate custom fact directory (/etc/puppet/facts.d for Puppet open source before 4.0 or /etc/puppetlabs/facter/facts.d for Puppet Enterprise or Puppet Open Source 4.x), or be added to a module in the facts.d/ directory. The file would have to be executable to run each time.

# determine if we are a production or DR site
# requires the `iproute` package to be installed
mynet=`ip -o -4 addr list | sed -n -e '/^.*inet 10\.\([^.]*\)\..*/10.\1/p'`
case $mynet in
  10.25) site="production" ;;
  10.38) site="DR" ;;
echo "site_type=${site}"

Alternatively, during server deployment, you can make the production-or-DR evaluation once, and create a simple text file in the appropriate facts.d directory that contains the line




Make that file not executable and it will just be evaluated as a list of facts.

edit flag offensive delete link more


Thanks for your reply. but I still not very understand about the part you mention create data sets,, can you have further example on it? really appreciate your sharing.

bnwcraft gravatar imagebnwcraft ( 2015-05-05 03:19:53 -0600 )edit

I expanded my answer and used the proper Hiera terminology of "data sources" for the `hieradata` files that contain the Hiera data. I also used the proper YAML syntax of enclosing in quotes any string that contains a `%`.

JohnsonEarls gravatar imageJohnsonEarls ( 2015-05-05 07:42:39 -0600 )edit

is it mean when the server ip from to then I need to list out all in the data sources one by one?

bnwcraft gravatar imagebnwcraft ( 2015-05-07 02:34:32 -0600 )edit

Yes. If you have that many networks to differentiate, then the better bet is to create a custom fact.

JohnsonEarls gravatar imageJohnsonEarls ( 2015-05-07 11:48:36 -0600 )edit

I updated my answer to give an example of a custom fact for you.

JohnsonEarls gravatar imageJohnsonEarls ( 2015-05-07 11:59:40 -0600 )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



Asked: 2015-04-30 15:08:52 -0600

Seen: 1,424 times

Last updated: May 07 '15