ansiblecron

Adjusting crontab MAILTO for single user in Ansible


I'm currently editing the crontab entry of a specific user like this:

---
- name: Create example cron entry
  cron:
      name: "Examplecrontab entry"
      minute: "5"
      hour: "5"
      day: "5"
      user: exampleuser
      job: "examplejob"
      state: present

This creates the following entry in my user's crontab (crontab -l):

#Ansible: Example cron entry
5 5 5 * * examplejob

I want to add MAILTO="" to the top of this user's crontab. The results should be as follows:

MAILTO=""
#Ansible: Example cron entry
5 5 5 * * examplejob

I've tried adding this to my ansible play:

---
- name: Disable cron mailing to root
  cronvar:
    name: MAILTO
    value: "\"\""

This runs without error but doesn't create the expected result as mentioned above.

I've copied this from another post, but this only works when creating a /etc/cron.d file, which my job does not do.

How can I get the expected result with my original play?

Technically I could use the lineinfile module and manually edit the /var/spool/cron/exampleuser file by adding MAILTO="" manually to the top of it, but as far as I know that's considered bad practice and is not a legitimate solution.

Thanks in advance.


Solution

  • It seems I've failed to read the docs properly. Seeing the user property, which defaults to root, I've added my specific user and my issue has been fixed. For anyone else, my play looks like this now:

    ---
    - name: Disable cron mailing to root
      cronvar:
        name: MAILTO
        value: "\"\""
        user: exampleuser
    
    - name: Create example cron entry
      cron:
          name: "Examplecrontab entry"
          minute: "5"
          hour: "5"
          day: "5"
          user: exampleuser
          job: "examplejob"
          state: present
    

    doing crontab -l as the exampleuser now gives me this:

    MAILTO=""
    #Ansible: Example cron entry
    5 5 5 * * examplejob