LoginSignup
0
0

More than 1 year has passed since last update.

ルール説明・safety】risky-shell-pipe

Last updated at Posted at 2022-12-18

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

今回はルール risky-shell-pipe について説明します。

risky-shell-pipe

risky-shell-pipeansible.builtin.shell モジュールでbashのpipefailオプションが付与されているか検証します。

bashのmanによると

pipefail オプションが有効になっている場合を除き、 パイプラインの返却ステータスは最後のコマンドの終了ステータスになります。 pipefail が有効になっている場合には、 0 以外のステータスを返した最後の (一番右の) コマンドの値が パイプラインの返却ステータスになり、 全てのコマンドが正常終了した場合にのみ 0 になります。

とあります。

例えば

false | true
echo "Ansible lint"

と書くとAnsible lintが表示されます。pipefailオプションを設定すると処理が途中で失敗しているためAnsible lintは表示されません。

set -o pipefail

false | true
echo "Ansible lint"

risky-shell-pipeはパイプ処理の途中でコマンドが失敗した場合、タスクが期待通りに失敗することを保証します。

問題のあるコード

---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Pipeline without pipefail
      shell: false | cat

修正されたコードその1

---
- name: Example playbook
  hosts: localhost
  become: no
  tasks:
    - name: Pipeline with pipefail
      shell: set -o pipefail && false | cat

修正されたコードその2

---
- name: Example playbook
  hosts: localhost
  become: no
  tasks:
    - name: Pipeline with pipefail, multi-line
      shell: |
        set -o pipefail # <-- pipefailオプションを付与する
        false | cat

参考サイト

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