Search code examples
puppethierafacter

apply config to group of servers in puppet + hiera


Need some help thinking.

I have 200 servers and have to apply a specific configuration to 100.

The only thing they share in common is the name prefix: they all start with zmb-

hiera.yaml

---
:backends:
  - yaml

:yaml:
  :datadir:

:hierarchy:
  - "nodes/pro/%{::trusted.certname}"
  - "nodes/hom/%{::trusted.certname}"
  - "nodes/%{::trusted.certname}"
  - "%{::os.family}"
  - common

:logger: puppet

What should i change in :hierarchy: ?


puppet version: 4.10.10


We need more information here, to understand what the data is you are sending to your 100 nodes, and why you believe this is a data problem, i.e. a problem to be solved in Hiera rather than in your Puppet manifests. - Alex Harvey

A very simple example to get things going:

until yesterday all nodes had the same NTP server, 10.1.1.1
this was set by the class ntp, which reads the values from common

common.yaml

---
classes:
    - base
    - ntp

ntp::server: '10.1.1.1'

ntp/init.pp

class ntp ($server) {
    ...
}

Now, today, i want to change the IP to 10.1.1.99 for 100 servers, and they all have their hostnames starting with zmb-

My question is: how?


Solution

  • There is no way to use a regular expression match (as far as I am aware, and as far as I can tell) in a Hiera hierarchy definition.

    You would need to firstly create a custom fact. For example, you could write:

    Facter.add(:host_code) do
      setcode do
        Facter.value(:hostname).slice(0..2)
      end
    end
    

    Then you could add a level:

    :hierarchy:
      - "nodes/%{::host_code}" # here
      - "nodes/pro/%{::trusted.certname}"
      - "nodes/hom/%{::trusted.certname}"
      - "nodes/%{::trusted.certname}"
      - "%{::os.family}"
      - common
    

    Then create a file hieradata/nodes/zmb.yaml.

    Of course, I would question that a fact based on the first three letters of the host name is a well-designed fact. What is it about those 100 nodes that causes them to have a hostname zmb-? The answer to that question probably gives you the proper basis of your new fact.