问题描述
我必须在Ansible中创建crontab,以使呈现的crontab应该具有多个MAILTO
,这基于以下cron任务。例如,最终的crontab可能如下所示
MAILTO=person1@domain.com
#Ansible: TEST 1
0 * * * * ls -ahl > /dev/null
MAILTO=person2@domain.com
#Ansible: TEST 2
1 * * * * ls -ahl > /dev/null
MAILTO
是一个环境变量,如果我多次指定它,它将被覆盖
使用env
的剧本示例。
- cron:
env: true
name: MAILTO
job: person1@domain.com
state: "present"
insertbefore: "TEST 1"
- cron:
name: "TEST 1"
job: "ls -ahl > /dev/null"
state: "present"
minute: "0"
hours: "5,2"
- cron:
env: true
name: MAILTO
job: person2@domain.com
state: "present"
insertbefore: "TEST 2"
- cron:
name: "TEST 2"
job: "ls -ahl > /dev/null"
state: "present"
minute: "1"
hours: "4,2"
但是没有运气,我也尝试使用cronvar
- cronvar:
name: MAILTO
value: person1@domain.com
state: "present"
insertbefore: "TEST 1"
- cron:
name: "TEST 1"
job: "ls -ahl > /dev/null"
state: "present"
minute: "0"
hours: "5,2"
- cronvar:
name: MAILTO
value: person2@domain.com
state: "present"
insertbefore: "TEST 2"
- cron:
name: "TEST 2"
job: "ls -ahl > /dev/null"
state: "present"
minute: "1"
hours: "4,2"
再没有运气
有人可以指出实现这一目标的方法吗?
解决方法
因为cronvar
旨在向文件中添加变量,这些变量是唯一的,并且由于我不确定that comment的存在,因此在所有实现上都可能有多个MAILTO
crontab
,因此Ansible可能不会支持它,我猜您最好的选择是求助于简单的旧lineinfile
。
当然,这不是很不错的部分,您必须制作一个主机,播放或任务变量来保存crontab文件的路径。
我快速浏览了文档,但似乎没有给出用户实际crontab的事实,并且由于它似乎是the cron module code itself的一部分,所以我不确定是否有找到它的好方法摆脱Ansible本身。
另一方面,标识应在何处添加行的部分似乎是未来的证明,因为它已明确记录为预期行为:
在管理crontab作业时:模块包括一行,其中crontab条目
"#Ansible: <name>"
的描述与传递给模块的“名称”相对应,以后的ansible / module调用将使用该行来查找/检查状态。 “名称”参数应该是唯一的,并且更改“名称”值将导致创建新的cron任务(或删除其他任务)。
摘自文档:https://docs.ansible.com/ansible/2.9/modules/cron_module.html
有了这一切,给出了剧本:
- hosts: all
gather_facts: no
vars:
cron_file: /etc/crontabs/root
tasks:
- cron:
name: "TEST 1"
job: "ls -ahl > /dev/null"
state: "present"
minute: "0"
hour: "5,2"
register: cron
- lineinfile:
path: "{{ cron_file }}"
insertbefore: '#Ansible: TEST 1'
line: 'MAILTO=person1@domain.com'
- cron:
name: "TEST 2"
job: "ls -ahl > /dev/null"
state: "present"
minute: "1"
hour: "4,2"
- lineinfile:
path: "{{ cron_file }}"
insertbefore: '#Ansible: TEST 2'
line: 'MAILTO=person2@domain.com'
这产生了回顾:
PLAY [all] *********************************************************************************************************
TASK [cron] ********************************************************************************************************
changed: [localhost]
TASK [lineinfile] **************************************************************************************************
changed: [localhost]
TASK [cron] ********************************************************************************************************
changed: [localhost]
TASK [lineinfile] **************************************************************************************************
changed: [localhost]
PLAY RECAP *********************************************************************************************************
localhost : ok=4 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在用于测试的Alpine的 / etc / crontabs / root 中:
MAILTO=person1@domain.com
#Ansible: TEST 1
0 5,2 * * * ls -ahl > /dev/null
MAILTO=person2@domain.com
#Ansible: TEST 2
1 4,2 * * * ls -ahl > /dev/null
最后但并非最不重要的是,重新运行完全相同的剧本表明尊重幂等性:
PLAY [all] *********************************************************************************************************
TASK [cron] ********************************************************************************************************
ok: [localhost]
TASK [lineinfile] **************************************************************************************************
ok: [localhost]
TASK [cron] ********************************************************************************************************
ok: [localhost]
TASK [lineinfile] **************************************************************************************************
ok: [localhost]
PLAY RECAP *********************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0