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

团队,我需要在多个主机上执行多个命令。单个主机的情况下可以使用以下命令,但如何在多个主机上进行相同的迭代?

      - 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'] }}"

但是出现语法错误。

elai30 回答:ansible多个with_items并在清单组中的所有主机上循环

您可以在命令模块中使用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'] }}"
本文链接:https://www.f2er.com/3156142.html

大家都在问