# ansible.builtin.cron

**URL:** <https://forum.ansible.com/t/ansible-builtin-cron/37903>\
**Category:** Ansible Project\
**Created:** [April 19, 2023, 2:26pm UTC](https://forum.ansible.com/t/ansible-builtin-cron/37903 "2023-04-19T14:26:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Welby](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@John\_Welby](https://forum.ansible.com/u/John_Welby)\
**Post date:** [April 19, 2023, 2:26pm UTC](https://forum.ansible.com/t/ansible-builtin-cron/37903/1 "2023-04-19T14:26:39Z")

</div>

All,

Can a playbook be run by ‘job’, i.e. instead of a script.

I created a playbook that creates an entry in crontab, which it does successfully, but it does not execute the playbook I specified in job:

Thanks!

John

---

<div class="post-metadata">

**Author:** ![Will\_McDonald](https://avatars.discourse-cdn.com/v4/letter/w/f475e1/32.png) [@Will\_McDonald](https://forum.ansible.com/u/Will_McDonald)\
**Post date:** [April 19, 2023, 2:55pm UTC](https://forum.ansible.com/t/ansible-builtin-cron/37903/2 "2023-04-19T14:55:39Z")

</div>

More information would be helpful here.

1. Can you share your usage of ansible.builtin.cron?

2. Can you share the output of crontab -l for the user you’re creating the cron job for/as, or the contents of /etc/crontab and/or /etc/cron.\* ?

3. What’s in your cronlog or systemd journal?

4. Obvious question but you do have a vixie cron variant installed and the appropriate services started?

---

<div class="post-metadata">

**Author:** ![John\_Welby](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@John\_Welby](https://forum.ansible.com/u/John_Welby)\
**Post date:** [April 19, 2023, 4:08pm UTC](https://forum.ansible.com/t/ansible-builtin-cron/37903/3 "2023-04-19T16:08:49Z")

</div>



---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [April 19, 2023, 4:16pm UTC](https://forum.ansible.com/t/ansible-builtin-cron/37903/4 "2023-04-19T16:16:56Z")

</div>

Things run from cron usually have a very limited set of time environments variables available to them.  
I assume you have no output at all now, so you don’t know where to start looking.

Start by ensuring that you receive any output from cron (pipe to email, write to log, whatever is appropriate in your use case)

That will reveal the reason why it’s not working.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [April 19, 2023, 4:18pm UTC](https://forum.ansible.com/t/ansible-builtin-cron/37903/5 "2023-04-19T16:18:17Z")

</div>

> job: “/home/jwelby/ansible/sub-playbooks/ansible-playbook -i dev-hosts /home/jwelby/ansible/sub-playbooks/patching.yml”

> 30 15 \* \* 3 /home/jwelby/ansible/sub-playbooks/patching.yaml

These don’t match. cron isn’t running ansible-playbook. It is trying to run the YAML file itself.

Walter

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [April 19, 2023, 4:21pm UTC](https://forum.ansible.com/t/ansible-builtin-cron/37903/6 "2023-04-19T16:21:18Z")

</div>

Also the file name extensions are different (yml vs yaml). Are we looking at some copy paste errors?

---

<div class="post-metadata">

**Author:** ![Will\_McDonald](https://avatars.discourse-cdn.com/v4/letter/w/f475e1/32.png) [@Will\_McDonald](https://forum.ansible.com/u/Will_McDonald)\
**Post date:** [April 19, 2023, 4:28pm UTC](https://forum.ansible.com/t/ansible-builtin-cron/37903/7 "2023-04-19T16:28:00Z")

</div>

There’s a discrepancy between your job as defined in your playbook and your output as listed from crontab -l. I suspect that’s just transcription but worth double-checking? (Walter and Dick have also spotted that.)

I’d also check that the ec2-user which is who you’re creating the crontab as has appropriate permissions and ownership of /home/jwelby/ and can descend into that tree and “do stuff”.

And finally, check your logs/journal. As Dick mentioned, the cron execution environment is restrictive, so it’s likely whatever’s happening will be logged under \_COMM = crontab or UNIT crontab.service.

I’d get it working with a simple debug job firing every 5 minutes with a playbook which just runs a debug or touches a file, check for its execution successfully in the journal logs then increase the complexity in stages.

---

<div class="post-metadata">

**Author:** ![John\_Welby](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@John\_Welby](https://forum.ansible.com/u/John_Welby)\
**Post date:** [April 19, 2023, 4:38pm UTC](https://forum.ansible.com/t/ansible-builtin-cron/37903/8 "2023-04-19T16:38:04Z")

</div>

Yes, there are copy/paste errors as I am transcribing and scrubbing info from a high-side environment.

---

<div class="post-metadata">

**Author:** ![Will\_McDonald](https://avatars.discourse-cdn.com/v4/letter/w/f475e1/32.png) [@Will\_McDonald](https://forum.ansible.com/u/Will_McDonald)\
**Post date:** [April 19, 2023, 5:18pm UTC](https://forum.ansible.com/t/ansible-builtin-cron/37903/9 "2023-04-19T17:18:41Z")

</div>

It works as expected for me on a test Fedora machine.

As a test user, create a debug playbook and test it runs.

[test-user@fedora ~]$ id  
uid=1001(test-user) gid=1001(test-user) groups=1001(test-user) context=unconfined\_u:unconfined\_r:unconfined\_t:s0-s0:c0.c1023  
[test-user@fedora ~]$ cat debug.yaml

---

<div class="post-metadata">

**Author:** ![John\_Welby](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@John\_Welby](https://forum.ansible.com/u/John_Welby)\
**Post date:** [April 19, 2023, 5:59pm UTC](https://forum.ansible.com/t/ansible-builtin-cron/37903/10 "2023-04-19T17:59:24Z")

</div>

Thanks, I will try that.
