ansible 多个 with_items 并在清单组中的所有主机上循环 [英] ansible multiple with_items and loop on all hosts in inventory group

查看:25
本文介绍了ansible 多个 with_items 并在清单组中的所有主机上循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

团队,我遇到了需要在多个主机上执行多个命令的情况.对于单一主机情况,下面很好,但如何在多个主机上迭代相同?

Team, I have a situation where I need to execute multiple commands on multiple hosts. for singular host case am fine with below but how to iterate the same over multiple hosts?

      - 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 的主机上完成这些命令.

Now, i have more commands to execute how should i modify above such that it done those on each hosts coming in with_items.

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

ex: command: df -kh command: 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 此处在命令模块中传递多个命令或使用shell传递多个命令如下.

Either you can use argv here in the command module to pass multiple commands or use shell to pass multiple commands as below.

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

这篇关于ansible 多个 with_items 并在清单组中的所有主机上循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆