【规则说明】一次性运行

本文是 Ansible lint Advent Calendar 2022 日历2第20天的文章。

这次我将解释一下关于“run-once”规则的内容。

执行一次

当 run-once 在任务中启用时,当 ansible.builtin.free 被指定时,将输出警告。

当使用Ansible时,默认情况下会按照指定的主机清单的顺序执行任务,但当指定 `ansible.builtin.free` 时,Ansible会按照与受管理节点(Managed node)的连接速度最快的顺序来执行任务。

只执行”run_once”选项在清单文件中指定的第一个主机的处理。而且”ansible.builtin.free”和”run_once”选项不能同时指定,如果指定了它们,将会产生错误。

执行示例

all:
  hosts:
    18.183.124.4:
    35.77.218.231:
    35.78.95.41:
---
- hosts: all
  become: true
  strategy: free
  serial: 3 

  tasks:
    - name: Get OS version
      command: cat /etc/redhat-release
      register: os_release

    - name: Debug
      debug:
        var: os_release
        run_once: true
ansible-playbook -u centos -i inventory.yml site.yml 

PLAY [all] *******************************************************************************

TASK [Gathering Facts] *******************************************************************
ok: [35.78.95.41]
ok: [35.77.218.231]
ok: [18.183.124.4]

TASK [Get OS version] ********************************************************************
changed: [18.183.124.4]
changed: [35.77.218.231]
changed: [35.78.95.41]

TASK [Debug] *****************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: NoneType: None
fatal: [18.183.124.4]: FAILED! => {"msg": "Invalid options for debug: run_once"}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: NoneType: None
fatal: [35.77.218.231]: FAILED! => {"msg": "Invalid options for debug: run_once"}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: NoneType: None
fatal: [35.78.95.41]: FAILED! => {"msg": "Invalid options for debug: run_once"}

PLAY RECAP *******************************************************************************
18.183.124.4               : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
35.77.218.231              : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
35.78.95.41                : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

有问题的代码

---
- name: "Example with run_once"
  hosts: all
  strategy: free # <-- strategy に free が設定されている
  gather_facts: false
  tasks:
    - name: Task with run_once
      ansible.builtin.debug:
        msg: "Test"
      run_once: true # <-- タスクで run_once が設定されている

修正后的代码1

- name: "Example without run_once"
  hosts: all
  gather_facts: false
  tasks:
    - name: Task without run_once
      ansible.builtin.debug:
        msg: "Test"

修改过的代码2

- name: "Example of using run_once with strategy other than free"
  hosts: all
  strategy: linear # <-- strategy に linear を設定する
  gather_facts: false
  tasks: # <-- unoqa を設定し Ansible lint の処理をスキップする
    - name: Task with run_once  # noqa: run_once[task]
      ansible.builtin.debug:
        msg: "Test"
      run_once: true

请提供网址作为参考

    run-once — Ansible Lint Documentation
广告
将在 10 秒后关闭
bannerAds