Search code examples
cronansible

Ansible adds the crontask on each apply


I have added the following line to cron to run the script on reboot

@reboot /usr/local/bin/autostart.sh

But when I prepared the ansible script for it I found that it adds 1 more line each time I apply the ansible.

The task is below:

- name: Add autostart script to cron
  cron:
    special_time: reboot
    user: user
    state: present
    job: /usr/local/bin/autostart.sh

And after several updates I get the following cron:

#Ansible: None
@reboot /usr/local/bin/autostart.sh
#Ansible: None
@reboot /usr/local/bin/autostart.sh
#Ansible: None
@reboot /usr/local/bin/autostart.sh
#Ansible: None
@reboot /usr/local/bin/autostart.sh

As for me this is strange behavior because state: present should check if the record is already present.

Or maybe have I missed anything else?


Solution

  • Add name parameter. For example

    - name: Add autostart script to cron
      cron:
        name: "autostart"
        special_time: reboot
        user: user
        state: present
        job: /usr/local/bin/autostart.sh
    

    Quoting from cron

    name: Description of a crontab entry or, if env is set, the name of environment variable. Required if state=absent. Note that if name is not set and state=present, then a new crontab entry will always be created, regardless of existing ones. This parameter will always be required in future releases.