I thought it will be a piece of cake to configure crontab with Ansible cron module, but there is something I can't understand.
This is simple task I wrote:
- name: Add job triggering logs rotation.
cron:
name: 'logrotate'
minute: '*/2'
job: '/etc/cron.daily/logrotate'
state: present
tags: cronjob
When I run it the output from Ansible was successful as I thought:
TASK [cron : Add job triggering logs rotation.] **************************************************************************************
task path: /home/vagrant/ansible/roles/cron/tasks/main.yml:3
Thursday 19 December 2019 19:31:05 +0000 (0:00:00.023) 0:00:02.427 *****
Using module file /usr/lib/python3/dist-packages/ansible/modules/system/cron.py
<127.0.0.1> ESTABLISH SSH CONNECTION FOR USER: None
<127.0.0.1> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 127.0.0.1 '/bin/sh -c '"'"'sudo -H -S -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-; /usr/bin/python'"'"'"'"'"'"'"'"' && sleep 0'"'"''
Escalation succeeded
<127.0.0.1> (0, b'\n{"envs": [], "invocation": {"module_args": {"name": "logrotate", "insertbefore": null, "state": "present", "cron_file": null, "reboot": false, "hour": "*", "month": "*", "disabled": false, "job": "/etc/cron.daily/logrotate", "special_time": null, "user": null, "env": null, "insertafter": null, "backup": false, "day": "*", "minute": "*/2", "weekday": "*"}}, "changed": true, "jobs": ["logrotate"], "warnings": []}\n', b'')
changed: [127.0.0.1] => {
"changed": true,
"envs": [],
"invocation": {
"module_args": {
"backup": false,
"cron_file": null,
"day": "*",
"disabled": false,
"env": null,
"hour": "*",
"insertafter": null,
"insertbefore": null,
"job": "/etc/cron.daily/logrotate",
"minute": "*/2",
"month": "*",
"name": "logrotate",
"reboot": false,
"special_time": null,
"state": "present",
"user": null,
"weekday": "*"
}
},
"jobs": [
"logrotate"
]
}
However, when I check /etc/crontab/
the output is not there.
The thing I want to achieve is to have this in /etc/crontab/
:
*/5 * * * * /etc/cron.daily/logrotate
By default is should add this job to root crontab, but I check also my user contab to be sure if it is not there and it's not.
Now, every time I try to run task, Ansible output is OK
. I don't know where was it added and have no idea what went wrong.
Any idea what happened?
the way you defined the ansible task will create the job in the users default cronfile. You can look at it with "crontab -l" oder edit it with "crontab -e". You can put it into the "/etc/cron.d"-Folder when you use the "cron_file" option. Take a look at https://docs.ansible.com/ansible/latest/modules/cron_module.html