-1

I need to write an Ansible Playbook that configures a linux machine to routinely prune files in /etc/dummy/ older than 1 hour in a configurable interval as a configurable user

So, this is what I tried. But I am having some difficulties. So before actually running this in production, I need to verify if the interval is correct. i.e that the script will run every 2 hours. And I need to make sure the cronjob will be created and will be persisted even after a system reboot.

---
- hosts: servers
  vars:
    olderthan: 1
    intervals: 120 # needs to run every 2 hours, everyday
    user: tdp
    scriptpath: /etc/tmp/pruner.sh

  tasks:

    - name: Copy the file
      copy:
        src: "{{ scriptpath }}"
        dest: "{{ scriptpath }}"

   - name: "The Dummy Pruner"
     cron:
      name: "Prune the items at /etc/dummy"
      user: "{{ user }}"
      minute: "{{ intervals }}"
      hour: "*"
      day: "*"
      weekday: "*"
      month: "*"
      job: "sh {{ scriptpath }} >> {{ scriptpath }}/results.log 2>&1"
      state: present

and my /etc/tmp/pruner.sh looks like this:

#!/bin/bash

find /etc/dummy -name '*.*' -mmin +60 -delete > /dev/null

Can someone help me figure out the above issues?

Jananath Banuka
  • 2,951
  • 8
  • 57
  • 105
  • 2
    _Can someone help me figure out the above issues_ > what issues? You described your purposed action but not what is problematic here. – β.εηοιτ.βε Feb 22 '22 at 22:24
  • @β.εηοιτ.βε: Issue is my doubts regarding the cron expression I have given. Need to run cronjob every 2 hours – Jananath Banuka Feb 23 '22 at 10:32
  • 1
    So, you mean that you are going to trust the saying of a stranger on Internet above a trial from your side that is going to prove you with facts that it works? Just test it. You can test it on your controller first to be sure, or in a container, or in a VM spined by Vagrant, ... – β.εηοιτ.βε Feb 23 '22 at 20:48

2 Answers2

1

According the documentation cron - Manage cron.d and crontab entries the Parameter for minute needs to be 0-59. You may have a look into the there given Examples.

Instead of describing the interval with 120 minutes for minute you need to describe it with every 2 hour */2 for hour.

Further Q&A

... and many more

U880D
  • 8,601
  • 6
  • 24
  • 40
0

Workaround can be putting complete line into the crontab by using module lineinfile or blockinfile if you need to put much more rather than one line.

ouflak
  • 2,458
  • 10
  • 44
  • 49
SPR
  • 15
  • 4