Ask Your Question
0

Can I use ip address in Hiera

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

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
2

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

JohnsonEarls gravatar image

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

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 255.255.255.0, 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:

…/hieradata/networks/10.25.x.0
…/hieradata/networks/10.25.y.0
…/hieradata/networks/10.25.z.0
…/hieradata/networks/10.38.a.0
…/hieradata/networks/10.38.b.0
…/hieradata/networks/10.38.c.0

Alternately,

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

…/hieradata/sites/production
…/hieradata/sites/DR

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.

#!/bin/sh
# 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" ;;
esac
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

site_type=production

or

site_type=DR

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

edit flag offensive delete link more

Comments

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 -0500 )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 -0500 )edit

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

bnwcraft gravatar imagebnwcraft ( 2015-05-07 02:34:32 -0500 )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 -0500 )edit

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

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

2 followers

Stats

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

Seen: 832 times

Last updated: May 07 '15