LoginSignup
0
0

More than 1 year has passed since last update.

【ルール説明・shared】no-handler

Last updated at Posted at 2022-12-22

こちらの記事はAnsible lint Advent Calendar 2022 23日目の記事になります。

今回はルール no-handler について説明します。

no-handler

no-handlerhandler が正しく設定されているか検証します。

条件にwhen: result.changed(もしくはwhen: 変数.changed)が付与された場合、Ansibleはその処理を handler と認識します。しかし handler には正式な書き方があるためそちらの書式で書く事が推奨されています。

問題のあるコード

---
- 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

タスクの結果を変数に登録せずに handler を直接実行します。

---
- 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!

参考サイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0