A collection of task oriented solutions in Puppet

 

Only add a file if it's absent

Challenge

Only add a file if it isn't already present. Once added, or if already there, leave it alone.

Solution

class absent_file {

  file { '/tmp/hello-file':
    ensure  => 'present',
    replace => 'no', # this is the important property
    content => "From Puppet\n",
    mode    => '0644',
  }

}

Explanation

Adding the replace property with the value of 'no' (or the uglier 'false') to a file will stop puppet managing its content if the file already exists. If it's not already present the file will be created as per the file types parameters and then left alone - even if its content changes on either the puppet master or client side.

In general this isn't the best approach as all configuration should be under centralised management. Having local changes that can drift over time will eventually cause pain and make your systems baseline configuration harder to predict.

It's worth noting that the file type will still maintain permissions and other such file properties if they change on the client. Even with replace set to 'no'.