【问题标题】:Configure cron with Ansible使用 Ansible 配置 cron
【发布时间】:2020-04-12 10:33:39
【问题描述】:

我以为用 Ansible cron 模块配置 crontab 会是小菜一碟,但有些地方我看不懂。

这是我写的简单任务:

- name: Add job triggering logs rotation.
  cron:
    name: 'logrotate'
    minute: '*/2'
    job: '/etc/cron.daily/logrotate'
    state: present
  tags: cronjob

当我运行它时,Ansible 的输出正如我所想的那样成功:

TASK [cron : Add job triggering logs rotation.] **************************************************************************************
task path: /home/vagrant/ansible/roles/cron/tasks/main.yml:3
Thursday 19 December 2019  19:31:05 +0000 (0:00:00.023)       0:00:02.427 *****
Using module file /usr/lib/python3/dist-packages/ansible/modules/system/cron.py
<127.0.0.1> ESTABLISH SSH CONNECTION FOR USER: None
<127.0.0.1> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 127.0.0.1 '/bin/sh -c '"'"'sudo -H -S -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-; /usr/bin/python'"'"'"'"'"'"'"'"' && sleep 0'"'"''
Escalation succeeded
<127.0.0.1> (0, b'\n{"envs": [], "invocation": {"module_args": {"name": "logrotate", "insertbefore": null, "state": "present", "cron_file": null, "reboot": false, "hour": "*", "month": "*", "disabled": false, "job": "/etc/cron.daily/logrotate", "special_time": null, "user": null, "env": null, "insertafter": null, "backup": false, "day": "*", "minute": "*/2", "weekday": "*"}}, "changed": true, "jobs": ["logrotate"], "warnings": []}\n', b'')
changed: [127.0.0.1] => {
    "changed": true,
    "envs": [],
    "invocation": {
        "module_args": {
            "backup": false,
            "cron_file": null,
            "day": "*",
            "disabled": false,
            "env": null,
            "hour": "*",
            "insertafter": null,
            "insertbefore": null,
            "job": "/etc/cron.daily/logrotate",
            "minute": "*/2",
            "month": "*",
            "name": "logrotate",
            "reboot": false,
            "special_time": null,
            "state": "present",
            "user": null,
            "weekday": "*"
        }
    },
    "jobs": [
        "logrotate"
    ]
}

但是,当我检查 /etc/crontab/ 时,输出不存在。

我想要实现的目标是在/etc/crontab/

*/5 * * * * /etc/cron.daily/logrotate 

默认情况下应该将此作业添加到根 crontab,但我也会检查我的用户 contab 以确保它不存在并且不存在。

现在,每次我尝试运行任务时,Ansible 的输出都是OK。我不知道它是在哪里添加的,也不知道出了什么问题。

知道发生了什么吗?

【问题讨论】:

    标签: linux cron ansible


    【解决方案1】:

    您定义 ansible 任务的方式将在用户默认的 cronfile 中创建作业。你可以用“crontab -l”查看它,或者用“crontab -e”编辑它。 使用“cron_file”选项时,可以将其放入“/etc/cron.d”文件夹。 看看https://docs.ansible.com/ansible/latest/modules/cron_module.html

    【讨论】:

      【解决方案2】:

      @EnlightMe 表示使用 crontab -l 可以看到输出。

      将脚本更改为:

      - name: Add job triggering logs rotation.
        cron:
          cron_file: '/etc/crontab'
          user: 'root'
          name: 'logrotate'
          minute: '*/2'
          job: '/etc/cron.daily/logrotate'
          state: present
        tags: cronjob
      

      解决了问题。

      【讨论】:

        猜你喜欢
        • 2019-11-05
        • 2020-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-25
        • 2020-12-19
        相关资源
        最近更新 更多