调整Ansible中的单用户的crontab mailto

发布于 2025-01-31 12:21:46 字数 1144 浏览 4 评论 0原文

我目前正在编辑类似特定用户的crontab条目:

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

这将在用户的crontab(crontab -l <​​/code>)中创建以下条目:

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

我想添加mailto =“” 到本用户的crontab的顶部。结果应该如下:

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

我尝试将其添加到我的Ansible Play:

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

这是没有错误的情况下运行的,但并未如上所述创建预期的结果。

我已经从另一个 ,但这仅在创建/etc/cron.d文件时起作用,而我的作业没有。

我如何通过我的原始游戏获得预期的结果?

从技术上讲,我可以使用lineInfile模块,然后手动编辑/var/spool/cron/exampleser文件,通过添加mailto =“”最重要的是,但据我所知,这被认为是不良的实践,也不是合理的解决方案。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光病人 2025-02-07 12:21:46

看来我没有正确阅读文档。查看默认为root,我添加了我的特定用户,并且问题已解决。对于其他任何人来说,我的剧本现在看起来像这样:

---
- 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

crontab -l <​​/code>,因为示例使用者现在给了我这一点:

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

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
亣腦蒛氧 2025-02-07 12:21:46

您可以指定cron_file指令在不同的cron文件中设置不同的mailto

---
# Set MAILTO
- name: MAILTO for some crons
  community.general.cronvar:
    name: MAILTO
    value: "\"\""
    user: root
    cron_file: "{{ item }}"
  loop:
    - cron_file_1
    - cron_file_2
    - cron_file_3

You can specify the cron_file directive to set a different MAILTO in different cron files.

---
# Set MAILTO
- name: MAILTO for some crons
  community.general.cronvar:
    name: MAILTO
    value: "\"\""
    user: root
    cron_file: "{{ item }}"
  loop:
    - cron_file_1
    - cron_file_2
    - cron_file_3
凉城已无爱 2025-02-07 12:21:46

我认为您正在寻找的是

eg:

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

- name: MAILTO exists for user username
  cronvar:
    name: MAILTO
    user: username
    value: "\"\""
    state: present
    

MAILTO=""
#Ansible: test

I think what you're looking for is cronvar_module

E.G.:

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

- name: MAILTO exists for user username
  cronvar:
    name: MAILTO
    user: username
    value: "\"\""
    state: present
    

MAILTO=""
#Ansible: test

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文