跳到内容

no-free-form

此规则识别任何使用自由格式模块调用语法的行为,并要求切换到完整语法。

自由格式语法,也称为内联简写,可能会产生细微的错误。它还会阻止编辑器和 IDE 为编辑的行提供反馈、自动完成和验证。

注意

只要您只是传递一个 YAML 字符串,其中包含一个 = 字符作为动作模块名称的参数,我们就认为这是在使用自由格式语法。请确保您将字典传递给模块,以便永远不会触发自由格式解析。

由于 raw 模块只接受自由格式,因此只有当我们在 raw 调用中检测到 executable= 的存在时,我们才会触发 no-free-form[raw]。我们建议显式使用 args: 来配置要运行的可执行文件。

此规则可以产生如下消息:

  • no-free-form - 不鼓励使用自由格式语法。
  • no-free-form[raw-non-string] - 将非字符串值传递给 raw 模块既没有文档记录也不受支持。

有问题的代码

---
- name: Example with discouraged free-form syntax
  hosts: localhost
  tasks:
    - name: Create a placefolder file
      ansible.builtin.command: chdir=/tmp touch foo # <-- don't use free-form
    - name: Use raw to echo
      ansible.builtin.raw: executable=/bin/bash echo foo # <-- don't use executable=
      changed_when: false

正确的代码

---
- name: Example that avoids free-form syntax
  hosts: localhost
  tasks:
    - name: Create a placefolder file
      ansible.builtin.command:
        cmd: touch foo # <-- ansible will not touch it
        chdir: /tmp
    - name: Use raw to echo
      ansible.builtin.raw: echo foo
      args:
        executable: /bin/bash # <-- explicit is better
      changed_when: false

注意

可以使用 --fix 选项自动修复此规则。