ansible multiple with_items 并在清单组中的所有主机上循环

ansible multiple with_items and loop on all hosts in inventory group

团队,我有一种情况需要在多个主机上执行多个命令。对于单个主机情况,下面的情况很好,但如何在多个主机上重复相同的情况?

      - name: "SMI Tests for ECC singlebit and double bit codes "
        command: "smi --xml-format --query | grep retired_count | grep -v 0"
        ignore_errors: no
        register: _smi_ecc_result
        failed_when: _smi_ecc_result.rc == 0
        delegate_to: "{{ item }}"
        with_items: "{{ groups['kube-gpu-node'] }}"

现在,我有更多的命令可以执行,我应该如何修改上面的内容,以便它在进入 with_items 的每个主机上执行这些命令。

例如: 命令:df -kh 命令:ls -ltr



      - name: "multi_commands Tests for ECC singlebit and double bit codes "
        command: 
           - "smi --xml-format --query | grep retired_count | grep -v 0"
           - "df -kh"
           - "ls -ltr"
        ignore_errors: no
        register: multi_commands_result
        failed_when: multi_commands_result.rc == 0
        delegate_to: "{{ item }}"
        with_items: "{{ groups['kube-gpu-node'] }}"

但出现语法错误。

您可以在命令模块中使用 argv here 来传递多个命令,或者使用 shell 来传递多个命令,如下所示。

- name: "multi_commands Tests for ECC singlebit and double bit codes "
  shell: |
      smi --xml-format --query | grep retired_count | grep -v 0
      df -kh
      ls -ltr
  ignore_errors: no
  register: multi_commands_result
  failed_when: multi_commands_result.rc != 0
  delegate_to: "{{ item }}"
  with_items: "{{ groups['kube-gpu-node'] }}"