LoginSignup
0
0

More than 1 year has passed since last update.

【ルール説明・shared】no-changed-when

Last updated at Posted at 2022-12-21

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

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

no-changed-when

no-changed-whenansible.builtin.command モジュールおよび ansible.builtin.shell モジュールにおいてタスクが冪等性を保証するか検証します。

Ansibleではほぼ全てのモジュールにおいて冪等性が保証されていますが ansible.builtin.command モジュールおよび ansible.builtin.shell モジュールではユーザーが処理が冪等になる条件を指定する必要があります。

冪等性とは

Wikipedia には 冪等性 とは大雑把に言って、ある操作を1回行っても複数回行っても結果が同じであることをいう概念とあります。

問題のあるコード

この処理は実行するたびにchangedが返却されます。

---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Does not handle any output or return codes
      ansible.builtin.command: cat {{ my_file | quote }} # <- 実行するたびに changed ステータスを返す。冪等性が保証されていない。

修正されたコード

changedが返却される条件を指定しそれ以外では冪等性を保証するように変更します。

---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Handle shell output with return code
      ansible.builtin.command: cat {{ my_file | quote }}
      register: my_output # <- コマンドの結果を変数にセットする
      changed_when: my_output.rc != 0 # <- 変数の値に対しステータスが changed になる条件を指定する

参考サイト

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