Skip to content

Commit 27e4ae1

Browse files
authored
Ignore risky-shell-pipe with pwsh (#3166)
1 parent f698051 commit 27e4ae1

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

.config/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ prerun
255255
prettierignore
256256
programoutput
257257
psutil
258+
pwsh
258259
pyargs
259260
pycache
260261
pycharm

examples/playbooks/rule-risky-shell-pipe-pass.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,11 @@
6060
set -o pipefail
6161
df | grep '/dev'
6262
changed_when: false
63+
64+
- name: "PowerShell with pipefail should be ok, bug #3161"
65+
# https://github.com/ansible/ansible-lint/issues/3161
66+
ansible.builtin.shell:
67+
executable: /bin/pwsh
68+
cmd: |
69+
$ProgressPreference = 'this | that'
70+
changed_when: false

src/ansiblelint/rules/risky_shell_pipe.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
This rule checks for the bash `pipefail` option with the Ansible `shell` module.
44

5-
You should always set `pipefail` when piping output from a command to another.
6-
The return status of a pipeline is the exit status of the command.
7-
The `pipefail` option ensures that tasks fail as expected if the first command fails.
5+
You should always set `pipefail` when piping output from one command to another.
6+
The return status of a pipeline is the exit status of the command. The
7+
`pipefail` option ensures that tasks fail as expected if the first command
8+
fails.
9+
10+
As this requirement does apply to PowerShell, for shell commands that have
11+
`pwsh` inside `executable` attribute, this rule will not trigger.
812

913
## Problematic Code
1014

@@ -14,7 +18,7 @@ The `pipefail` option ensures that tasks fail as expected if the first command f
1418
hosts: localhost
1519
tasks:
1620
- name: Pipeline without pipefail
17-
shell: false | cat
21+
ansible.builtin.shell: false | cat
1822
```
1923
2024
## Correct Code
@@ -23,13 +27,13 @@ The `pipefail` option ensures that tasks fail as expected if the first command f
2327
---
2428
- name: Example playbook
2529
hosts: localhost
26-
become: no
30+
become: false
2731
tasks:
2832
- name: Pipeline with pipefail
29-
shell: set -o pipefail && false | cat
33+
ansible.builtin.shell: set -o pipefail && false | cat
3034

3135
- name: Pipeline with pipefail, multi-line
32-
shell: |
36+
ansible.builtin.shell: |
3337
set -o pipefail # <-- adding this will prevent surprises
3438
false | cat
3539
```

src/ansiblelint/rules/risky_shell_pipe.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ def matchtask(
4545

4646
jinja_stripped_cmd = self.unjinja(get_cmd_args(task))
4747

48+
# https://github.com/ansible/ansible-lint/issues/3161
49+
if "pwsh" in task["action"].get("executable", ""):
50+
return False
51+
4852
return bool(
4953
self._pipe_re.search(jinja_stripped_cmd)
5054
and not self._pipefail_re.search(jinja_stripped_cmd)

0 commit comments

Comments
 (0)