【规则说明・共享】无处理程序
这篇文章是Ansible lint Advent Calendar 2022的第23天的文章。
这次我们将解释关于规则 no-handler。
无人操作者
no-handler 会验证 handler 是否被正确设置。
当条件 `when: result.changed` 或 `when: 変数.changed` 被添加时,Ansible 将识别该操作为 handler。然而,由于 handler 有正式的命名方式,因此建议使用该格式进行编写。
有问题的代码 de de
---
- name: Example of no-handler rule
hosts: localhost
tasks:
- name: Register result of a task
ansible.builtin.copy:
dest: "/tmp/placeholder"
content: "Ansible made this!"
mode: 0600
register: is_copied # <- 実行結果を変数へ登録する
- name: Second command to run
ansible.builtin.debug:
msg: The placeholder file was modified!
when: is_copied.changed # <- handler を実行する
修正后的代码1
---
# ルール no-handler の検証を無効化する
when: is_copied.changed # noqa: no-handler
修正后的代码2
不将任务结果存储在变量中,直接执行处理程序。
---
- name: Example of no-handler rule
hosts: localhost
tasks:
- name: Register result of a task
ansible.builtin.copy:
dest: "/tmp/placeholder"
content: "Ansible made this!"
mode: 0600
notify:
- Second command to run # <-- ファイルが変更された場合のみ handler が実行される
handlers:
- name: Second command to run
ansible.builtin.debug:
msg: The placeholder file was modified!
请参考以下网站
-
- no-handler — Ansible Lint Documentation
- Handlers: running operations on change — Ansible Documentation