ansible-lint要求pipefail但在Ubuntu22.04的dash中报错求解决方案
解决Ubuntu 22.04下Ansible Shell任务
set -o pipefail报错的方案 方法1:指定bash作为执行shell
在ansible.builtin.shell任务中通过executable参数明确指定用bash执行,bash原生支持pipefail选项:
- name: 执行带管道错误检测的shell命令 ansible.builtin.shell: | set -o pipefail your_actual_command_here | some_other_command executable: /bin/bash
方法2:用dash兼容写法实现管道错误检测
如果不想切换shell,可以通过手动捕获前序命令退出码的方式,实现类似pipefail的效果:
- name: 在dash环境下处理管道错误 ansible.builtin.shell: | your_actual_command_here > temp_output; cmd_exit_code=$? some_other_command < temp_output [ $cmd_exit_code -ne 0 ] && exit $cmd_exit_code
这种写法会先执行核心命令并保存其退出码,再执行后续管道操作,最后根据前序命令的结果决定整个任务的退出状态。
方法3:修改目标机器默认sh为bash(谨慎使用)
若需要全局生效但不推荐,可能影响系统依赖dash的脚本,可在目标机器上修改默认shell:
sudo ln -sf /bin/bash /bin/sh
修改后需重新登录或重启会话生效,操作前需确认系统中没有依赖dash特定特性的关键脚本。
内容的提问来源于stack exchange,提问作者James Foster
相关产品推荐
相关产品推荐

