使用 Ansible 更新根 crontab

Update root crontab with Ansible

我有以下 Ansible 任务:

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

目标是更新 root 的 crontab(类似于 sudo crontab -e)。但是我收到以下错误:

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

通常,在Ansible中,选项become: true用于以root身份执行任务,但cron模块不支持它。我如何根据需要修改我的任务?

become: true 是 task/play/inventory 级别选项而不是模块选项。例如,在任务级别,您可以使用:

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

ansible privilege escalation