【问题标题】:Is there any way to comment cron job using ansible有没有办法使用 ansible 来评论 cron 作业
【发布时间】:2021-06-24 00:59:59
【问题描述】:

我尝试使用以下代码-

 - name: "cron"
  hosts: localhost
  tasks:
    - cron:
        name: "Puppet agent"
        job: "/bin/true"
        state: present
        disabled: True

它没有评论现有的 cron 作业,而是附加了 #Ansible: test.我想修改现有的 cron 作业,而不是追加。

cron 作业看起来像这样- crontab -l

# puppet Name: Puppet agent
* * * * * /bin/true

执行ansible代码后的结果是: crontab -l

# puppet Name: Puppet agent
 * * * * * /bin/true
# Ansible: Puppet agent
#* * * * * /bin/true

【问题讨论】:

  • 好吧,ansible 预计以前的剧本使用 cron: 任务插入该条目,因此无法识别手动修改的 crontab 的部分。您需要使用...infile: 任务之一来使该 crontab 合规,以便开始使用 ansible 对其进行管理
  • 非常感谢您的回复。您能否详细解释一下 infile: 任务。我该如何使用它?
  • docs.ansible.com/ansible/latest/collections/ansible/builtin/…(有lineinfile和blockinfile模块)

标签: ansible cron


【解决方案1】:

从上面的 cmets 中,我发现如果 cron 由 ansible 维护,那么我们可以使用我之前的方法修改 cron 作业,否则 ...infile: 是最好的选择。我的要求是从 cron 作业中注释多行。所以,我的代码如下所示-

lineinfile:
      path: /var/spool/cron/root
      # Line to Search/Match against
      regexp: '{{item.From}}'
      # Line to Replace with
      line: '{{item.To}}'
      state: present  
    with_items:
     - { From: '/bin/true', To: '#* * * * * /bin/true'}
     - { From: '/bin/test.sh', To: '#* * * * * /bin/test.sh'}
     

我希望这对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-28
    • 2020-05-15
    • 2015-12-11
    • 2016-01-10
    • 2020-12-22
    • 1970-01-01
    • 2020-10-28
    • 2021-09-23
    相关资源
    最近更新 更多