Search code examples
puppet

How can one set up a conditional subscribe in puppet enterprise 3.7?


I have a situation in my puppet enterprise 3.7 manifest where I could use a conditional 'subscribe' from my service to a file. I get this error message ...

Invalid relationship: Service[openstack-cinder-api] { subscribe => File[/etc/cinder/nfs-shares.conf] }, because File[/etc/cinder/nfs-shares.conf] doesn't seem to be in the catalog

... and here is the puppet code that results in the above error. Note that the error occurs when the condition $enabled_backends =~ /netapp-cinder/ evaluates to false.

class cinder (
... long list of parameters ...
) {
    if ( $enabled_backends =~ /netapp-cinder/ ) {
        file { '/etc/cinder/nfs-shares.conf':
            ensure  => file,
            path    => '/etc/cinder/nfs-shares.conf',
            owner   => cinder,
            group   => cinder,
            mode    => '0640',
            content => template('cinder/nfs-shares.conf.erb'),
        }
    }

    # long list of package and file resources 

    package {'qemu-kvm-rhev':
        ensure        => 'present',
        allow_virtual => false,
    } ->

    package { 'openstack-cinder':
        ensure => 'present',
        name   => 'openstack-cinder',
        before => [
            File['/etc/nova/nova.conf'],
        ],
    } ->

    package { 'python-cinderclient':
        ensure => 'present',
    } ->

    ... even more resource declarations ...

    service { 'openstack-cinder-api':
        ensure    => running,
        enable    => true,
        subscribe => [
            File['/etc/cinder/cinder.conf'],
            File['/etc/cinder/nfs-shares.conf'],
        ],
    } ->

    service { 'openstack-cinder-scheduler':
        ensure    => running,
        enable    => true,
        subscribe => [
            File['/etc/cinder/cinder.conf'],
            File['/etc/cinder/nfs-shares.conf'],
        ],
    } ->

    service { 'openstack-cinder-volume':
        ensure    => running,
        enable    => true,
        subscribe => [
            File['/etc/cinder/cinder.conf'],
            File['/etc/cinder/nfs-shares.conf'],
        ],
    }

    service { 'openstack-cinder-backup':
        ensure    => running,
        enable    => true,
        subscribe => [
            File['/etc/cinder/cinder.conf'],
            File['/etc/cinder/nfs-shares.conf'],
        ],
    } -> 

    ... more puppet code ...

Is there some way I can make the subscribe File['/etc/cinder/nfs-shares.conf'] conditional?


Solution

  • file { '/etc/cinder/nfs-shares.conf':
      ensure  => file,
      path    => '/etc/cinder/nfs-shares.conf',
      owner   => cinder,
      group   => cinder,
      mode    => '0640',
      content => template('cinder/nfs-shares.conf.erb'),
      notify  => Service['openstack-cinder-api', 'openstack-cinder-scheduler', 'openstack-cinder-volume', 'openstack-cinder-backup'],  <-- add this attribute
    }
    

    Then remove all the subscribes on File['/etc/cinder/nfs-shares.conf'] from your openstack services. Now the service restart trigger is inside your conditional and you will have your desired behavior.

    I would also recommend converting those openstack service resources into a lambda if you are using the future parser and upgrading from 3.7 since it is EOL for puppetlabs support.