puppetpuppet-enterprise

Puppet - Service require package before starting not working


Puppet is trying to start the service when the package is purged.

As a test I configured the package to be purged. But the service section is trying to start the zabbix-agent service. Any idea what went wrong?..

  package { 'zabbix-agent':
    name => $service_name,
    ensure => purged,
  }

  service { 'zabbix-agent':
    name => $service_name,
    ensure => running,
    enable => true,
    require => Package['zabbix-agent'],
    subscribe => File['zabbix-agentd.conf'],
  }

puppet agent -t output:

Error: Could not start Service[zabbix-agent]: Execution of '/bin/systemctl start zabbix-agent' returned 5: Failed to start zabbix-agent.service: Unit zabbix-agent.service not found.
Error: /Stage[main]/Zabbix/Service[zabbix-agent]/ensure: change from stopped to running failed: Could not start Service[zabbix-agent]: Execution of '/bin/systemctl start zabbix-agent' returned 5: Failed to start zabbix-agent.service: Unit zabbix-agent.service not found.
Notice: Finished catalog run in 0.25 seconds

Solution

  • When you enforce zabbix-agent to be running, you also need the package, so your package enforcement needs to be present.

      package { 'zabbix-agent':
        name   => $service_name,
        ensure => present,
      }
    
      service { 'zabbix-agent':
        name      => $service_name,
        ensure    => running,
        enable    => true,
        require   => Package['zabbix-agent'],
        subscribe => File['zabbix-agentd.conf'],
      }
    

    If you also want to have the capability of purging, that would come in a different class (e.g.:

    class zabbix::purge {
      package { 'zabbix-agent':
        name   => $service_name,
        ensure => purged,
      }
    
      service { 'zabbix-agent':
        name   => $service_name,
        ensure => false,
      }
    }
    

    As a consequence, you can't have both classes enforced to the same node, as you can't have the service in both states in the same time: running and purged.