-1

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?

U880D
  • 8,601
  • 6
  • 24
  • 40
Sam
  • 97
  • 1
  • 10

1 Answers1

1

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


... 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.

U880D
  • 8,601
  • 6
  • 24
  • 40
  • 1
    Unfortunately your comment doesn't add more information or details to your initial question, at least not for me. Maybe you can provide more, as well a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and by editing your question. – U880D Apr 20 '23 at 15:31