Ask Your Question

managing /etc/hosts with Puppet and deleting duplicates

asked 2017-01-21 15:29:47 -0500

bess gravatar image


Im working a solution to dynamically manage /ect/hosts for my environment if a node has a certain tag.

For example,

if $facts['env'] =! undef {
  @@host { $facts['fqdn']:
     ip            => $facts['ipaddress'],
    host_aliases  => $facts['hostname'],
    tag           => [$env],
  ## get all stored hosts entries for $env from PuppetDB
  ## and ensure they exist in the hosts file.
  Host <<| tag == $env |>>


My question is, if say the IP or fqdn changes, will a new entry be appended to the hosts file persist?

If so, this dynamic solution will not work and ill have to keep using templates

I could write a function that parses /etc/hosts for these conditions and removes entries but the hosts resource should do that already.


edit retag flag offensive close merge delete


Why don't you just try it out?

Kai Burghardt gravatar imageKai Burghardt ( 2017-01-21 19:47:28 -0500 )edit

+1 try it out. I think there is also a 'purge' option that you can use to make sure it removes any host entries not managed by puppet. I believe your expected behavior is how it performs.

DarylW gravatar imageDarylW ( 2017-01-23 07:47:46 -0500 )edit

The purge option is a nogo since I need the ability to add manual entries and not have puppet clobber them. I'm probably going to settle on template but I liked the exported option :)

bess gravatar imagebess ( 2017-01-23 10:01:13 -0500 )edit

I don't understand "I need the ability to add entries not managed by puppet" but you say you are going with a template. Isn't that template still managed by puppet? You can add a lookup in hiera with a hash representing the entries you need added, and use create_resources to instantiate them

DarylW gravatar imageDarylW ( 2017-02-06 08:09:00 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2017-02-03 04:55:19 -0500

Josh gravatar image

The fqdn is the namevar, so if the ip address changes then the entry will get updated, e.g.

Notice: /Stage[main]/Main/Host[foo]/ip: ip changed '' to ''

If the fqdn changes, then that's a new resource so you could end up with multiple entries with the same IP address but different hostnames

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-01-21 15:29:47 -0500

Seen: 60 times

Last updated: Feb 03