跳转到内容

run-once

strategy 设置为 free 时,此规则会警告不要使用 run_once

此规则可以产生以下消息

  • run-once[play]: Play 使用 strategy: free
  • run-once[task]: 如果 strategy 设置为 free,使用 run_once 的行为可能会有所不同。

有关更多信息,请参阅 Ansible 文档中的以下主题

警告

此规则存在的原因是为了提醒用户,run_once 并不能保证任务只会运行一次。无论 'strategy' 字段中配置的值是什么,此规则都将始终触发。这是因为运行时使用的有效值可能与文件中的值不同。例如,ansible 命令行参数可以更改它。

添加 # noqa: run-once[task] 来标记警告已被确认并忽略是完全可以的。

有问题的代码

---
- name: "Example with run_once"
  hosts: all
  strategy: free # <-- avoid use of strategy as free
  gather_facts: false
  tasks:
    - name: Task with run_once
      ansible.builtin.debug:
        msg: "Test"
      run_once: true # <-- avoid use of strategy as free at play level when using run_once at task level

正确的代码

- name: "Example without run_once"
  hosts: all
  gather_facts: false
  tasks:
    - name: Task without run_once
      ansible.builtin.debug:
        msg: "Test"
- name: "Example of using run_once with strategy other than free"
  hosts: all
  strategy: linear
  # strategy: free # noqa: run-once[play] (if using strategy: free can skip it this way)
  gather_facts: false
  tasks: # <-- use noqa to disable rule violations for specific tasks
    - name: Task with run_once # noqa: run-once[task]
      ansible.builtin.debug:
        msg: "Test"
      run_once: true