# syntax error in crontab

**URL:** <https://forum.ansible.com/t/syntax-error-in-crontab/38662>\
**Category:** Ansible Project\
**Created:** [January 23, 2024, 11:10am UTC](https://forum.ansible.com/t/syntax-error-in-crontab/38662 "2024-01-23T11:10:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [January 23, 2024, 11:10am UTC](https://forum.ansible.com/t/syntax-error-in-crontab/38662/1 "2024-01-23T11:10:06Z")

</div>

Good morning. I am updating a cron job by replacing a line in the crontab file. My error comes when I use the "" (see below) to escape the “(”. Using the escape character works on the command line but Ansible keeps giving me a syntax error. Here is my code:

- name: Check for cron job to remove wazuh logs  
lineinfile:  
path: “{{ cron\_job\_path[ansible\_distribution] }}”  
regexp: “find /var/ossec/logs”  
line: “{{ 59|random }} {{ 23|random }} \* \* \* find /var/ossec/logs/ ( -name ‘_.gz’ -o -name '_.json’ -o -name ‘\*.log’ ) -type f -mtime +2 -delete; find /var/ossec/logs -type d -empty -delete”  
state: present

With this code, Ansible states “this one looks like it might be an issue with missing quotes”.

I need to escape the “(” for it to work in crontab/bash command line. When I delete the "" characters the Ansible error goes away, but of course it does not work in crontab/bash.

I’ve tried using " around each of the commands but that doesn’t work either.

Does anyone see where I have gone wrong?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [January 23, 2024, 12:21pm UTC](https://forum.ansible.com/t/syntax-error-in-crontab/38662/2 "2024-01-23T12:21:40Z")

</div>

To get the literal backslashes in the crontab file, you need to escape the backslashes (“\”) in your double-quoted YAML string.

Alternatively, you could change your double-quoted string to a single-quoted string, because backslashes aren’t “special” in single-quoted strings. If you do that, you’ll also need to double the single quotes to get literal single quotes — i.e. ‘_.gz’ becomes ‘’_.gz’’ etc. (Double-quoted strings and single-quoted strings have completely different rules.)

Another alternative is to change your string to a “block scalar” ([https://yaml-multiline.info/](https://yaml-multiline.info/)) where all quotes and backslashes are not “special”:

```auto
  line: |
    {{ 23|random }} * * * find /var/ossec/logs/ \( -name '*.gz' -o -name '*.json' -o -name '*.log' \) -type f -mtime +2 -delete; find /var/ossec/logs -type d -empty -delete

```

---

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [January 23, 2024, 12:42pm UTC](https://forum.ansible.com/t/syntax-error-in-crontab/38662/3 "2024-01-23T12:42:48Z")

</div>

That’s great - thanks. Not sure i understood “block scalars” before.

---

<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:** [January 23, 2024, 12:42pm UTC](https://forum.ansible.com/t/syntax-error-in-crontab/38662/4 "2024-01-23T12:42:52Z")

</div>

What Todd said. Worth mentioning that using ansible.builtin.cron might be slightly cleaner, although you do still have the same quoting/escaping concerns with the job specification.

Also worth mentioning that using {{ 59|random }} & {{ 23|random }} will result in a CHANGED every time you run the play or role. You can still get a pseudo-random variation in cronjob execution timing but consistency across runs using something like:

{{ range(0,59) | random(seed=inventory\_hostname) }}
