介绍
一个 playbook 可以为各主机选择不同工作,无需复制整个任务列表。你将结合布尔策略输入和操作系统事实,处理结构化列表的每个条目,并使用真实的注册检查结果做出独立的批准决定。真实文件和明确的清理操作,将使选中及跳过的行为都可观察。
使用布尔输入和事实选择任务
本步骤将使一个可复用 playbook 对两个主机采取不同操作,并在策略禁用时明确移除输出。
预先准备的环境仅在 web1 上有独立的批准输入 /home/automation/maintenance-approved。暂时保留该输入不变;首次策略决定使用布尔变量和发现的操作系统事实。
进入控制节点项目:
cd /home/labex/project
主机变量文件将数据与各清单名称关联。编写不带引号的 YAML 布尔值,使其保持为 true/false 值,而非字符串:
cat > host_vars/web1.yml <<'YAML'
maintenance_enabled: true
YAML
cat > host_vars/web2.yml <<'YAML'
maintenance_enabled: false
YAML
when 关键字为当前主机计算布尔表达式。不要用 Jinja 花括号包裹表达式。这里 and 要求策略启用且事实为 Debian 家族。not 反转布尔值,or 允许任一原因选中清理分支。
cat > maintenance.yml <<'YAML'
- name: Apply a host-specific maintenance policy
hosts: all
gather_facts: true
tasks:
- name: Prepare the common policy directory
ansible.builtin.file:
path: /home/automation/maintenance-policy
state: directory
mode: "0750"
- name: Record enabled Debian maintenance
ansible.builtin.copy:
dest: /home/automation/maintenance-policy/enabled.txt
content: "Enabled maintenance\n"
mode: "0640"
when: maintenance_enabled and ansible_facts.os_family == "Debian"
- name: Remove the marker when the policy is disabled
ansible.builtin.file:
path: /home/automation/maintenance-policy/enabled.txt
state: absent
when: not maintenance_enabled or ansible_facts.os_family != "Debian"
YAML
公共目录存在于两台主机。仅启用策略的 Debian 主机收到标记;互补的 absent 任务在策略禁用时移除标记。仅跳过创建任务会留下旧标记,因此清理是独立的期望状态任务。
验证并应用策略:
ansible-playbook maintenance.yml --syntax-check
ansible-playbook maintenance.yml
输出应为 web1 和 web2 显示不同的 changed/ok/skipped 决定。检查两台主机的实际标记元数据:
ansible all -m ansible.builtin.stat -a 'path=/home/automation/maintenance-policy/enabled.txt'
stat 应在 web1 报告 exists true,在 web2 报告 false。为练习清理,对两台主机覆盖布尔值。JSON 额外变量保留布尔 false 类型;外层单引号防止 shell 解释 JSON:
ansible-playbook maintenance.yml -e '{"maintenance_enabled":false}'
ansible all -m ansible.builtin.stat -a 'path=/home/automation/maintenance-policy/enabled.txt'
现在两个标记都应缺失。不带临时覆盖运行,以恢复保存的各主机策略:
ansible-playbook maintenance.yml
web1 应重新获得标记,web2 保持禁用。覆盖未编辑保存的主机变量文件。
应用结构化循环中的每个条目
本步骤将为启用策略的主机上的每个声明条目生成真实的维护检查记录。
YAML 列表包含多个以短横线开始的条目。下面每个条目都是具有 name 和 purpose 字段的映射。all 的组变量使列表对两个主机均可用,已有布尔条件仍决定哪个主机收到输出。
cat > group_vars/all.yml <<'YAML'
maintenance_checks:
- name: disk
purpose: Inspect disk space
- name: logs
purpose: Inspect application logs
- name: packages
purpose: Inspect package state
YAML
loop 为每个列表条目重复一个任务。迭代中 item.name 和 item.purpose 读取该条目字段。when 条件为各主机及条目求值。将这些任务追加到现有 playbook,保持缩进与此前任务对齐:
cat >> maintenance.yml <<'YAML'
- name: Prepare checks only for enabled Debian hosts
ansible.builtin.file:
path: /home/automation/maintenance-policy/checks
state: directory
mode: "0750"
when: maintenance_enabled and ansible_facts.os_family == "Debian"
- name: Write every declared maintenance check
ansible.builtin.copy:
dest: "/home/automation/maintenance-policy/checks/{{ item.name }}.txt"
content: |
host={{ inventory_hostname }}
check={{ item.name }}
purpose={{ item.purpose }}
mode: "0640"
loop: "{{ maintenance_checks }}"
when: maintenance_enabled and ansible_facts.os_family == "Debian"
- name: Remove check outputs when the policy is disabled
ansible.builtin.file:
path: /home/automation/maintenance-policy/checks
state: absent
when: not maintenance_enabled or ansible_facts.os_family != "Debian"
YAML
清理任务移除禁用主机上的整个 checks 目录,包括此前启用策略留下的旧输出。循环在启用的主机上写入稳定内容,并明确权限。
应用扩展后的 playbook:
ansible-playbook maintenance.yml
web1 应有三个循环结果,每项检查一个。web2 应跳过这些写入,且无 checks 目录。读取 web1 的全部三个实际产物,不要将一个成功条目视为整个列表的证据:
ansible web1 -m ansible.builtin.command -a 'cat /home/automation/maintenance-policy/checks/disk.txt /home/automation/maintenance-policy/checks/logs.txt /home/automation/maintenance-policy/checks/packages.txt'
输出应为每条记录标识 host=web1,并显示相应检查名称及用途。单独检查禁用主机:
ansible web2 -m ansible.builtin.stat -a 'path=/home/automation/maintenance-policy/checks'
其 stat 结果应报告 exists false。这些记录声明维护检查指令;它们并非声称执行了磁盘、日志或软件包检查本身。
根据注册的检查结果分支
本步骤将使用真实 stat 结果作为独立决策输入,并确认完整策略重复运行时稳定。
批准输入与 maintenance_enabled 独立。stat 任务始终检查各主机,随后 register 保存该主机结果。stat.exists 布尔值安全地区分输入存在与路径缺失。追加检查及互补输出任务:
cat >> maintenance.yml <<'YAML'
- name: Inspect the independent approval input
ansible.builtin.stat:
path: /home/automation/maintenance-approved
register: approval_info
- name: Record an existing approval input
ansible.builtin.copy:
dest: /home/automation/maintenance-policy/approved.txt
content: "Approval input exists\n"
mode: "0640"
when: approval_info.stat.exists
- name: Remove the output when approval input is absent
ansible.builtin.file:
path: /home/automation/maintenance-policy/approved.txt
state: absent
when: not approval_info.stat.exists
YAML
copy 任务使用注册的 exists 值。absent 任务覆盖相反情况,因此移除批准输入也会移除过时批准输出。此分支基于检查状态,而非固定 debug 消息。
运行完整策略:
ansible-playbook maintenance.yml
预先准备的批准输入仅存在于 web1,因此仅 web1 应有 approved.txt。检查两个实际输出路径:
ansible all -m ansible.builtin.stat -a 'path=/home/automation/maintenance-policy/approved.txt'
web1 应报告 exists true,web2 为 false。重复 playbook:
ansible-playbook maintenance.yml
两行汇总均应报告 changed=0、failed=0 和 unreachable=0。条件为 false 时跳过任务是预期行为;每个要求的循环条目及选中输出仍必须存在且内容正确。
总结
你使用各主机的布尔数据和发现的事实选择任务,在循环中处理每个映射,并根据注册的 stat 元数据分支。互补的 absent 任务移除了过时输出,而非假设跳过创建就能撤销旧状态。两个主机收敛到各自不同的策略结果,且无不必要的重复更改。



