puppetpuppet-enterprise

How to replace file if checksum is different and then restart the service?


How do I compare a file in tenant VM and replace it if checksum is different from the source (catalog pulled from Puppet Master)? Subsequently restart the service if file is replaced. Do nothing if file is the same.

File in tenant VM /etc/rsyslog.d/proxy.conf. I have the following code but it is not replacing (edited /etc/rsyslog.d/proxy.conf then executed puppet agent -t) nor is it restarting the service.

class lin_proxy::service {
  service { 'syslog':
    ensure  => 'running',
    enable  => true,
  }
  
  file { '/etc/rsyslog.d/proxy.conf':
    notify         => Service['syslog']
    ensure         => present,
    path           => '/etc/rsyslog.d/proxy.conf'
    replace        => 'yes',
    source         => 'puppet:///modules/lin_proxy/proxy.conf',
    checksum       => 'md5'
    checksum_value => 'dcb0c65283e52fe7aff25cd69200eb69'
    mode           => '0644',
    owner          => 'root',
    group          => 'root'
  }
}

Solution

  • How do I compare a file in tenant VM and replace it if checksum is different from the source (catalog pulled from Puppet Master)? Subsequently restart the service if file is replaced. Do nothing if file is the same.

    The class presented should do the job, provided that it is actually applied to the target node, and that all the details are in fact correct (especially the service name and checksum value).

    It is very unusual, however, to express a checksum value in the resource declaration instead of letting Puppet calculate it at need, and it is uncommon under any other circumstances to specify a particular checksum algorithm. It is also unusual to explicitly specify replace => 'yes', which is the default, and ensure => present is probably not as strong as you want. Stylistically, it is also usual to use chaining arrows to express relationships where that is feasible, and it is unusual to explicitly specify the path property when it matches the resource title.

    Overall, then, I would write your code like this:

    class lin_proxy::service {
      file { '/etc/rsyslog.d/proxy.conf':
        ensure => 'file',
        source => 'puppet:///modules/lin_proxy/proxy.conf',
        mode   => '0644',
        owner  => 'root',
        group  => 'root',
      }
    
      ~> service { 'syslog':
        ensure => 'running',
        enable => true,
      }
    }
    

    However, I do not expect that version to resolve your problem, which most likely arises from your class not having been assigned to the target node in the first place, or possibly from having an environment timeout that prevents Puppet from recognizing your class changes. You can check on that by looking at the node's cached copy of its catalog to check whether the class and the two resources are actually present, and whether they have the properties you expect.