checking to see if cron entry exists

397 views
Skip to first unread message

Kathy L

unread,
Jun 28, 2023, 10:55:36 AM6/28/23
to Ansible Project
I am checking to see if a cron job exists and if it doesn't, add it.  Because we use random times (for both hours and minutes) I cannot search for the entire line.  Here is what I have:

    - name: UPDATE_WAZUH
      lineinfile:
        path: /var/spool/cron/crontabs/root
        regexp: "find /var/log/nginx/nginx_error.log"
        state: present
     register: remove_nginx_logs

    - name: UPDATE_WAZUH
      cron:
        name: "empty nginx logs"
        minute: "{{ 59|random }}"
        hour: "{{ 23|random }}"
        job: 'find /var/log/nginx/nginx_error.log -name "*.gz" -type f -mtime +15 -delete'
    when: remove_wazuh_logs is defined

One of the errors I get is:

   line is required with state=present

When I try adding line to lineinfile like this:

    - name: UPDATE_WAZUH
      lineinfile:
        path: /var/spool/cron/crontabs/root
        regexp: "find /var/log/nginx/nginx_error.log"
        state: present
        line: 'find /var/log/nginx/nginx_error.log -name "*.gz" -type f -mtime +15 -delete'
     register: remove_nginx_logs


It still fails.  How can I accomplish this?

Rowe, Walter P. (Fed)

unread,
Jun 28, 2023, 11:01:41 AM6/28/23
to ansible...@googlegroups.com
lineinfile wants to add the line if it is not "present". You can collapse these two tasks into a single task using just linieinfile.

Remember that each task is describing an "desired state". In a single task you can look for and add the line if missing. If it is present the task will make no change. if it is missing the task will add it.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/52410dee-5afe-405d-94da-bcf0074d9fe4n%40googlegroups.com.

Brian Coca

unread,
Jun 28, 2023, 11:50:56 AM6/28/23
to ansible...@googlegroups.com
If you want to just see if things 'would change':
`check_mode: true`

not all modules support it, but both cron and lininefile modules say they do
--
----------
Brian Coca

Kathy L

unread,
Jun 29, 2023, 6:35:04 AM6/29/23
to Ansible Project
Both items make sense - thank you.

Nico Kadel-Garcia

unread,
Jun 29, 2023, 8:59:25 AM6/29/23
to ansible...@googlegroups.com
On Thu, Jun 29, 2023 at 6:35 AM Kathy L <lyon...@gmail.com> wrote:
>
> Both items make sense - thank you.
>
> On Wednesday, June 28, 2023 at 11:50:56 AM UTC-4 Brian Coca wrote:
>>
>> If you want to just see if things 'would change':
>> `check_mode: true`
>>
>> not all modules support it, but both cron and lininefile modules say they do

Putting cron jobs in individual lines of a shared cron file has
always been an iffy sort of task. Errors in ansible scripting can lead
to putting in duplicate lines, and failing to clear slightly older
formats of the same line. It's why I encourage using "/etc/cron.daily"
files and similar hoptions. Those can also help prevent running your
cron job in the midst of another interfering cron jobs, such as
database backups and filesystem backups.
Reply all
Reply to author
Forward
0 new messages