Search code examples
ansiblecron

Update root crontab with Ansible


I have the following Ansible task:

    - name: foo
      ansible.builtin.cron:
        name: "bar"
        minute: "*/5"
        job: " /home/mytask.sh"
        user: root

The goal is to update root's crontab (similiar to sudo crontab -e). However I get the following error:

fatal: [192.168.1.11]: FAILED! => {"changed": false, "msg": "must be privileged to use -u\n"}

Usually, in Ansible, the option become: true is used to perform task as root, but the cron module does not support it. How could I modify my task accordingly to my needs?


Solution

  • become: true is a task/play/inventory level option not a module option. At task level for example you can use:

        - name: foo
          ansible.builtin.cron:
            name: "bar"
            minute: "*/5"
            job: " /home/mytask.sh"
            user: root
          become: true
    

    See ansible privilege escalation