Issue
I have below Ansible cron
task to enable/disable cron.
- name: Enable/Disable cron
cron:
cron_file: /var/spool/cron/myuser
user: myuser
state: present
disabled: true
name: my cron
job: cron script
minute: "0"
hour: "4"
But when I execute this crontab is created as
0 4 * * * myuser myscript.sh >> /tmp/myuser.log 2>&1
How can I avoid myuser
getting added in cron entry?
Solution
According the documentation cron
module – Manage cron.d
and crontab
entries, the given Examples and your current description as it is
How can I avoid myuser getting added
it is the expected behavior
TASK [Enable or disable cron] *****************************************
fatal: [localhost]: FAILED! => changed=false
msg: To use cron_file=... parameter you must specify user=... as well
and since it seems you are creating a cron file under /var/spool/cron/myuser
.
Please take note that a dedicated cron file is something different than an entry in a cron table file (crontab).
A minimal example like
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Enable or disable cron
cron:
cron_file: "/home/{{ ansible_user }}/test/cronfile"
user: "{{ ansible_user }}"
state: present
disabled: true
name: my cron
job: cron script
minute: "0"
hour: "4"
will result into a cron file of
cat cronfile
#Ansible: my cron
#0 4 * * * user cron script
or if disabled: false
of
0 4 * * * user cron script
To summarize, you can't avoid it and would need an other approach.
Further Readings
about
- Difference between
cron
,crontab
, andcronjob
? cronjob
entry incrontab -e
vs/etc/crontab
. Which one is better?- Are
crontab -e
&/etc/crontab
the same? - Difference between
cron
andcrontab
?
... you could to make sure the presence or absence of the cron file in example, but unfortunately there is almost no description of what you try to achieve.
Answered By - U880D Answer Checked By - Dawn Plyler (WPSolving Volunteer)