跳到内容

risky-shell-pipe

此规则检查 Ansible shell 模块中 bash 的 pipefail 选项。

当将一个命令的输出管道传输到另一个命令时,您应该始终设置 pipefail。管道的返回状态是命令的退出状态。如果第一个命令失败,pipefail 选项可确保任务按预期失败。

由于此要求不适用于 PowerShell,对于 executable 属性内部具有 pwsh 的 shell 命令,此规则不会触发。

有问题的代码

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

正确的代码

---
- name: Example playbook
  hosts: localhost
  become: false
  tasks:
    - name: Pipeline with pipefail
      ansible.builtin.shell:
        cmd: set -o pipefail && false | cat
        executable: /bin/bash

    - name: Pipeline with pipefail, multi-line
      ansible.builtin.shell:
        cmd: |
          set -o pipefail # <-- adding this will prevent surprises
          false | cat
        executable: /bin/bash