Havnar
Havnar

Reputation: 2628

add entries to hosts file based on facts

In Puppet I would like to create entries to all hosts files in a large group of servers.

256.344.987.776     6.fqn.mycompany.info my-hosts-hostname6
256.344.987.777     7.fqn.mycompany.info my-hosts-hostname7
256.344.987.778     8.fqn.mycompany.info my-hosts-hostname8
256.344.987.779     9.fqn.mycompany.info my-hosts-hostname9
256.344.987.780     10.fqn.mycompany.info my-hosts-hostname10

where the IP is taken from eth2 fact, the fqn is taken from concatting a fact hostname to domain, the short notation would be the fact: hostname.

I'm not sure how to best approach this.

Upvotes: 1

Views: 203

Answers (1)

John Bollinger
John Bollinger

Reputation: 180103

It sounds like you want to glean the information from all of your hosts, collate it, and provide it to all the hosts. This is one of the classic use cases for exported resources. And of course, Puppet provides a built-in Host resource type for managing the individual entries. A minimal class that handles such a job might look like this:

class site::hosts {

  # Export *this* host's entry for all machines to pick up
  @@host { "${hostname}.${domain}":
    ensure => 'present',
    ip => $ipaddress_eth2,
    host_aliases => ${hostname}
  }

  # Apply *all* machines' hosts entries to this machine
  Host<<| |>>
}

You will need to have exported resources enabled on your master for this to work. After you first put it into place, it may take a couple of cycles to stabilize, as on any given run, each host will receive only the entries provided by machines that have already received catalogs with that class in them.

Upvotes: 2

Related Questions