user1240076
user1240076

Reputation: 303

How to apply resource only if content would change

I am trying to maintain a nonstandard hosts location using puppet's hosts resource type. Since it is nonstandard hosts file its content is not "prefetched" by puppet and then it is not possible to do something like purging entries. In order to workaround the issue I want to remove the file before puppet is applying any changes to that file. However, I don't want to remove the file every time puppet is running but rather only if there is something to be changed. Is there a way to apply configurations for the resource only if there is going to change anything ?

Right now, I define hosts via hiera and use create_resources function to produce the desired hosts resources.

create_resources(host,$host_entries)

To make sure that there are not any other entries, my most simple idea is to make sure file doesnt exist, right before applying the host configuration:

file { '/nonstandard/hosts':
  ensure => absent,
}

By doing so the hosts will be always removed, even if there is nothing to change. As it will be in 99 percent of the cases. So what options I have to remove the file only in case of create_resources(host,$host_entries) will really bring something new. Maybe there is a different and more simple approach ?

Upvotes: 0

Views: 101

Answers (1)

John Bollinger
John Bollinger

Reputation: 180093

Is there a way to apply configurations for the resource only if there is going to change anything ?

Not in a general sense. What you could do instead is write a custom fact that provides a list of the hosts defined by your custom hosts file (only the hostnames are needed), and based on the value of that fact and your hiera data, generate Host resources to ensure absent those hosts for which you do not have definitions. That does, however, assume that all of the hosts that should be listed in the file are known to you from Hiera data.

Upvotes: 1

Related Questions