介绍
可靠的调度需要比成功的 play 汇总更多的证据。你将为普通用户部署受保护的报告程序,管理稳定的具名 cron 条目,观察自然执行,更新阶段,并移除独立的临时条目。最终检查将观察新一次调度器运行,同时保留现有的无关任务。
部署普通用户报告和具名计划任务
本步骤将部署提供的报告程序,并在两个受管主机上为 automation 用户安排执行。
cron 已安装,其守护进程已作为前置条件运行。节点不运行 systemd。各 automation crontab 还包含无关的 keep-existing-report 条目;请保留它。cron 模块管理稳定的具名条目,而非替换用户的整个 crontab。
进入项目并检查提供的控制节点程序:
cd /home/labex/project
cat files/host-report.sh
程序向受保护的受管日志追加 UTC 纪元时间戳、实际主机名及阶段参数。umask 027 使新日志文件权限为 0640。保持程序不变,让 cron 自然运行它;手动调用不能证明计划任务有效。
将阶段定义为可复用输入:
cat > group_vars/all.yml <<'YAML'
report_phase: initial
YAML
创建 playbook。cron 任务具有分钟、小时、月中日期、月份和星期字段;每个字段均为星号时,每分钟运行一次。name 创建用于后续更新的 Ansible 标记,user automation 选择该用户的 crontab。become 允许管理这个具名用户的计划任务及明确的文件所有权;不会使计划程序以 root 运行。
cat > reporting.yml <<'YAML'
- name: Manage a scheduled ordinary-user host report
hosts: all
gather_facts: false
become: true
tasks:
- name: Prepare the ordinary user's reporting directory
ansible.builtin.file:
path: /home/automation/reporting
state: directory
owner: automation
group: automation
mode: "0750"
- name: Deploy the supplied report program
ansible.builtin.copy:
src: /home/labex/project/files/host-report.sh
dest: /home/automation/reporting/host-report.sh
owner: automation
group: automation
mode: "0750"
- name: Keep the named report schedule present
ansible.builtin.cron:
name: ansible-host-report
user: automation
minute: "*"
hour: "*"
day: "*"
month: "*"
weekday: "*"
job: "/home/automation/reporting/host-report.sh {{ report_phase }}"
YAML
验证并应用:
ansible-playbook reporting.yml --syntax-check
ansible-playbook reporting.yml
检查实际 automation crontab。命令使用 become 运行,以便明确指定查询账户:
ansible all -b -m ansible.builtin.command -a 'crontab -u automation -l'
各 crontab 应包含一个 ansible-host-report 标记及每分钟执行的命令,以及未更改的 keep-existing-report 条目。仅一行 crontab 不能证明执行。
等待首次自然调度时刻。timeout 将等待限制为 90 秒;bash 的 until 循环每两秒检查非空日志。命令只读取状态,日志由 cron 创建:
ansible all -m ansible.builtin.command -a "timeout 90 bash -c 'until test -s /home/automation/reporting/host-report.log; do sleep 2; done'"
ansible all -m ansible.builtin.command -a 'cat /home/automation/reporting/host-report.log'
各日志应有类似 time= host=web1 phase=initial 的记录,web2 上使用对应实际主机名。时间戳会变化,应与自然的分钟调度对齐。有时限的等待失败时,请检查计划任务和运行的守护进程,不要自行写入成功日志。
更新一个具名条目并观察新阶段
本步骤将改变任务输入,同时保留其稳定身份及无关计划任务。
更新保存的阶段值。cron 任务渲染新命令,但保留相同名称:
cat > group_vars/all.yml <<'YAML'
report_phase: verified
YAML
ansible-playbook reporting.yml
再次检查两个 crontab:
ansible all -b -m ansible.builtin.command -a 'crontab -u automation -l'
每台主机应仍恰好有一个 ansible-host-report 条目,现在以 verified 结尾。复用名称会更新已有条目,而非累积第二个计划任务。keep-existing-report 必须保持不变。
等待使用新阶段的自然运行。grep -q 检查匹配日志记录而不打印;timeout 和 until 循环使等待有时间上限:
ansible all -m ansible.builtin.command -a "timeout 90 bash -c 'until grep -q phase=verified /home/automation/reporting/host-report.log; do sleep 2; done'"
ansible all -m ansible.builtin.command -a 'cat /home/automation/reporting/host-report.log'
两者现在都应包含由实际调度器产生的新 phase=verified 记录。旧 initial 记录是有用历史,应予保留。更新 crontab 命令不会追溯改变旧日志行。
仅移除具名临时任务并保持报告稳定
本步骤将通过稳定名称移除临时计划任务,并验证预期普通用户报告继续执行。
使用临时 cron 模块调用创建无害的具名临时条目。它在午夜调度 /usr/bin/true,提供真实可移除条目:
ansible all -b -m ansible.builtin.cron -a 'name=ansible-temporary-report user=automation minute=0 hour=0 job=/usr/bin/true'
ansible all -b -m ansible.builtin.command -a 'crontab -u automation -l'
编写小型清理 playbook。state absent 和匹配的 name/user 仅移除该受管条目。不要清空或替换完整 crontab:
cat > cleanup.yml <<'YAML'
- name: Remove only the obsolete named schedule
hosts: all
gather_facts: false
become: true
tasks:
- name: Remove the obsolete report entry by its stable name
ansible.builtin.cron:
name: ansible-temporary-report
user: automation
state: absent
YAML
ansible-playbook cleanup.yml
ansible all -b -m ansible.builtin.command -a 'crontab -u automation -l'
临时标记和命令应已消失。阶段为 verified 的 ansible-host-report 和 keep-existing-report 都应保留。
重复两个声明:
ansible-playbook reporting.yml
ansible-playbook cleanup.yml
两个 playbook 都应对两台主机报告 changed=0、unreachable=0 和 failed=0。cron 可能独立地继续追加日志记录;这些自然运行记录不会使声明式部署失去幂等性。
最终验收会检查具名普通用户任务、精确部署的程序和受保护日志,然后等待两台主机上的下一次自然调度时刻。这可能需要约一分钟。保持报告计划任务和守护进程运行,使此次观察能证明持续执行,而非手动准备的成功文件。
总结
你为预期普通用户管理了稳定的具名 cron 条目,观察实际计划执行,更新命令输入且未重复创建任务,并仅移除了临时条目。明确的程序/日志权限与保留的无关计划任务使范围清晰。部署重复运行无更改,报告仍自然持续运行。



