Search code examples
cronpuppethiera

Puppet: Crons via Hiera?


I am trying to use hiera for my crons. Unfortunately this is not working with cron_core?

# common.yaml
---
my_module::cron::job::multiple:
  'my_cronjobs':
    jobs:
      # job1
      - {
          minute: 30,
          hour: 10,
          date: '*',
          month: '*',
          weekday: '*',
          user: user,
          command: '/bin/sleep 120',
          description: 'sleep for 2 min',
        }
      # job2
      - {
          minute: 0,
          hour: 11,
          date: '*',
          month: '*',
          weekday: '*',
          user: user,
          command: '/bin/sleep 60',
          description: 'sleep for 1 min',
        }

In my class I've tried

class my_module(){
    # enable crons
    include cron
}

But then it fails with Evaluation Error: Error while evaluating a Function Call, Could not find class ::cron

Is there a way to solve this with puppetlabs/cron_core?


Solution

  • So two things to note. First, you don't need to use cron_core. The cron resource type is included as standard. Second, cron is a resource type, not a class, so you can't include it.

    If you want to create cron jobs using Hiera, try using the create_resources function. Restructure your Hiera to look like

    my_module::cron::job::multiple:
     'sleep for 2 minutes': {
              minute: 30,
              hour: 10,
              date: '*',
              month: '*',
              weekday: '*',
              user: user,
              command: '/bin/sleep 120',
              description: 'sleep for 2 min',
            }
      'sleep for 1 minute': {
              minute: 0,
              hour: 11,
              date: '*',
              month: '*',
              weekday: '*',
              user: user,
              command: '/bin/sleep 60',
              description: 'sleep for 1 min',
            }
    

    and you'll then be able to add the resources using

    $cron_job_hash = lookup('my_module::cron::job::multiple', Hash, 'hash', {})
    create_resources('cron', $cron_job_hash)