command-instead-of-shell¶
此规则识别在不需要时使用 shell
模块而不是 command
模块的情况。Shell 比 command 慢得多,除非特别需要使用 shell 功能(如环境变量扩展或使用管道链接多个命令),否则应避免使用。
有问题代码¶
---
- name: Problematic example
hosts: localhost
tasks:
- name: Echo a message
ansible.builtin.shell: echo hello # <-- command is better in this case
changed_when: false
正确代码¶
---
- name: Correct example
hosts: localhost
tasks:
- name: Echo a message
ansible.builtin.command: echo hello
changed_when: false
注意
此规则可以使用 --fix
选项自动修复。