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?
CodePudding user response:
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.
CodePudding user response:
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